$Run GLM Models Through Claude Code with a Single Command
A one-line shell function to run Zhipu AI's latest GLM-5.1, GLM-5-Turbo, and GLM-4.7 models through Claude Code — keep the TUI you love, swap the model underneath.
A one-line shell function to run Zhipu AI's GLM models through Claude Code's CLI — keep the TUI you love, swap the model underneath.
If you've been wanting to try out Zhipu AI's GLM models but don't want to leave the comfort of Claude Code's interface, here's a dead-simple setup that gets you there in one line.
The One-Liner
Paste this into your terminal and you're done:
echo 'glm(){ ANTHROPIC_BASE_URL="https://api.z.ai/api/anthropic" ANTHROPIC_AUTH_TOKEN="YOUR_API_KEY" ANTHROPIC_DEFAULT_SONNET_MODEL="glm-5.1" ANTHROPIC_DEFAULT_OPUS_MODEL="glm-5-turbo" ANTHROPIC_DEFAULT_HAIKU_MODEL="glm-4.7" API_TIMEOUT_MS="3000000" claude "$@"; }' >> ~/.zshrc && source ~/.zshrc
Replace YOUR_API_KEY with your actual Zhipu AI API key.
Already have the old version?
If you set this up before with the older models (glm-4.7 / glm-4.5-air), run this instead — it removes the old function first, then adds the updated one:
sed -i '' '/^glm()/d' ~/.zshrc && echo 'glm(){ ANTHROPIC_BASE_URL="https://api.z.ai/api/anthropic" ANTHROPIC_AUTH_TOKEN="YOUR_API_KEY" ANTHROPIC_DEFAULT_SONNET_MODEL="glm-5.1" ANTHROPIC_DEFAULT_OPUS_MODEL="glm-5-turbo" ANTHROPIC_DEFAULT_HAIKU_MODEL="glm-4.7" API_TIMEOUT_MS="3000000" claude "$@"; }' >> ~/.zshrc && source ~/.zshrc
You can grab your existing API key before overwriting by running grep 'glm()' ~/.zshrc.
What This Does
It creates a shell function called glm in your .zshrc that wraps the claude CLI with a few overridden environment variables:
ANTHROPIC_BASE_URL— Points requests to Zhipu AI's Anthropic-compatible endpoint instead of Anthropic's servers.ANTHROPIC_AUTH_TOKEN— Your Zhipu API key for authentication.ANTHROPIC_DEFAULT_SONNET_MODEL— Mapped toglm-5.1, their most capable model. Claude Code uses Sonnet for the majority of tasks, so your best model goes here.ANTHROPIC_DEFAULT_OPUS_MODEL— Mapped toglm-5-turbo, a fast and powerful model used for complex reasoning tasks.ANTHROPIC_DEFAULT_HAIKU_MODEL— Mapped toglm-4.7, the reliable workhorse for lightweight tasks like file summaries and commit messages. It also has no quota multiplier, keeping your usage efficient.API_TIMEOUT_MS— Set to 3,000,000ms (50 minutes) because GLM responses can take longer than Anthropic's defaults expect."$@"— Passes through any flags you give to theglmcommand directly toclaude.
Model Mapping Rationale
| Claude Code Slot | GLM Model | Why |
|---|---|---|
| Sonnet (default) | glm-5.1 | Used for most tasks. Best model = best default. |
| Opus | glm-5-turbo | Complex reasoning. Fast and powerful. |
| Haiku | glm-4.7 | Lightweight tasks. No quota multiplier — cheap and reliable. |
Quota Awareness
GLM-5.1 and GLM-5-Turbo are premium models that consume quota at 3× during peak hours (14:00–18:00 UTC+8) and 2× during off-peak hours. GLM-4.7 has no multiplier, which is why it sits in the Haiku slot — routine operations won't eat through your limits.
On the Lite plan (~$10/month), you get roughly 80 prompts per 5-hour window. Having GLM-4.7 handle the small stuff means your quota goes further where it matters.
Usage
Once sourced, just use glm anywhere you'd use claude:
# Start an interactive session
glm
# Run with a direct prompt
glm -p "explain this codebase"
# Everything else works the same
glm --resume
Your original claude command stays completely untouched — it still hits Anthropic's API as usual. The glm function is a separate entry point.
Why This Is Nice
Claude Code's TUI is genuinely great — the conversation flow, /compact, session resumption, the tool use loop. This lets you keep all of that while swapping the brain underneath. Useful for comparing model outputs on the same codebase or just experimenting with GLM without context-switching to a different tool.
You get two entry points: claude for Anthropic's models, glm for Zhipu's — same interface, different backends.
Notes
- You'll need a Zhipu AI API key from z.ai.
- If you're on bash instead of zsh, replace
~/.zshrcwith~/.bashrc. - The long timeout is intentional — GLM can be slower on complex agentic loops. Adjust if needed.
- GLM-5.1 is available on all plans (Lite, Pro, Max). GLM-5 (non-Turbo) is only available on Pro and Max.
That's it. One line, no config files, no wrappers. Just a shell function doing shell function things.