Skip to main content

Moonshot AI

还没翻译译自英文(ai-sdk.dev)本页 75 段都还没译

The Moonshot AI provider offers access to powerful language models through the Moonshot API, including the Kimi series of models with reasoning capabilities. You can use Moonshot AI models through AI Gateway (recommended) or connect directly with @ai-sdk/moonshotai.

import { generateText } from 'ai';

const { text } = await generateText({
  model: 'anthropic/claude-sonnet-5.5',
  prompt: 'Write a vegetarian lasagna recipe for 4 people.',
});

console.log(text);

Using AI Gateway

The AI SDK uses AI Gateway by default for model strings. Gateway provides access to models across providers through a single API key, with usage and cost tracking. Install ai:

pnpm add ai
npm install ai
yarn add ai
bun add ai

Set an AI Gateway API key in your environment:

export AI_GATEWAY_API_KEY=your_api_key_here

Vercel deployments can also use OIDC authentication. See the AI Gateway provider documentation for configuration and the model catalog for available models.

Gateway feature and provider-option support depends on the model and the provider serving the request. See Gateway provider options.

Using @ai-sdk/moonshotai

For direct access to Moonshot AI, install ai and @ai-sdk/moonshotai:

pnpm add ai @ai-sdk/moonshotai
npm install ai @ai-sdk/moonshotai
yarn add ai @ai-sdk/moonshotai
bun add ai @ai-sdk/moonshotai

Set your Moonshot AI API credential in the MOONSHOT_API_KEY environment variable:

export MOONSHOT_API_KEY=your_api_key_here

AI Gateway does not expose every API in @ai-sdk/moonshotai. Use the direct provider for provider-specific APIs or custom connection settings.

Note

The following sections document the direct @ai-sdk/moonshotai provider, including its model factories and settings. Gateway model and feature availability can differ.

API keys can be obtained from the Kimi API Platform.

Provider Instance

You can import the default provider instance moonshotai from @ai-sdk/moonshotai:

import { moonshotai } from '@ai-sdk/moonshotai';

For custom configuration, you can import createMoonshotAI and create a provider instance with your settings:

import { createMoonshotAI } from '@ai-sdk/moonshotai';

const moonshotai = createMoonshotAI({
  apiKey: process.env.MOONSHOT_API_KEY ?? '',
});

You can use the following optional settings to customize the Moonshot AI provider instance:

  • baseURL string

    Use a different URL prefix for API calls. The default prefix is https://api.moonshot.ai/v1

  • apiKey string

    API key that is being sent using the Authorization header. It defaults to the MOONSHOT_API_KEY environment variable

  • headers Record<string,string>

    Custom headers to include in the requests

  • fetch (input: RequestInfo, init?: RequestInit) => Promise<Response>

    Custom fetch implementation

Language Models

You can create language models using a provider instance:

import { moonshotai } from '@ai-sdk/moonshotai';
import { generateText } from 'ai';

const { text } = await generateText({
  model: moonshotai('kimi-k3'),
  prompt: 'Write a vegetarian lasagna recipe for 4 people.',
});

You can also use the .chatModel() or .languageModel() factory methods:

const model = moonshotai.chatModel('kimi-k3');
// or
const model = moonshotai.languageModel('kimi-k3');

Moonshot AI language models can be used in the streamText function (see AI SDK Core).

Moonshot V1 models support the standard temperature, topP, presencePenalty, and frequencyPenalty settings. Kimi models use fixed sampling parameters; the provider omits those settings for Kimi requests and returns an unsupported-setting warning when they are supplied.

Kimi K3 supports toolChoice: 'required'. Kimi K2.6 and the Kimi K2.7 Code models reject that setting, so the provider omits it and returns an unsupported warning for those model IDs. Other tool choice modes are passed through unchanged.

Structured Outputs

Native structured outputs are enabled for all model IDs, including Kimi K models, the official Moonshot V1 text, auto, and vision models, and custom or future model IDs.

The provider normalizes schemas to Moonshot's supported JSON Schema subset and enables strict schema validation by default.

For best reliability, it is strongly recommended to include your schema requirements in your prompt in addition to passing the schema through Output.

Message Names

Moonshot AI supports optional participant names on system, user, and assistant messages. Set providerOptions.moonshotai.name on each message that should include a name:

import {
  moonshotai,
  type MoonshotAIMessageProviderOptions,
} from '@ai-sdk/moonshotai';
import { generateText } from 'ai';

const { text } = await generateText({
  model: moonshotai('kimi-k3'),
  messages: [
    {
      role: 'user',
      content: 'Suggest a name for my neighborhood book club.',
      providerOptions: {
        moonshotai: {
          name: 'organizer',
        } satisfies MoonshotAIMessageProviderOptions,
      },
    },
  ],
});

