Skip to main content
MindStudio
Pricing
Blog About
My Workspace

How to Use GLM 5.2 in Claude Code with OpenRouter: 5-Minute Setup Guide

Set up GLM 5.2 as the backend model for Claude Code using OpenRouter in under 5 minutes. Get frontier-level coding at a fraction of the API cost.

MindStudio Team RSS
How to Use GLM 5.2 in Claude Code with OpenRouter: 5-Minute Setup Guide

Why Swap the Default Model in Claude Code

Claude Code is one of the most capable agentic coding tools available right now. But its default setup ties you to Anthropic’s own models — and those API costs add up fast on heavy workloads.

Here’s the thing most developers don’t know: Claude Code supports custom API endpoints. That means you can point it at OpenRouter and use nearly any frontier model as the backend — including GLM 5.2 from ZhipuAI.

GLM 5.2 is a strong coding-focused model that benchmarks competitively against much more expensive options. Running it through OpenRouter inside Claude Code gives you a familiar interface with a significantly lower per-token cost. This guide walks through the full setup in under five minutes.


What GLM 5.2 Actually Is

GLM 5.2 comes from ZhipuAI, a Beijing-based AI lab that has been building large language models since 2019. The GLM (General Language Model) series has steadily improved across each release, with recent versions placing strongly on coding, math, and reasoning benchmarks.

GLM 5.2 is tuned specifically for technical tasks. It handles:

  • Multi-file code generation — it can reason across a codebase, not just complete single functions
  • Debugging and refactoring — strong instruction-following makes it reliable for targeted edits
  • Tool use and function calling — essential for agentic workflows like Claude Code
  • Long context — supports extended windows suitable for reading large files or full repos
Hermes Crash Course — free 1-hour live workshop
The free Hermes Agent crash courseReserve your spot

The model is available on OpenRouter under ZhipuAI’s model family, which means you get access without creating a separate ZhipuAI account or managing a new API key.


Why Use OpenRouter as the Bridge

OpenRouter is a unified API layer that gives you access to hundreds of models — from Anthropic, OpenAI, Google, Meta, Mistral, ZhipuAI, and others — through a single endpoint and a single API key.

For this setup, OpenRouter serves two purposes:

  1. It speaks Anthropic’s API format. Claude Code is built to talk to Anthropic’s API. OpenRouter can receive those same requests and route them to a different model — transparently.
  2. It handles billing in one place. You load credits once and use them across any model you route through it.

The combination means you get Claude Code’s interface and tooling, with GLM 5.2 doing the actual inference. The cost difference is real: GLM 5.2 on OpenRouter runs at a fraction of Claude Sonnet pricing per million tokens, which matters if you’re using Claude Code for hours a day.


Prerequisites

Before you start, make sure you have:

  • Claude Code installednpm install -g @anthropic-ai/claude-code if you haven’t already
  • An OpenRouter account — free to create at openrouter.ai
  • An OpenRouter API key — generated in your OpenRouter dashboard under “API Keys”
  • Some OpenRouter credits loaded — you can start with a few dollars; GLM 5.2 is inexpensive per token

That’s it. No ZhipuAI account needed. No separate model download. No local GPU required.


Step-by-Step Setup

Step 1: Get Your OpenRouter API Key

Log into your OpenRouter account and navigate to the API Keys section. Create a new key and copy it somewhere safe — you’ll only see it once.

Your key will start with sk-or-.

Step 2: Find the GLM 5.2 Model ID on OpenRouter

OpenRouter identifies models by a provider/model-name format. For ZhipuAI’s GLM 5.2, navigate to OpenRouter’s model library and search for “GLM” or “ZhipuAI” to find the exact current identifier.

It will look something like:

zhipuai/glm-4.5

or a versioned variant like:

zhipuai/glm-z1-flash

Copy the model ID exactly as listed — you’ll use it in the next step.

Step 3: Set the Environment Variables

Claude Code uses two environment variables to determine where API requests go:

  • ANTHROPIC_BASE_URL — the API endpoint
  • ANTHROPIC_API_KEY — the authentication key

Set both in your terminal session:

export ANTHROPIC_BASE_URL=https://openrouter.ai/api/v1
export ANTHROPIC_API_KEY=sk-or-your-key-here

