BROKE → BUILT EST. 2026 · BUILDING IN PUBLIC · $0 → $4.99
Guides Jun 29, 2026

How to Set Up Claude Code With the GLM API (Cheap Claude Code)

Run Claude Code on cheap GLM models in 5 minutes: install, get a z.ai key, set ANTHROPIC_BASE_URL, and point the CLI at GLM. Mac, Linux & Windows steps.

BROKE → BUILT · GUIDE How to Set Up ClaudeCode With the GLM API(Cheap Claude Code) broke2builtai.com
Short answer

Install Claude Code (npm i -g @anthropic-ai/claude-code), get a z.ai GLM Coding Plan key, then set ANTHROPIC_BASE_URL to z.ai's Anthropic-compatible endpoint and ANTHROPIC_AUTH_TOKEN to your key. The same CLI now runs on cheap GLM models. Unset those vars to switch back.

Or skip the work: Meta-Prompt + System Prompt Architect does it in seconds →

Why run Claude Code on GLM (the broke-builder math)

Claude Code is the best agentic coding tool I’ve used. It’s also metered: every long agent run burns Anthropic inference, and when you’re broke and running it all day, that adds up fast.

Here’s the trick almost nobody tells you: Claude Code doesn’t have to talk to Anthropic. It will point at any Anthropic-compatible endpoint you give it. And z.ai serves the GLM models behind an Anthropic-compatible API — so you can run the exact same Claude Code CLI, with all its tools and agent loop, on GLM models that cost a fraction of premium inference. Same workflow, much smaller bill.

I run a whole content + automation stack this way. This is the setup, start to finish, in about five minutes.

What you need

  • Node.js installed (so you have npm).
  • A z.ai account with a GLM Coding Plan key (the cheap inference — more on that in Step 2).
  • A terminal. Works on macOS, Linux, and Windows.

Step 1 — Install Claude Code

If you don’t already have it, install the CLI globally:

npm install -g @anthropic-ai/claude-code

That’s the real Anthropic Claude Code CLI. We’re not replacing it — we’re just changing where it sends requests.

Step 2 — Get your z.ai API key