The same option works with streamText. Names are omitted when unset. The provider ignores a name on a tool message and returns an unsupported warning. Avoid including unnecessary personal or identifying information in message names.

Partial Mode

Moonshot AI's Partial Mode continues the content of a final assistant message. Set partial: true in that assistant message's provider options:

import {
  moonshotai,
  type MoonshotAIAssistantMessageProviderOptions,
} from '@ai-sdk/moonshotai';
import { generateText } from 'ai';

const prefix = 'The sky is';

const { text } = await generateText({
  model: moonshotai('kimi-k3'),
  messages: [
    {
      role: 'user',
      content: 'Write one short sentence about the color of the sky.',
    },
    {
      role: 'assistant',
      content: prefix,
      providerOptions: {
        moonshotai: {
          partial: true,
        } satisfies MoonshotAIAssistantMessageProviderOptions,
      },
    },
  ],
});

const completeResponse = prefix + text;

The partial message must be the final assistant message in the prompt. Moonshot returns only the continuation, so concatenate the prefix and generated text when you need the complete response.

Note

Partial Mode cannot be combined with Moonshot's json_object response format. The provider rejects that combination before sending the request. A supported json_schema response remains available, but structured-output parsing validates only the returned continuation.

Kimi K3 Dynamic Tool Loading

Kimi K3 can load complete function tool definitions at arbitrary positions in a conversation. Add tools to the Moonshot provider options of an empty system message. The provider sends { role: 'system', tools: [...] } without a content field and applies the same Moonshot Flavored JSON Schema normalization used for top-level tools.

import {
  moonshotai,
  type MoonshotAISystemMessageProviderOptions,
} from '@ai-sdk/moonshotai';
import { generateText } from 'ai';

const result = await generateText({
  model: moonshotai('kimi-k3'),
  allowSystemInMessages: true,
  messages: [
    { role: 'user', content: 'Help me prepare for a trip.' },
    { role: 'assistant', content: 'I can help with that.' },
    {
      role: 'system',
      content: '',
      providerOptions: {
        moonshotai: {
          tools: [
            {
              type: 'function',
              name: 'get_weather',
              description: 'Get the current weather for a city',
              inputSchema: {
                type: 'object',
                properties: { city: { type: 'string' } },
                required: ['city'],
                additionalProperties: false,
              },
              strict: true,
            },
          ],
        } satisfies MoonshotAISystemMessageProviderOptions,
      },
    },
    { role: 'user', content: 'What is the weather in San Francisco?' },
  ],
});

Each entry must include its complete function definition. Top-level tools and dynamically loaded tools can be used in the same request. Known unsupported official models omit the dynamic message and return an unsupported warning; custom model IDs retain it for forward compatibility.

Note

Keep dynamic system messages in trusted server-side state. Tool names, descriptions, and schemas are sent to Moonshot and consume context. Dynamic declarations are request-local, so retain earlier declarations in later requests while those tools should remain available. Appending messages also preserves prompt-cache prefixes better than rewriting earlier declarations.

Reasoning Models

Kimi K3 and Kimi K3.1 Pro always reason and support low, high, and max reasoning effort. The default is max. You can configure the effort through provider options or the generic reasoning setting:

import {
  moonshotai,
  type MoonshotAILanguageModelOptions,
} from '@ai-sdk/moonshotai';
import { generateText } from 'ai';

const { text, reasoningText } = await generateText({
  model: moonshotai('kimi-k3'),
  providerOptions: {
    moonshotai: {
      reasoningEffort: 'high',
    } satisfies MoonshotAILanguageModelOptions,
  },
  prompt: 'How many "r"s are in the word "strawberry"?',
});

console.log(reasoningText);
console.log(text);

Kimi K2.5 and K2.6 can enable or disable thinking. Kimi K2.7 always has thinking and preserved reasoning enabled. Keep reasoning history in multi-turn Kimi K2.7 conversations. The reasoning output is exposed through the standard AI SDK reasoning parts.

import {
  moonshotai,
  type MoonshotAILanguageModelOptions,
} from '@ai-sdk/moonshotai';
import { streamText } from 'ai';

const result = streamText({
  model: moonshotai('kimi-k2.7-code'),
  providerOptions: {
    moonshotai: {
      thinking: { type: 'enabled' },
      reasoningHistory: 'preserved',
    } satisfies MoonshotAILanguageModelOptions,
  },
  prompt: 'How many "r"s are in the word "strawberry"?',
});

for await (const part of result.fullStream) {
  if (part.type === 'reasoning-delta') {
    process.stdout.write(part.text);
  } else if (part.type === 'text-delta') {
    process.stdout.write(part.text);
  }
}