Replace sk-or-your-key-here with the actual key you copied from OpenRouter.

To make these persistent across terminal sessions, add both lines to your ~/.bashrc, ~/.zshrc, or equivalent shell config file:

# OpenRouter for Claude Code
export ANTHROPIC_BASE_URL=https://openrouter.ai/api/v1
export ANTHROPIC_API_KEY=sk-or-your-key-here

Then run source ~/.zshrc (or your equivalent) to apply the changes.

Step 4: Launch Claude Code with the GLM 5.2 Model Flag

Now start Claude Code and specify the model using the --model flag:

claude --model zhipuai/glm-4.5

Replace zhipuai/glm-4.5 with the exact model ID you found in Step 2.

Claude Code will initialize as normal, but the underlying requests will route through OpenRouter to GLM 5.2 instead of Anthropic’s infrastructure.

Step 5: Verify It’s Working

Run a quick sanity check inside Claude Code. Ask it something simple:

What model are you running on?
Wondering what the Hermes hype is about? Free 60-minute primer
The free Hermes Agent crash courseReserve your spot

Claude Code may not always return the underlying model name (it depends on how OpenRouter surfaces that metadata), but you can verify the routing by checking your OpenRouter dashboard under Activity — you should see a request logged there under the GLM model you specified.

If you see activity in OpenRouter and the response comes back correctly, the setup is complete.


Making the Config Permanent (Optional)

If you want to avoid setting environment variables manually every session, Claude Code also supports a settings.json configuration file. You can place it at:

~/.claude/settings.json

The structure looks like this:

{
  "env": {
    "ANTHROPIC_BASE_URL": "https://openrouter.ai/api/v1",
    "ANTHROPIC_API_KEY": "sk-or-your-key-here"
  }
}

This makes OpenRouter the default endpoint any time Claude Code runs, regardless of your shell environment. You can still override the model per-session using the --model flag if you want to switch back to a native Claude model.


Troubleshooting Common Issues

”Authentication failed” or 401 error

This almost always means the API key is wrong or wasn’t exported correctly. Double-check:

  • The key starts with sk-or-
  • There are no spaces or extra characters in the key
  • You ran source after editing your shell config (if you went that route)

“Model not found” error

The model ID you specified doesn’t match what OpenRouter expects. Go back to OpenRouter’s model library, search for GLM, and copy the identifier exactly as it appears. Model IDs are case-sensitive.

Requests showing up in Anthropic dashboard, not OpenRouter

Your ANTHROPIC_BASE_URL wasn’t set correctly, or a new terminal session reset it. Run echo $ANTHROPIC_BASE_URL to confirm it’s pointing to https://openrouter.ai/api/v1. If it’s empty, re-export the variable.

Slow responses or timeouts

OpenRouter routes to ZhipuAI’s infrastructure, which adds a small amount of latency compared to a direct API call. For most coding tasks this is negligible. If you’re experiencing consistent timeouts, check OpenRouter’s status page — model providers occasionally have degraded availability.

Tool use not working as expected

Claude Code relies heavily on structured tool calling. GLM 5.2 supports function calling, but the exact formatting behavior can differ slightly from Claude models. If you notice tool calls failing, try a prompt that explicitly describes the task step by step — this tends to improve reliability across models.


Cost Comparison: What You’re Actually Saving

To give this some context, here’s a rough comparison of model pricing at the time of writing:

ModelInput (per 1M tokens)Output (per 1M tokens)
Claude Sonnet 3.7~$3.00~$15.00
Claude Haiku 3.5~$0.80~$4.00
GLM 5.2 (via OpenRouter)~$0.10–$0.50~$0.50–$1.50

If you’re running Claude Code for a few hours a day on a medium-complexity project, the difference compounds quickly. For teams running multiple developers, it’s even more significant.

The tradeoff is quality. GLM 5.2 is strong, but it’s not Claude Sonnet on every task. For pure coding work — writing functions, refactoring, generating boilerplate — the gap is smaller than you might expect. For nuanced reasoning or complex multi-step architecture decisions, Claude’s native models may still have an edge.

