thelastapi
Get started free

Kimi API access: call Moonshot AI models with an OpenAI client

Call Moonshot AI's Kimi models through an OpenAI-compatible gateway at /v1/ai using model ids like moonshotai/kimi-k3.1-pro with one key.

On this page 6 sections

You can call Moonshot AI's Kimi models directly through the OpenAI client by pointing your base URL to thelastapi's gateway at /v1/ai and requesting Kimi model identifiers. You do not need a direct Moonshot account, custom SDKs, or foreign billing arrangements. Because the gateway accepts the standard chat completions payload, your existing code, tooling, and agents run as normal while the exact lab model you specify executes your request.

This guide covers how to discover available Kimi models, configure the OpenAI client, manage budgets, and handle model responses reliably.

How to call Moonshot AI models using an OpenAI client

thelastapi provides an OpenAI-compatible endpoint at https://thelastapi.dev/v1/ai. It supports chat completions (including streaming, tool calling, and JSON mode) across hundreds of lab models. When you route a request to the gateway, you pass the Moonshot AI model identifier in the model field.

The platform never silently swaps models. If you call a Kimi model, exactly that model runs. If the model or provider experiences downtime, the gateway returns a clean 503 error (model_unavailable or provider_unavailable) with a retry hint rather than rerouting your prompt to an alternative provider without your knowledge.

Finding Moonshot models in the catalogue

Beyond named aliases like vibiz/fast or vibiz/smart, you can call over 400 language models from major labs—including OpenAI, Anthropic, Google, DeepSeek, xAI, Qwen, Mistral, Meta, and Moonshot AI.

To look up available Kimi models and their token prices, use the vibiz CLI:

Terminal
vibiz ai models kimi

You can also query the gateway directly using plain HTTP:

Terminal
curl "https://thelastapi.dev/v1/ai/models?all=1&q=kimi" \
  -H "Authorization: Bearer $VIBIZ_API_KEY"

If an identifier is mistyped, the gateway rejects it immediately with a 400 invalid_request status code pointing to param: "model", preventing accidental runs against unexpected configurations.

Setting up your environment

When working within a project, initialize the required AI resources. This configures local environment variables automatically:

Terminal
vibiz init "Kimi Assistant" --resources ai
vibiz dev

The vibiz dev command generates .env.development.local containing:

  • VIBIZ_AI_BASE_URL
  • VIBIZ_AI_API_KEY
  • OPENAI_BASE_URL (set to https://thelastapi.dev/v1/ai)
  • OPENAI_API_KEY

Because standard OpenAI environment variables are exported, the default OpenAI SDK client works out of the box.

Code examples: Node.js, Python, and cURL

You can execute calls through modern language SDKs or plain HTTP. To see how this pattern works across different labs, read our guide on how to change the base URL to call other AI models from OpenAI code.

Calling Kimi with the OpenAI TypeScript SDK

TypeScript
import OpenAI from 'openai';

const openai = new OpenAI({
  baseURL: process.env.VIBIZ_AI_BASE_URL,
  apiKey: process.env.VIBIZ_AI_API_KEY,
});

async function main() {
  const completion = await openai.chat.completions.create({
    model: 'moonshotai/kimi-k3.1-pro',
    messages: [
      { role: 'system', content: 'You are a precise technical writer.' },
      { role: 'user', content: 'Explain distributed locks in two sentences.' },
    ],
    stream: false,
  });

  console.log(completion.choices[0]?.message.content);
}

main();

If your project uses @vibiz/app, you can initialize the client using the built-in configuration helper without tracking individual URL variables:

TypeScript
import OpenAI from 'openai';
import { vibiz } from '@vibiz/app';

const openai = new OpenAI(vibiz.ai);

Calling Kimi via HTTP (cURL)

You can call the gateway directly via POST /v1/ai/chat/completions:

Terminal
curl https://thelastapi.dev/v1/ai/chat/completions \
  -H "Authorization: Bearer $VIBIZ_API_KEY" \
  -H "Content-type: application/json" \
  -d '{
    "model": "moonshotai/kimi-k3.1-pro",
    "messages": [
      { "role": "user", "content": "Outline three database migration risks." }
    ]
  }'