See AI SDK UI: Chatbot for more details on how to integrate reasoning into your chatbot.

Log Probabilities

Moonshot AI can return token log probabilities for Chat Completions. Set logprobs to request token probabilities, or set topLogprobs to request the most likely alternatives at each token position. Setting topLogprobs automatically enables logprobs.

import {
  moonshotai,
  type MoonshotAILanguageModelOptions,
} from '@ai-sdk/moonshotai';
import { generateText } from 'ai';

const result = await generateText({
  model: moonshotai('moonshot-v1-8k'),
  prompt: 'Reply with one word that means happy.',
  providerOptions: {
    moonshotai: {
      topLogprobs: 3,
    } satisfies MoonshotAILanguageModelOptions,
  },
});

console.log(result.text);
console.log(result.providerMetadata?.moonshotai?.logprobs);

For generateText, log probabilities are available at providerMetadata.moonshotai.logprobs. For streamText, the accumulated log probabilities are included in the final finish part under the same metadata path.

Moonshot documents the request options and OpenAI Chat Completions compatibility, but does not separately specify the log probability response schema. The provider therefore preserves the OpenAI-compatible content entries, including each token's logprob, nullable bytes, and top_logprobs.

Note

Token probabilities and alternatives can significantly increase response size. Request them only when needed, and avoid forwarding or persistently logging provider metadata unless your application requires it.

Video Input

Kimi K3, Kimi K2.7 Code, Kimi K2.6, and Kimi K2.5 support video input. Pass the video as a file content part with a video media type:

import { moonshotai } from '@ai-sdk/moonshotai';
import { generateText } from 'ai';
import fs from 'node:fs';

const { text } = await generateText({
  model: moonshotai('kimi-k3'),
  messages: [
    {
      role: 'user',
      content: [
        { type: 'text', text: 'Summarize what happens in this video.' },
        {
          type: 'file',
          data: fs.readFileSync('./video.mp4'),
          mediaType: 'video/mp4',
        },
      ],
    },
  ],
});

console.log(text);

You can also pass a URL. Since Moonshot AI does not fetch external URLs, the AI SDK downloads the video and inlines it as base64 before sending:

{
  type: 'file',
  data: new URL('https://example.com/video.mp4'),
  mediaType: 'video/mp4',
}

Note

Supported image media types are image/jpeg, image/png, image/gif, image/webp, image/bmp, image/heic, and image/heif. Supported video media types are video/mp4, video/mpeg, video/mov, video/avi, video/x-flv, video/mpg, video/webm, video/wmv, and video/3gpp. Other image and video media types are rejected before a request is sent.

Note

Moonshot AI recommends videos up to 1080p. For larger videos, or videos you reuse across many requests, upload them with the Moonshot Files API instead. Audio and PDF inputs are not supported by Moonshot AI chat completions.

File References and Text Files

Images and videos uploaded through the Moonshot Files API can be passed as provider references. Prefix the uploaded file ID with ms:// and use an image or video media type:

{
  type: 'file',
  data: {
    type: 'reference',
    reference: {
      moonshotai: 'ms://file-id',
    },
  },
  mediaType: 'image/png',
}

Inline text file data is sent as a native text content part:

{
  type: 'file',
  data: {
    type: 'text',
    text: 'Document contents',
  },
  mediaType: 'text/plain',
}

Predicted Outputs

You can provide static predicted content when much of the expected response is already known. Moonshot AI can use the prediction to accelerate the response while still generating any changed content:

import {
  moonshotai,
  type MoonshotAILanguageModelOptions,
} from '@ai-sdk/moonshotai';
import { streamText } from 'ai';

const source = `export function greet(name: string) {
  return \`Hello, \${name}!\`;
}`;

const result = streamText({
  model: moonshotai('kimi-k3'),
  messages: [
    {
      role: 'user',
      content:
        'Change the function to say "Welcome" instead of "Hello". Respond only with the updated code.',
    },
    { role: 'user', content: source },
  ],
  providerOptions: {
    moonshotai: {
      prediction: {
        type: 'content',
        content: source,
      },
    } satisfies MoonshotAILanguageModelOptions,
  },
});

for await (const textPart of result.textStream) {
  process.stdout.write(textPart);
}

The content can also be an array of text parts:

prediction: {
  type: 'content',
  content: [
    { type: 'text', text: 'First known section' },
    { type: 'text', text: 'Second known section' },
  ],
}

Note

Predicted content is sent to Moonshot AI as part of the request. Avoid including sensitive or proprietary text unless you intend to share it with the provider.

Provider Options

