Skip to main content
Kimi K2.7 Code is a MoE architecture foundation model with exceptional code and Agent capabilities. We use Claude Code, VS Code & Cline/RooCode as examples to illustrate how to use the Kimi K2.7 Code series models.

Usage Notes

When using large models for code generation, due to the randomness and complexity of the model, multiple attempts may be required to generate code that meets expectations. Programming tools will automatically perform multiple rounds of retries and calls, which may lead to rapid token usage growth. To better control costs and improve the user experience, we recommend you pay attention to the following points:
  • Budget Control
    • Set Daily Spending Limit: Before use, please go to Kimi Open Platform Project Settings to configure the β€œProject Daily Spending Budget”. Once the budget limit is reached, the system will automatically reject all API requests under this project (Note: Due to billing delays, the limit may take about 10 minutes to take effect). For setup instructions, please see Organization Management Best Practices
    • Balance Alert Reminder: It is recommended to enable the account balance reminder function. When the account balance falls below the preset amount (default Β₯20), the system will notify you via SMS to recharge in a timely manner.
  • Usage Recommendations
    • Continuous Monitoring: It is recommended to keep monitoring while the programming software is running, and handle abnormal situations promptly to avoid unnecessary resource consumption due to infinite loops or excessive retries.
    • Model Selection: If response speed is not a high priority, you can choose to use the kimi-k2.7-code model.

Obtaining API Key

key

Using Kimi K2.7 Code model in Claude Code

Install Claude Code

If you have already installed Claude Code, you can skip this step

MacOS and Linux

# Install nodejs on MacOS and Linux
curl -fsSL https://fnm.vercel.app/install | bash

# Open a new terminal to make fnm take effect
fnm install 24.3.0
fnm default 24.3.0
fnm use 24.3.0

# Install claude-code
npm install -g @anthropic-ai/claude-code --registry=https://registry.npmmirror.com

# Initialize configuration
node --eval "
    const homeDir = os.homedir(); 
    const filePath = path.join(homeDir, '.claude.json');
    if (fs.existsSync(filePath)) {
        const content = JSON.parse(fs.readFileSync(filePath, 'utf-8'));
        fs.writeFileSync(filePath,JSON.stringify({ ...content, hasCompletedOnboarding: true }, 2), 'utf-8');
    } else {
        fs.writeFileSync(filePath,JSON.stringify({ hasCompletedOnboarding: true }), null, 'utf-8');
    }"

Windows

# Open the PowerShell terminal in Windows Terminal
# Install nodejs on Windows
# Right-click the Windows button and select "Terminal"

# Then execute the following commands in sequence
winget install OpenJS.NodeJS
Set-ExecutionPolicy -Scope CurrentUser RemoteSigned

# Then close the terminal window and open a new one

# Install claude-code
npm install -g @anthropic-ai/claude-code --registry=https://registry.npmmirror.com

# Initialize configuration
node --eval "
    const homeDir = os.homedir(); 
    const filePath = path.join(homeDir, '.claude.json');
    if (fs.existsSync(filePath)) {
        const content = JSON.parse(fs.readFileSync(filePath, 'utf-8'));
        fs.writeFileSync(filePath,JSON.stringify({ ...content, hasCompletedOnboarding: true }, 2), 'utf-8');
    } else {
        fs.writeFileSync(filePath,JSON.stringify({ hasCompletedOnboarding: true }), null, 'utf-8');
    }"

Configure Environment Variables

After completing the installation of Claude Code, please set the environment variables as follows to use the kimi-k2.7-code model and start Claude.

MacOS and Linux

# Start the kimi-k2.7-code model on Linux/macOS
export ANTHROPIC_BASE_URL=https://api.moonshot.ai/anthropic
export ANTHROPIC_AUTH_TOKEN=${YOUR_MOONSHOT_API_KEY}
export ANTHROPIC_MODEL=kimi-k2.7-code
export ANTHROPIC_DEFAULT_OPUS_MODEL=kimi-k2.7-code
export ANTHROPIC_DEFAULT_SONNET_MODEL=kimi-k2.7-code
export ANTHROPIC_DEFAULT_HAIKU_MODEL=kimi-k2.7-code
export CLAUDE_CODE_SUBAGENT_MODEL=kimi-k2.7-code
export ENABLE_TOOL_SEARCH=false
export CLAUDE_CODE_AUTO_COMPACT_WINDOW=262144
claude

Windows

# Start the kimi-k2.7-code model on Windows PowerShell
$env:ANTHROPIC_BASE_URL="https://api.moonshot.ai/anthropic";
$env:ANTHROPIC_AUTH_TOKEN="YOUR_MOONSHOT_API_KEY"
$env:ANTHROPIC_MODEL="kimi-k2.7-code"
$env:ANTHROPIC_DEFAULT_OPUS_MODEL="kimi-k2.7-code"
$env:ANTHROPIC_DEFAULT_SONNET_MODEL="kimi-k2.7-code"
$env:ANTHROPIC_DEFAULT_HAIKU_MODEL="kimi-k2.7-code"
$env:CLAUDE_CODE_SUBAGENT_MODEL="kimi-k2.7-code"
$env:ENABLE_TOOL_SEARCH=false
$env:CLAUDE_CODE_AUTO_COMPACT_WINDOW="262144"
claude

