Back to Blog
Software development

How to Use Kimi K2 with Claude Code (Cheaper, Faster, Fully Compatible)

Medianeth Team
July 24, 2025
2 minutes read
How to Use Kimi K2 with Claude Code (Cheaper, Faster, Fully Compatible)

How to Replace Claude 4 with Kimi K2 in Claude Code (Faster, Cheaper, Open Source)

If you're using Claude Code and want something faster, cheaper, and open source, plug in Kimi K2 by Moonshot AI. It performs on par with Claude 4 (sometimes better), costs up to 90% less, and is open source. The swap is simple — just two environment variables.

Here’s the direct integration method.


Step 1: Get Your Kimi K2 API Key

  1. Go to https://platform.moonshot.ai
  2. Click Console (top-right)
  3. Sign in with Google
  4. Go to API Keys
  5. Click Create API Key, name it anything, and set the project to default
  6. Copy the key immediately — you won’t be able to see it again
  7. Fund your account — $10 is enough for most use cases

Step 2: Set Environment Variables

Claude Code uses two environment variables to define the model provider. You’ll point it to Moonshot’s Kimi K2 endpoint:

export ANTHROPIC_BASE_URL="<https://api.moonshot.ai/v1>" export ANTHROPIC_AUTH_TOKEN="your_kimi_api_key_here"

Replace your_kimi_api_key_here with the key you got from Step 1.

Once set, any Claude Code command will route through Kimi K2 instead of Anthropic.


Step 3: Run Claude Code Normally

With the env vars set, run Claude Code like you always do. You’ll see a message about the API base being overridden — that’s your confirmation it’s working.

Example command:

/init

This scans your codebase and generates a CLAUDE.md file at the root. It still looks like Claude is doing the work, but behind the scenes, it’s Kimi K2 answering.


Optional: Add a Shell Function for Quick Access

To make this even faster, drop this into your .zshrc file (Mac):

kimi() { ANTHROPIC_AUTH_TOKEN="your_kimi_api_key_here" ANTHROPIC_BASE_URL="<https://api.moonshot.ai/anthropic/>" claude "$@" }

Then reload your shell:

source ~/.zshrc

Now you can run Claude Code with Kimi K2 by simply typing:

kimi /init

or any other Claude command.


⚠️ Performance Note

Moonshot’s API can get congested. During peak times, expect slower responses — around 60 to 75 seconds for full generations. It's the tradeoff for a cheaper, open-source alternative.


Done

That’s all it takes: one API key, two environment variables (or one CLI function), and you’re running Claude Code powered by Kimi K2.

No extra installs. No breaking changes. Just faster, cheaper output with full Claude Code compatibility.

Let's Build Something Great Together!

Ready to make your online presence shine? I'd love to chat about your project and how we can bring your ideas to life.

Free Consultation 💬