📝 Tutorials
· 7 min read

Use Kimi K2.7 Code with Aider, Claude Code, and OpenCode


You don’t need to use Moonshot’s own CLI to get the most out of Kimi K2.7 Code. Since the API is OpenAI-compatible (and Anthropic-compatible), you can point your favorite coding tools — Aider, Claude Code, OpenCode, and others — directly at K2.7. This gives you a model with 1T parameters, 256K context, and interleaved thinking, all through tools you already know.

In this guide, I’ll show you exactly how to configure each tool, what model IDs to use, and whether the cost savings over Claude Opus or Sonnet make this switch worthwhile.

Why Use K2.7 with Third-Party Tools?

Before we get into configs, here’s why you might want to do this:

  • Cost: K2.7 Code is significantly cheaper per token than Claude Opus 4 or GPT-4.5
  • 256K context: Larger context window than most competing models at this price point
  • Coding focus: K2.7 Code is specifically fine-tuned for programming tasks
  • Preserve thinking: Reasoning chains persist across turns, improving multi-file refactors
  • Open weights: If the API ever becomes unavailable, you can self-host

The tradeoff? These tools weren’t built specifically for K2.7, so you won’t get features like native preserve thinking in Aider. For that, Kimi Code CLI is the better choice. But if you’re already invested in Aider or Claude Code workflows, K2.7 slots in nicely.

Setting Up with Aider

Aider is one of the most popular terminal-based AI coding tools. It supports custom OpenAI-compatible endpoints out of the box.

Environment Variables

export OPENAI_API_BASE=https://api.moonshot.ai/v1
export OPENAI_API_KEY=your-moonshot-api-key

Aider Configuration File

Create or edit ~/.aider.conf.yml:

model: openai/kimi-k2.7-code

openai-api-base: https://api.moonshot.ai/v1
openai-api-key: your-moonshot-api-key

edit-format: diff
auto-commits: true

Running Aider with K2.7

aider --model openai/kimi-k2.7-code \
      --openai-api-base https://api.moonshot.ai/v1 \
      --openai-api-key $MOONSHOT_API_KEY

aider --model openai/kimi-k2.7-code

Aider Model Metadata

For proper token counting and cost tracking, add a custom model definition in ~/.aider.model.metadata.json:

{
  "openai/kimi-k2.7-code": {
    "max_tokens": 8192,
    "max_input_tokens": 262144,
    "max_output_tokens": 8192,
    "input_cost_per_token": 0.000002,
    "output_cost_per_token": 0.000008,
    "litellm_provider": "openai",
    "mode": "chat"
  }
}

Adjust the cost values based on current Moonshot pricing — check platform.moonshot.ai for the latest rates.

Aider Tips for K2.7

  • Use diff edit format — K2.7 handles structured diffs well
  • The 256K context means you can add more files to the chat without hitting limits
  • For large refactors, K2.7’s multi-step reasoning outperforms smaller models
  • Set --map-tokens 2048 to give the model more room for repository maps

Setting Up with Claude Code

Claude Code supports custom API providers through its configuration. You can route it through any OpenAI-compatible endpoint.

Configuration

Edit your Claude Code configuration (typically ~/.claude/config.json or project-level .claude/config.json):

{
  "model": "kimi-k2.7-code",
  "provider": {
    "type": "openai-compatible",
    "baseUrl": "https://api.moonshot.ai/v1",
    "apiKey": "your-moonshot-api-key",
    "model": "kimi-k2.7-code"
  }
}

Using Anthropic-Compatible Endpoint

Since Claude Code naturally speaks the Anthropic message format, you can also use Moonshot’s Anthropic-compatible endpoint:

{
  "model": "kimi-k2.7-code",
  "provider": {
    "type": "anthropic-compatible",
    "baseUrl": "https://api.moonshot.ai/v1/anthropic",
    "apiKey": "your-moonshot-api-key",
    "model": "kimi-k2.7-code"
  }
}

This often works more smoothly since the message formatting matches what Claude Code expects natively.

What You Lose vs Native Claude

Being honest here — routing Claude Code through K2.7 means you lose:

  • Claude’s specific system prompt optimizations in Claude Code
  • Some tool-use behaviors that are tuned to Claude’s response format
  • The extremely well-tested Claude Code ↔ Claude Sonnet/Opus integration

What you gain: lower cost per token, 256K context, and a model that benchmarks competitively on coding tasks. For a detailed comparison of CLI tools, see our Claude Code vs Codex CLI vs Gemini CLI breakdown.

Setting Up with OpenCode

OpenCode supports custom providers through its TOML configuration.

Configuration

Edit ~/.config/opencode/config.toml:

[providers.moonshot]
kind = "openai"
base_url = "https://api.moonshot.ai/v1"
api_key = "your-moonshot-api-key"

[models.kimi-k2-7]
provider = "moonshot"
id = "kimi-k2.7-code"
max_tokens = 8192
context_window = 262144
supports_tool_use = true

Running OpenCode with K2.7

opencode --model kimi-k2-7

OpenCode’s TUI will connect to Moonshot’s API and you’ll have K2.7 Code as your backend model. Tool calling works natively since K2.7 supports the OpenAI tool calling format.