Verify Environment Variables

Enter /status in Claude Code to check the model status: status
  • How to experience the thinking capabilities of kimi-k2.7-code in Claude Code
    • After configuring the model, click the Tab button to switch after entering the Claude Code page. A β€œThinking on” indicator will appear when the switch is successful.
status You can now use Claude Code for development as usual!

Using Kimi K2.7 Code model in Cline

Install Cline

  1. Open VS Code
  2. Click the Extensions icon in the left activity bar (or use the shortcut Ctrl+Shift+X / Cmd+Shift+X)
  3. Enter cline in the search box
  4. Find the Cline extension (usually published by Cline Team)
  5. Click the Install button to install it
  6. After installation, you may need to restart VS Code
cline

Verify Installation

After installation, you can:
  1. See the Cline icon in the left activity bar of VS Code
  2. Or search for β€œCline” related commands through the command palette (Ctrl+Shift+P / Cmd+Shift+P) to verify the installation was successful

Official Recommendation: Configure Moonshot Provider to use Kimi K2.7 Code model

  • Select β€˜Moonshot’ as the API Provider
  • Select β€˜api.moonshot.ai’ as the Moonshot Entrypoint
  • Configure the Moonshot API Key with the Key obtained from the Kimi Open Platform
  • Select β€˜kimi-k2.7-code’ as the Model
  • Check β€˜Disable browser tool usage’ under Browser
  • Click β€˜Done’ to save the configuration
moonshot_cline

Experience the effects of Kimi K2.7 Code model in cline

  • We asked the Kimi K2.7 Code model to write a Snake game
  • Game effects

Using Kimi K2.7 Code model in RooCode

Install RooCode

  1. Open VS Code
  2. Click the Extensions icon in the left activity bar (or use the shortcut Ctrl+Shift+X / Cmd+Shift+X)
  3. Enter roo code in the search box
  4. Find the Roo Code extension (usually published by RooCode Team)
  5. Click the β€œInstall” button to install it
  6. After installation, you may need to restart VS Code
cline

Verify Installation

After installation, you can:
  1. See the RooCode icon in the left activity bar of VS Code
  2. Or search for β€œRooCode” related commands through the command palette (Ctrl+Shift+P / Cmd+Shift+P) to verify the installation was successful

Official Recommendation: Configure Moonshot Provider to use Kimi K2.7 Code model

  • Select β€˜Moonshot’ as the API Provider
  • Select β€˜api.moonshot.ai’ as the Moonshot Entrypoint
  • Configure the Moonshot API Key with the Key obtained from the Kimi Open Platform
  • Select β€˜kimi-k2.7-code’ as the Model
  • Check β€˜Disable browser tool usage’ under Browser
roocode

Using Kimi K2.7 Code Model in OpenCode

Install OpenCode

The easiest way to install OpenCode is through the install script.
curl -fsSL https://opencode.ai/install | bash
You can also install it with npm:
npm install -g opencode-ai

Configure API key

  1. Run opencode auth login and select Moonshot AI.
    $ opencode auth login
    β”Œ  Add credential
    β”‚
    β—†  Select provider
    β”‚  ● Moonshot AI
    β”‚  ...
    β””
    
  2. Enter your Moonshot AI API key.
    $ opencode auth login
    β”Œ  Add credential
    β”‚
    β—‡  Select provider
    β”‚  Moonshot AI
    β”‚
    β—‡  Enter your API key
    β”‚  _
    β””
    
  3. Run opencode to launch OpenCode.
    $ opencode
    
    Use the /models command to select the Kimi K2.7 Code model.
    /models
    
Let’s get started. For more information, please visit opencode.ai/docs opencode

Direct API Calls to Kimi K2.7 Code model

from openai import OpenAI

client = OpenAI(
    api_key = "$MOONSHOT_API_KEY",
    base_url = "https://api.moonshot.ai/v1",
)

completion = client.chat.completions.create(
    model = "kimi-k2.7-code",
    messages = [
        {"role": "system", "content": "You are Kimi, an artificial intelligence assistant provided by Moonshot AI. You are more proficient in Chinese and English conversations. You will provide users with safe, helpful, and accurate answers. At the same time, you will refuse to answer any questions involving terrorism, racial discrimination, pornography, or violence. Moonshot AI is a proper noun and cannot be translated into other languages."},
        {"role": "user", "content": "Hello, my name is Li Lei, what is 1+1?"}
    ]
)

print(completion.choices[0].message.content)
Where $MOONSHOT_API_KEY needs to be replaced with the API Key you created on the platform. When running the code in the documentation using the OpenAI SDK, ensure that the Python version is at least 3.7.1, the Node.js version is at least 18, and the OpenAI SDK version is not lower than 1.0.0.