Every successful JSON response includes standard chat completion objects alongside additional metadata:

  • served_model: The exact model that executed the prompt.
  • cost_micro_usd: The exact execution cost for that specific request in micro-USD.

For projects using multiple labs under one roof, see our guide on accessing multiple AI models with a single API.

Spend limits, budgets, and per-call tracking

Building production features on language models requires safeguards to avoid unexpected bills. thelastapi enforces guardrails across accounts, projects, and users.

Safety LayerDefaultHow to adjustBehavior when reached
Account Daily Spend$5 per UTC dayPATCH /v1/billing or console ▸ Billing429 rate_limited (daily_spend)
Project AI Budget$1 per UTC dayPATCH /v1/projects/:id/ai or vibiz ai budget429 rate_limited (project_budget)
Per-User Daily BudgetNone (null)PATCH /v1/projects/:id/ai (end_user_daily_budget_usd)429 rate_limited (end_user_budget)

Accounts operate on a prepaid system. A new account includes $2 of trial credit. Adding credits or configuring auto-recharge is managed in the console or via the CLI:

Terminal
vibiz billing top-up 25
vibiz billing auto-recharge on --threshold 5 --amount 25

Before any model runs, each call's worst-case spend (max_tokens at list price) is checked against your available balance and daily limits. By default, max_tokens is 1,024 when omitted, with a maximum limit of 16,384 depending on model support.

To audit historical usage by model or tool, run:

Terminal
vibiz usage --by model

Detailed limit rules and reset schedules are described in the Usage and limits documentation.

Error shapes and rate limits

Because the gateway speaks standard OpenAI error structures, existing error-handling logic catches issues without modification:

JSON
{
  "error": {
    "code": "rate_limited",
    "message": "Daily spend limit reached.",
    "type": "requests",
    "param": null,
    "details": {
      "reason": "daily_spend"
    }
  }
}

Common response statuses include:

  • 400 invalid_request: Model name not found in catalogue or bad request payload.
  • 401 unauthorized: Missing, revoked, or invalid API key.
  • 402 payment_required: Insufficient account credits to hold the worst-case token cost. See Billing and credits.
  • 429 rate_limited: Breached request limits, token limits, or spend ceilings. An account key allows 300 requests and 400k tokens per minute (50 in-flight). A project key allows 120 requests and 200k tokens per minute (25 in-flight).
  • 503 model_unavailable: The upstream provider is degraded or down. The gateway does not silently route to another model.

If you stream a completion via server-sent events (SSE) and an error occurs mid-stream, the connection terminates with a final error payload (data: {"error": {...}}) without emitting the [DONE] marker.

To explore switching other foundation models under the same interface, see our walkthrough on one API key for GPT, Claude, and Gemini. For media models like video generation, consult the guide on calling Kling, Seedance, and Veo by model id.

To start making requests against Moonshot AI models, check out the complete endpoint reference in the AI documentation or create your account at thelastapi.dev/start.

Frequently asked questions

Can I use Moonshot AI models with the official OpenAI Python or Node.js SDK?

Yes. Set the client's `base_url` to `https://thelastapi.dev/v1/ai`, pass your thelastapi key as the API key, and specify the Kimi model identifier in the `model` parameter.

What happens if a Kimi model experiences an outage?

The gateway returns a `503 model_unavailable` or `provider_unavailable` error with a retry hint. It never reroutes your request to a different model or provider behind your back.

How can I check the exact cost of a Moonshot API call?

Every non-streaming JSON response from `/v1/ai/chat/completions` includes the `cost_micro_usd` integer field, which reports the actual cost of the call in micro-USD.

What rate limits apply when calling Moonshot models through thelastapi?

Account keys support 300 requests and 400k tokens per minute with 50 concurrent requests in flight. Project keys default to 120 requests and 200k tokens per minute with 25 concurrent requests.