Sign up at the z.ai Open Platform (https://z.ai/model-api), then create a key on the API Keys page (https://z.ai/manage-apikey/apikey-list). Copy it somewhere safe — you’ll paste it in the next step.

The cheap-coding part is z.ai’s GLM Coding Plan subscription, which is what makes running Claude Code all day actually affordable. That’s the plan I’m on, and this is a referral link — disclosed, and it helps fund our compute: https://z.ai/subscribe?ic=BWTG6TRYYQ. You don’t have to use it, and the setup below works the same either way.

Step 3 — Point Claude Code at GLM

Claude Code reads its config from ~/.claude/settings.json (that’s your home folder). Open it (create it if it doesn’t exist) and add an env block:

{
  "env": {
    "ANTHROPIC_AUTH_TOKEN": "your_zai_api_key",
    "ANTHROPIC_BASE_URL": "https://api.z.ai/api/anthropic",
    "API_TIMEOUT_MS": "3000000"
  }
}

Three things matter here:

  • ANTHROPIC_BASE_URL is the whole trick — it redirects Claude Code to z.ai’s Anthropic-compatible endpoint instead of Anthropic’s own.
  • ANTHROPIC_AUTH_TOKEN (not ANTHROPIC_API_KEY) is where your z.ai key goes. That variable sets the raw Bearer token sent to the custom endpoint — using the wrong one is the single most common reason people’s setup silently fails.
  • API_TIMEOUT_MS is set high on purpose. Agent runs can be long; a short timeout will kill them mid-task.

On Windows

If you’d rather set environment variables than edit JSON, Command Prompt works too:

setx ANTHROPIC_AUTH_TOKEN your_zai_api_key
setx ANTHROPIC_BASE_URL https://api.z.ai/api/anthropic

Then close and reopen your terminal so the variables load. The settings.json method above is cleaner and survives across shells, so I prefer it — but either works.

Step 4 — Run it

Drop into any project and start Claude Code the normal way:

cd your-project
claude

If it starts and answers a prompt, you’re done — that response just came from GLM, not Anthropic. A quick way to confirm you’re pointed at the right place is to run /status inside Claude Code and check the API base URL.

Which GLM model you actually get

You don’t pick the model by hand — Claude Code asks for “opus” / “sonnet” / “haiku” internally, and z.ai maps those to current GLM models. As of now the defaults map roughly like this:

  • Opus → GLM-4.7
  • Sonnet → GLM-4.7
  • Haiku → GLM-4.5-Air

My advice: don’t hardcode the model mapping. Leave it on the defaults so you automatically ride the latest GLM release instead of getting stuck on an old one when they ship an upgrade.

What cheap Claude Code is actually good for

Lowering the cost-per-run changes how you work, because you stop rationing the tool. Once a run is cheap, you let the agent do the things you’d normally avoid spending premium tokens on:

  • The grind — refactors across a dozen files, renaming things consistently, writing the boring tests, wiring up boilerplate.
  • Throwaway exploration — “spike three different ways to do this and show me,” where you were going to delete two of them anyway.
  • Long agent loops — letting it iterate on a failing build or chase a bug across files without you flinching at the meter, or handing whole chunks of work off to subagents that each burn cheap tokens in parallel.
  • Learning — pointing it at an unfamiliar codebase and asking it to explain, because at this price you can afford to be curious.

That’s genuinely how I use it: GLM carries the volume, and I only reach for premium inference on the rare problem that actually needs the extra reasoning. The empty-wallet version of “use the best tool” is “use the cheap tool for 80% of the work and save your money for the 20% that needs it.”

Troubleshooting the three things that usually break

It errors the moment Claude Code starts. Almost always the auth variable. Make sure your z.ai key is in ANTHROPIC_AUTH_TOKEN, not ANTHROPIC_API_KEY — they are different variables, and Claude Code uses the auth-token one when you point it at a custom endpoint. Double-check the base URL has no trailing slash or typo: https://api.z.ai/api/anthropic.

It worked, then stopped after you opened a new terminal. On Windows, setx only applies to future shells — close and reopen your terminal (or just use the settings.json method, which doesn’t have this problem).

Long runs get cut off mid-task. That’s the timeout. Confirm API_TIMEOUT_MS is set high (the 3000000 above is intentional). Without it, a long agentic task can die partway through and look like a model failure when it’s really just the clock.

Treat the key like a password — don’t paste it into a repo, a shared dotfile, or a screenshot. And to go back to normal Claude at any time, just remove the ANTHROPIC_BASE_URL and ANTHROPIC_AUTH_TOKEN values and Claude Code talks to Anthropic again with your usual login.

Where this fits

Cheap inference is only half the equation. The other half is feeding the model good instructions so it doesn’t waste those cheap tokens flailing — the difference between an agent that one-shots a task and one that burns ten loops guessing what you meant. That’s the muscle worth building next: clear, reusable prompts and system instructions that hold up across runs, whatever model is behind them.

Frequently asked

Is it allowed to use Claude Code with GLM instead of Anthropic?

Yes. Claude Code is designed to read a configurable API endpoint, and z.ai publishes an Anthropic-compatible API for exactly this. You're pointing an official CLI at a compatible provider, which is a supported configuration, not a hack or a bypass.

How much cheaper is GLM than paying Anthropic directly?

It depends on your usage, but the GLM Coding Plan is built for heavy, all-day agentic coding at a flat low cost, which is dramatically cheaper than metered premium inference if you run Claude Code a lot. The exact savings scale with how much you use it.

Will GLM be as good as Claude in Claude Code?

For most everyday coding — refactors, boilerplate, tests, small features — GLM is genuinely strong and you may not notice a difference. On the hardest reasoning-heavy problems, premium Claude models still have an edge. A common setup is GLM for volume work and premium only when you hit something gnarly.

How do I switch back to normal Claude?

Remove or comment out the ANTHROPIC_BASE_URL and ANTHROPIC_AUTH_TOKEN values you added to ~/.claude/settings.json (or unset them on Windows). With those gone, Claude Code talks to Anthropic again using your normal account.

Some links may be referral links, always marked. Full disclosure →