How to Connect Claude Code to Kimi K2.5 and GLM-4.7: Complete Setup Guide
Claude Code kept timing out on me.
I’d type a simple request, wait 30 seconds, and then get slapped with an API timeout error. Living in China, this was my daily reality with the official Claude API. Account bans, slow connections, and that constant anxiety of whether my subscription would survive the week.
Then I discovered Kimi K2.5 and GLM-4.7 - Chinese AI models that work with Claude Code through a simple configuration change.
The Problem with Official Claude API in China
If you’re a developer in China using Claude Code, you probably recognize this pattern:
Error: API request timed out after 30000msOr worse:
Error: Your account has been suspendedThe official Claude API has several issues for Chinese users:
- High latency: Requests often take 10-30 seconds just to connect
- Account instability: Random bans and suspensions
- Cost: At $20/month for Claude Pro, it adds up
- Payment barriers: Getting a subscription requires jumping through hoops
I needed an alternative that:
- Works reliably in China
- Costs less
- Still integrates with Claude Code
The Solution: Anthropic-Compatible Endpoints
Both Kimi K2.5 and GLM-4.7 offer Anthropic-compatible API endpoints. This means you can point Claude Code to their servers instead of Anthropic’s, and everything just works.
┌─────────────────┐ ┌──────────────────┐ ┌─────────────────┐│ Claude Code │────▶│ Anthropic API │────▶│ Claude Model ││ (Your PC) │ │ Compatible API │ │ (Kimi/GLM) │└─────────────────┘ └──────────────────┘ └─────────────────┘ │ │ │ Three environment variables │ └────────────────────────────────────────────────┘The magic happens through three environment variables:
| Variable | Purpose |
|---|---|
ANTHROPIC_BASE_URL | API endpoint URL |
ANTHROPIC_AUTH_TOKEN | Your API key |
ANTHROPIC_MODEL | Model identifier |
Setting Up GLM-4.7
GLM-4.7 from Zhipu AI is a strong all-rounder. Here’s how to set it up:
Step 1: Get Your API Key
Head to bigmodel.cn, register, and grab your API key from the console.
Step 2: Configure Environment Variables
export ANTHROPIC_BASE_URL=https://open.bigmodel.cn/api/anthropicexport ANTHROPIC_AUTH_TOKEN=your_api_key_hereexport ANTHROPIC_MODEL=GLM-4.7Step 3: Test It
claudeIf configured correctly, Claude Code will connect to GLM-4.7 instead of Anthropic’s servers.
Setting Up Kimi K2.5
Kimi K2.5 from Moonshot AI has been dominating OpenRouter’s weekly leaderboard, often sitting at #1. It’s particularly good at coding tasks.
Step 1: Get Your API Key
Visit moonshot.cn and create an account.
Step 2: Configure Environment Variables
export ANTHROPIC_BASE_URL=https://api.moonshot.cn/anthropicexport ANTHROPIC_AUTH_TOKEN=your_api_key_hereexport ANTHROPIC_MODEL=kimi-k2.5Step 3: Restart Claude Code
claudeQuick Model Switching with claude-code-router
Manually setting environment variables gets tedious. I built claude-code-router (ccr) to make switching instant:
# Installpip install claude-code-router
# Switch modelsccr use kimi # Switch to Kimi K2.5ccr use glm # Switch to GLM-4.7ccr use claude # Back to official ClaudeBehind the scenes, it just updates those three environment variables for you.
Performance Comparison
After using both for a month, here’s my honest assessment:
| Aspect | Claude Official | Kimi K2.5 | GLM-4.7 |
|---|---|---|---|
| Connection Speed | Slow/Unstable in China | Fast | Fast |
| Code Quality | Excellent | Very Good | Good |
| Chinese Understanding | Good | Excellent | Excellent |
| Cost (per 1M tokens) | ~$3 | ~$0.5 | ~$0.3 |
| Availability | 95% (with VPN) | 99.9% | 99.9% |
Kimi K2.5 feels closer to Claude’s reasoning capabilities, while GLM-4.7 is more cost-effective for simpler tasks.
Why This Works
Claude Code uses the Anthropic SDK internally, which respects these environment variables:
ANTHROPIC_BASE_URL ──▶ Overrides api.anthropic.comANTHROPIC_AUTH_TOKEN ──▶ Replaces the API keyANTHROPIC_MODEL ──▶ Specifies which model to useBoth Kimi and GLM implemented Anthropic’s API specification, so Claude Code can’t tell the difference. It’s like putting a different engine in your car - the steering wheel still works the same.
Common Pitfalls
Wrong Model Name
# Wrongexport ANTHROPIC_MODEL=glm-4-7 # Will fail
# Correctexport ANTHROPIC_MODEL=GLM-4.7Always check the exact model identifier from the provider’s documentation.
Forgetting to Restart
Environment variables only apply to new sessions. If Claude Code is already running, restart it:
# Exit current session, thenclaudeMissing API Key
# Check your configurationecho $ANTHROPIC_BASE_URLecho $ANTHROPIC_AUTH_TOKENecho $ANTHROPIC_MODELIf any are empty, set them in your shell profile:
# Add to ~/.zshrc or ~/.bashrcexport ANTHROPIC_BASE_URL=https://api.moonshot.cn/anthropicexport ANTHROPIC_AUTH_TOKEN=your_api_key_hereexport ANTHROPIC_MODEL=kimi-k2.5Making It Permanent
Add the configuration to your shell profile:
# ~/.zshrc or ~/.bashrc
# Claude Code with Kimi K2.5export ANTHROPIC_BASE_URL="https://api.moonshot.cn/anthropic"export ANTHROPIC_AUTH_TOKEN="your_api_key_here"export ANTHROPIC_MODEL="kimi-k2.5"Then reload:
source ~/.zshrc # or ~/.bashrcWhen to Use Which Model
I use different models for different tasks:
- Kimi K2.5: Complex reasoning, architecture decisions, debugging tricky issues
- GLM-4.7: Quick questions, simple refactors, Chinese language tasks
- Claude Official: When I need Claude’s specific strengths (rare, but happens)
OpenRouter Alternative
If you want access to multiple models through one API, OpenRouter works too:
export ANTHROPIC_BASE_URL=https://openrouter.ai/api/v1export ANTHROPIC_AUTH_TOKEN=your_openrouter_keyexport ANTHROPIC_MODEL=anthropic/claude-3.5-sonnetThe advantage is you can switch models by just changing ANTHROPIC_MODEL:
export ANTHROPIC_MODEL=google/gemini-2.0-flash # Switch to Geminiexport ANTHROPIC_MODEL=openai/gpt-4o # Switch to GPT-4Summary
In this post, I showed how to connect Claude Code to Kimi K2.5 and GLM-4.7 using three environment variables. The key point is that these models offer comparable performance at a fraction of the cost, with better reliability for users in China.
Final Words + More Resources
My intention with this article was to help others share my knowledge and experience. If you want to contact me, you can contact by email: Email me
Here are also the most important links from this article along with some further resources that will help you in this scope:
- 👨💻 OpenRouter - AI Model Gateway
- 👨💻 Zhipu AI GLM Platform
- 👨💻 Moonshot AI Kimi Platform
- 👨💻 Anthropic API Documentation
Oh, and if you found these resources useful, don’t forget to support me by starring the repo on GitHub!
Comments