A practical approach: use GLM 5.2 for the bulk of your work, and switch back to a Claude model when you hit tasks that need maximum reasoning depth.


Where MindStudio Fits Into This Kind of Stack

Plans first. Then code.

PROJECTYOUR APP
SCREENS12
DB TABLES6
BUILT BYREMY
1280 px · TYP.
yourapp.msagent.ai
A · UI · FRONT END

Remy writes the spec, manages the build, and ships the app.

If you’re already thinking about model routing and cost optimization, you’re likely also thinking about how to build AI-powered workflows that don’t break the bank.

MindStudio is a no-code platform for building and deploying AI agents — and it comes with 200+ models built in, including models from ZhipuAI and the rest of the OpenRouter-accessible catalog. You don’t need to manage API keys, configure endpoints, or handle rate limiting yourself. You pick a model from the library and use it.

If you want to build an agent that uses GLM 5.2 for code-related tasks and then routes to a cheaper model for summarization or formatting steps, you can set that up visually in MindStudio without writing any routing logic. The average workflow takes 15 minutes to an hour to build.

This is especially useful if you’re building AI tools for a team — you can wrap your model access in a proper agent interface, connect it to GitHub or Slack, and deploy it so others can use it without touching any API config.

You can try MindStudio free at mindstudio.ai. It’s also worth reading about how MindStudio handles multi-model workflows if you’re exploring cost-optimized AI architectures.


FAQ

Can Claude Code use any model on OpenRouter?

Technically, Claude Code will attempt to route requests to any model you specify via the --model flag. In practice, you want a model that supports Anthropic-compatible tool calling and structured outputs, since Claude Code relies on these heavily. GLM 5.2, along with most major frontier models on OpenRouter, supports this. Smaller or older models may not handle tool use reliably.

Does this violate Anthropic’s terms of service?

No. Claude Code is designed to support custom API endpoints — it’s a documented feature, not a workaround. Anthropic explicitly supports using Claude Code with Bedrock, Vertex AI, and compatible third-party APIs. OpenRouter is a legitimate API provider, and this configuration is a supported use case.

Will Claude Code’s features still work with a non-Anthropic model?

Most features work. File reading, code editing, bash execution, and multi-turn conversations all function normally. Some features that depend on model-specific behavior (like certain extended thinking modes tied to Claude’s architecture) may not be available. For standard coding workflows, you won’t notice a meaningful difference in the tooling.

What’s the best OpenRouter model for coding tasks?

GLM 5.2 performs well for coding. Other strong options on OpenRouter for coding-heavy use include DeepSeek Coder V2, Qwen 2.5 Coder, and Mistral’s coding variants. The best choice depends on your specific workloads — it’s worth running a few test sessions with different models to see which feels right for your projects. OpenRouter’s pricing page makes it easy to compare costs before committing.

Can I switch between models mid-session in Claude Code?

Not mid-session — the model is set when you start Claude Code and stays fixed for that session. To switch, exit Claude Code and restart with a different --model flag. If you’re using a settings.json file for defaults, you can still override that at launch time with the flag.

Is there a way to do this without OpenRouter?

Yes. Other API providers that support the Anthropic API format or proxy layer can work similarly. But OpenRouter is the most straightforward option because it aggregates dozens of model providers, handles model availability failover, and has clear per-model pricing. It’s the simplest path to GLM 5.2 inside Claude Code for most developers.


Key Takeaways

  • Claude Code supports custom API endpoints via ANTHROPIC_BASE_URL and ANTHROPIC_API_KEY environment variables
  • Setting both to OpenRouter’s values lets you use GLM 5.2 — or any OpenRouter model — as the Claude Code backend
  • GLM 5.2 delivers strong coding performance at a fraction of native Claude Sonnet pricing
  • The setup takes under five minutes with no additional accounts or local installs required
  • For teams or more complex multi-model workflows, MindStudio offers a no-code platform with the same model access baked in

If you’re building more sophisticated AI workflows beyond a local coding assistant — agents that connect to databases, send messages, or run on a schedule — MindStudio is worth a look. It brings the same model flexibility to a full agent-building platform, without the infrastructure overhead.

Presented by MindStudio

No spam. Unsubscribe anytime.