🤖 AI Tools
· 3 min read

How to Use GLM-5.1 with Claude Code — Complete Setup Guide


GLM-5.1 provides an Anthropic-compatible API, which means you can use it as a drop-in replacement for Claude in Claude Code. You get 94.6% of Claude Opus 4.6’s coding performance at a fraction of the cost.

Here’s how to set it up.

Prerequisites

  • Claude Code installed (npm install -g @anthropic-ai/claude-code)
  • A GLM API key from either:

The GLM Coding Plan gives you direct access to GLM-5.1 through Z.ai’s Anthropic-compatible endpoint. All tiers (Max, Pro, Lite) support GLM-5.1.

Step 1: Get your API key

Sign up at z.ai and subscribe to any Coding Plan tier. Copy your API key from the dashboard.

Step 2: Configure Claude Code

Set the environment variables to point Claude Code at Z.ai’s endpoint:

export ANTHROPIC_BASE_URL="https://api.z.ai/v1"
export ANTHROPIC_API_KEY="your-glm-api-key"

Add these to your shell profile (~/.bashrc, ~/.zshrc) to make them permanent:

echo 'export ANTHROPIC_BASE_URL="https://api.z.ai/v1"' >> ~/.zshrc
echo 'export ANTHROPIC_API_KEY="your-glm-api-key"' >> ~/.zshrc
source ~/.zshrc

Step 3: Launch Claude Code

claude

Claude Code will now route all requests through GLM-5.1 instead of Claude. The interface, commands, and workflow remain identical.

Option 2: OpenRouter

If you prefer pay-per-token pricing without a subscription:

export ANTHROPIC_BASE_URL="https://openrouter.ai/api/v1"
export ANTHROPIC_API_KEY="your-openrouter-key"

Note: OpenRouter pricing may differ from Z.ai’s direct pricing. Check openrouter.ai/z-ai/glm-5 for current rates.

Option 3: Self-hosted GLM-5.1

If you’re running GLM-5.1 on your own infrastructure via vLLM:

export ANTHROPIC_BASE_URL="http://your-server:8000/v1"
export ANTHROPIC_API_KEY="dummy"  # vLLM doesn't need a real key

This gives you zero per-token costs but requires significant hardware. See our how to run GLM-5.1 locally guide.

Switching between GLM-5.1 and Claude

You’ll probably want to switch between models depending on the task. Create shell aliases:

# Add to ~/.zshrc or ~/.bashrc
alias claude-glm='ANTHROPIC_BASE_URL="https://api.z.ai/v1" ANTHROPIC_API_KEY="your-glm-key" claude'
alias claude-opus='ANTHROPIC_BASE_URL="" ANTHROPIC_API_KEY="your-anthropic-key" claude'

Now you can run claude-glm for cost-effective sessions and claude-opus when you need Claude’s full capabilities.

Performance comparison in Claude Code

Based on Z.ai’s benchmarks using Claude Code as the evaluation harness:

Task typeGLM-5.1Claude Opus 4.6
Multi-file refactors94.6% relative100% (baseline)
Bug fixesStrongSlightly better
New feature implementationComparableComparable
Long autonomous sessionsBetter (8hr)Good (shorter)
Code explanationGoodBetter

The 5.4% gap is most noticeable on complex reasoning tasks where Claude’s careful analysis shines. For routine coding work — implementing features, fixing bugs, writing tests — the difference is hard to notice.

Cost savings

Rough comparison for a developer doing 4 hours of AI-assisted coding per day:

SetupMonthly cost
Claude Pro subscription$20
Claude API (heavy use)$100-500+
GLM Coding Plan (Lite)$3
GLM Coding Plan (Pro)$10
Self-hosted GLM-5.1Hardware only

The savings are substantial, especially for teams. A 10-person team switching from Claude API to GLM Coding Plan could save thousands per month.

Tips for best results

  1. Use --read for context files — Same as with Claude, mark reference files as read-only to save tokens
  2. Keep sessions focused — GLM-5.1 excels at long sessions, but give it clear goals
  3. Monitor token usage — Even at $3/month, the Lite plan has limits
  4. Test on your codebase — The 94.6% benchmark is an average; your mileage may vary depending on language and complexity

Troubleshooting

“Model not found” error: Make sure your Coding Plan tier supports GLM-5.1. All current tiers (Max, Pro, Lite) should work.

Slow responses: Z.ai’s servers are primarily in China. If latency is an issue, try OpenRouter which may have closer endpoints.

Formatting differences: GLM-5.1’s output style differs slightly from Claude’s. You may notice different code comment styles or explanation formats. This doesn’t affect functionality.

Related: GLM-5.1 Complete Guide · GLM-5.1 vs Claude vs GPT-5 · Claude Code vs Codex CLI vs Gemini CLI