The following optional provider options are available for Moonshot AI language models:

  • strictJsonSchema boolean

    Whether to use strict JSON schema validation for structured outputs. Defaults to true.

  • logprobs boolean

    Whether to return log probabilities for generated tokens. Results are available in providerMetadata.moonshotai.logprobs.

  • topLogprobs number

    Number of most likely tokens to return at each token position. Accepts integer values from 0 through 20 and automatically enables logprobs.

  • reasoningEffort 'low' | 'high' | 'max'

    Reasoning effort for Kimi K3 and Kimi K3.1 Pro. Defaults to 'max'. You can also use the generic reasoning setting. Portable reasoning: 'max' is sent directly; minimal maps to low, while medium and high map to high.

  • prediction { type: 'content'; content: string | Array<{ type: 'text'; text: string }> }

    Supplies static predicted output that can accelerate requests where most of the response is already known.

  • thinking object

    Configuration for Kimi K2.5 and K2.6. Kimi K2.7 accepts only enabled because its thinking cannot be disabled. Kimi K3 and Kimi K3.1 Pro do not accept this field.

    • type 'enabled' | 'disabled'

      Whether to enable thinking mode. For Kimi K2.7 Code, only 'enabled' is accepted.

    • budgetTokens number

      Deprecated. Moonshot Chat Completions does not support thinking budgets. The provider omits this value and returns a warning. It remains accepted for backwards compatibility.

  • reasoningHistory 'disabled' | 'interleaved' | 'preserved'

    Controls preserved reasoning behavior in multi-turn conversations:

    • 'disabled' and 'interleaved' are retained for compatibility. They do not change the request, so the model's server-default behavior applies.
    • 'preserved' maps to thinking.keep: 'all' for Kimi K2.6. Kimi K2.7 and K3 preserve reasoning by default.
  • promptCacheKey string

    Groups similar requests to improve cache hit rates. Typically a session or task id.

  • promptCacheOptions { mode?: 'implicit'; ttl?: '5m' | '1h' }

    Context caching options for Kimi K3 models, sent as prompt_cache_options. ttl selects the cache write tier, and Moonshot uses '5m' when it is omitted. A cached prefix keeps the TTL it was first written with, so a '1h' request that extends a '5m' prefix still writes at '5m'. See Context Caching.

import {
  moonshotai,
  type MoonshotAILanguageModelOptions,
} from '@ai-sdk/moonshotai';
import { generateText } from 'ai';

const { usage, providerMetadata } = await generateText({
  model: moonshotai('kimi-k3.1-pro'),
  system: longReferenceDocument,
  prompt: 'Summarize the key conclusions.',
  providerOptions: {
    moonshotai: {
      promptCacheOptions: { ttl: '1h' },
    } satisfies MoonshotAILanguageModelOptions,
  },
});

console.log(usage.inputTokenDetails.cacheWriteTokens);
console.log(providerMetadata?.moonshotai?.cacheWriteTokens); // { ttl5m, ttl1h }

Chat Response Metadata

Moonshot AI preserves provider-specific response fields in providerMetadata.moonshotai for generated and streamed responses:

  • responseObject: chat.completion or chat.completion.chunk
  • choiceIndex: the selected response choice index
  • messageRole: the response message role, when supplied
  • toolCallTypes: the tool-call type for each returned call
  • cacheWriteTokens: cache write tokens split by TTL (ttl5m and ttl1h), from Moonshot's usage response headers, when supplied

These fields remain in provider metadata because they are specific to Moonshot's Chat Completions response rather than shared AI SDK result fields.

Model Capabilities

The Moonshot V1 series and Kimi K2.5 are unavailable to newly registered users and are scheduled for full platform sunset on August 31, 2026. They remain listed because they are still part of the Chat Completions API model surface, but new applications should use Kimi K3, Kimi K2.7 Code, or Kimi K2.6.

ModelImage InputVideo InputObject GenerationTool UsageTool Streaming
moonshot-v1-auto✗✗✓✓✓
moonshot-v1-8k✗✗✓✓✓
moonshot-v1-32k✗✗✓✓✓
moonshot-v1-128k✗✗✓✓✓
moonshot-v1-8k-vision-preview✓✗✓✓✓
moonshot-v1-32k-vision-preview✓✗✓✓✓
moonshot-v1-128k-vision-preview✓✗✓✓✓
kimi-k2.5✓✓✓✓✓
kimi-k2.6✓✓✓✓✓
kimi-k2.7-code✓✓✓✓✓
kimi-k2.7-code-highspeed✓✓✓✓✓
kimi-k3✓✓✓✓✓
kimi-k3.1-pro✓✓✓✓✓
kimi-k3.1-pro-highspeed✓✓✓✓✓

Note

See the Kimi model list and model parameter reference for current availability and model-specific constraints. You can also pass custom or retired provider model IDs as strings when needed.