Pointing at a Local Instance

If you’re running K2.7 locally via vLLM or SGLang, just change the base URL:

Aider with Local K2.7

aider --model openai/moonshotai/Kimi-K2.7-Code \
      --openai-api-base http://localhost:8000/v1 \
      --openai-api-key dummy

OpenCode with Local K2.7

[providers.local]
kind = "openai"
base_url = "http://localhost:8000/v1"
api_key = "dummy"

[models.kimi-k2-7-local]
provider = "local"
id = "moonshotai/Kimi-K2.7-Code"
max_tokens = 8192
context_window = 131072
supports_tool_use = true

This gives you full privacy — no code leaves your network — with zero per-token cost.

Cost Comparison

Here’s the real reason most people consider this switch. Prices are approximate and based on mid-2026 rates:

ModelInput (per 1M tokens)Output (per 1M tokens)Context Window
Claude Opus 4$15.00$75.00200K
Claude Sonnet 4$3.00$15.00200K
GPT-4.5$10.00$30.00128K
Kimi K2.7 Code~$2.00~$8.00256K
Kimi K2.7 Code (self-hosted)$0.00$0.00256K

For heavy Aider usage — say 50 coding sessions per day, averaging 10K input and 3K output tokens each — your monthly costs look like:

  • Claude Opus 4: ~$345/month
  • Claude Sonnet 4: ~$69/month
  • Kimi K2.7 Code (API): ~$42/month
  • Kimi K2.7 Code (self-hosted): ~$2,100/month (hardware) but unlimited tokens

The sweet spot for K2.7 API is when you want Opus-level code quality at Sonnet-level prices with a larger context window. The self-hosted route only makes sense above ~5M tokens/day.

Which Tool Works Best with K2.7?

Let me be direct: if your primary goal is the best K2.7 experience, use Kimi Code CLI. It’s built specifically for this model and supports features like preserve thinking and multi-step tool calling natively.

But if you’re already embedded in another workflow:

  • Aider: Best for its git-native workflow. K2.7 handles diff-based editing well. Good for file-by-file refactors.
  • Claude Code: Best if your team is already using it and you want to experiment with K2.7 as a cheaper backend. The Anthropic-compatible endpoint helps.
  • OpenCode: Best for its TUI interface and lightweight footprint. Configuration is cleaner than Aider for custom providers.

For a comprehensive comparison of AI coding CLI tools, see our CLI tools comparison.

Troubleshooting Common Issues

”Model not found” errors

Make sure the model ID is exactly kimi-k2.7-code (for API) or moonshotai/Kimi-K2.7-Code (for local vLLM). The casing and format matter.

Streaming breaks in Aider

Some older Aider versions have issues with non-OpenAI SSE streams. Update to the latest version:

pip install --upgrade aider-chat

Tool calls not working

Verify your tool definitions match OpenAI’s format exactly. K2.7 supports the standard tools parameter — not the deprecated functions parameter. Check our tool calling guide for the correct format.

Slow responses

The Moonshot API can have variable latency depending on load. If you need consistent low latency, consider self-hosting or using K2.7 via OpenRouter as a fallback.

FAQ

Can I use Kimi K2.7 Code as a drop-in replacement for Claude in Aider?

Yes, for most use cases. Set the OpenAI-compatible base URL to Moonshot’s API and use the model ID openai/kimi-k2.7-code. The main difference is that K2.7’s response format may differ slightly from what Aider’s prompts are optimized for, but the diff edit format works well. You won’t get Aider’s Claude-specific optimizations, but the quality is competitive.

Does preserve thinking work through Aider or OpenCode?

Not natively. Preserve thinking requires the extra_body parameter which Aider and OpenCode don’t expose in their standard config. You get regular (non-preserved) thinking at best, or standard responses. For full preserve thinking support, use the Kimi Code CLI or call the API directly.

How does K2.7 Code quality compare to Claude Opus for coding?

On standard benchmarks, K2.7 Code is competitive with Claude Opus 4 for code generation and refactoring. Where Opus still leads is in complex multi-step reasoning over very large codebases and in following nuanced natural language instructions. K2.7 excels at pure code tasks — writing functions, debugging, implementing features from specs. At roughly 5-10x cheaper, it’s excellent value.

Can I switch between K2.7 and Claude dynamically in Aider?

Yes. Aider supports the /model command to switch models mid-session. You can start complex architectural discussions with Claude Opus and switch to K2.7 for implementation:

/model openai/kimi-k2.7-code  # Switch to K2.7 for coding
/model claude-3-opus           # Switch back for design decisions

Is there rate limiting on the Moonshot API?

Yes, the free tier has strict rate limits. Paid plans have higher limits but they exist. If you’re hitting limits frequently, consider self-hosting for unlimited throughput or distributing requests across multiple API keys.

Wrapping Up

Setting up K2.7 with your existing tools is straightforward — it’s a standard OpenAI-compatible API. The 5-minute setup gives you access to a 1T parameter coding model at a fraction of what you’d pay for Claude Opus.

My recommendation: try K2.7 through your current tool first. If you love the model quality but want deeper integration, move to Kimi Code CLI for the full experience. And if the API costs add up, self-hosting is always there as the endgame.