How to Run GLM 5.2 in Claude Code Using OpenRouter: A 5-Minute Setup Guide
You can run GLM 5.2 inside Claude Code's harness via OpenRouter in minutes. This guide covers setup, the anthropic_base_url trick, and web search integration.
Why Run a Different Model Inside Claude Code?
Claude Code is a powerful agentic coding environment. It reads files, runs shell commands, edits code, and reasons across large contexts — all through Anthropic’s CLI. But locking it to a single model limits your options. Running GLM 5.2 in Claude Code via OpenRouter gives you access to Zhipu AI’s capable model family inside the same harness you already know, often at a lower cost per token.
This matters for teams experimenting with model diversity, developers working on multilingual codebases (GLM models handle Chinese-English tasks particularly well), or anyone who just wants to compare outputs from different frontier models without switching tools.
The setup takes about five minutes once you understand the key trick: Claude Code exposes an ANTHROPIC_BASE_URL environment variable that lets you point its API calls at any OpenAI-compatible or Anthropic-compatible endpoint — including OpenRouter.
Here’s exactly how to do it.
What Is GLM 5.2 and Why Use It?
GLM 5.2 is part of Zhipu AI’s General Language Model series. Zhipu AI, spun out of Tsinghua University, has steadily built one of the most competitive open-weight and API-accessible model families outside of the US.
The GLM line has earned a reputation for strong reasoning, solid code generation, and better-than-average bilingual (Chinese/English) performance. On standard benchmarks like MMLU and HumanEval, recent GLM models compete with similarly-sized models from Mistral and Meta.
Key characteristics of GLM 5.2:
- Strong multilingual capabilities — particularly useful for teams working across Chinese and English documentation or codebases
- Competitive context window — handles long files and multi-file reasoning without degradation
- Tool use support — includes function calling, which matters if you want web search or custom integrations
- Available on OpenRouter — accessible through a single API without managing separate keys or accounts
OpenRouter acts as a unified gateway to hundreds of models. Instead of signing up with Zhipu AI directly, you call one endpoint with one key and specify the model ID.
Prerequisites
Before you start, make sure you have the following:
- Claude Code installed — install it globally via
npm install -g @anthropic-ai/claude-code - An OpenRouter account — free to create at openrouter.ai
- Some OpenRouter credits — you can add as little as $5 to get started; GLM 5.2 is priced competitively
- A terminal — the setup involves a few environment variable exports
You do not need an Anthropic API key for this setup. You’re replacing the Anthropic endpoint entirely.
Step-by-Step Setup: Connecting GLM 5.2 to Claude Code via OpenRouter
Step 1: Get Your OpenRouter API Key
- Log in to your OpenRouter account.
- Navigate to Settings → API Keys.
- Click Create Key and give it a name (e.g.,
claude-code-glm). - Copy the key — it starts with
sk-or-v1-....
Keep this key handy. You’ll set it as an environment variable in the next step.
Step 2: Set the Environment Variables
Claude Code reads two key environment variables when it initializes:
ANTHROPIC_API_KEY— the key used for authenticationANTHROPIC_BASE_URL— the base URL for API requests
By overriding both, you redirect all of Claude Code’s model calls to OpenRouter.
In your terminal, run:
export ANTHROPIC_API_KEY="sk-or-v1-your-openrouter-key-here"
export ANTHROPIC_BASE_URL="https://openrouter.ai/api/v1"
For a permanent setup, add these lines to your ~/.bashrc, ~/.zshrc, or equivalent shell config file:
# OpenRouter config for Claude Code
export ANTHROPIC_API_KEY="sk-or-v1-your-openrouter-key-here"
export ANTHROPIC_BASE_URL="https://openrouter.ai/api/v1"
Then reload your shell:
source ~/.zshrc
Step 3: Set the Model
Claude Code needs to know which model to request. Use the --model flag when launching:
claude --model zhipuai/glm-4-plus
Or, if you’re using a newer GLM 5.2 release that has a different slug on OpenRouter, substitute the correct model ID. You can find all available GLM models by searching “glm” in the OpenRouter models directory.
Common GLM model IDs on OpenRouter include:
zhipuai/glm-4-pluszhipuai/glm-4-airzhipuai/glm-z1-airzhipuai/glm-z1-rumination
Check the OpenRouter listing to confirm which ID corresponds to GLM 5.2 at the time you’re reading this — Zhipu AI pushes new versions regularly.
Step 4: Verify the Connection
Run a simple test to confirm everything is wired up correctly:
claude --model zhipuai/glm-4-plus "What model are you?"
If the connection is working, you’ll get a response. The model may or may not accurately self-identify — that’s normal. What matters is that the request goes through without an authentication error or endpoint failure.
If you see a 401 Unauthorized error, double-check that your ANTHROPIC_API_KEY is set to your OpenRouter key, not an Anthropic key. If you see a 404 Not Found, verify the model slug matches exactly what OpenRouter expects.
The ANTHROPIC_BASE_URL Trick Explained
This is the core mechanism worth understanding, because it opens the door to any model on OpenRouter — not just GLM 5.2.
Claude Code is essentially an agentic shell that wraps Anthropic’s API. When you run a command, Claude Code packages your request into an Anthropic-formatted API call and sends it to api.anthropic.com by default. The ANTHROPIC_BASE_URL variable overrides that destination.
OpenRouter accepts Anthropic-formatted requests at https://openrouter.ai/api/v1. It translates them, routes them to the correct upstream model, and returns a response in a format Claude Code can parse.
This means the same trick works for other models too:
# Run Mistral instead
claude --model mistralai/mistral-large --model
# Run Llama 4 instead
claude --model meta-llama/llama-4-maverick
The ANTHROPIC_BASE_URL approach isn’t a hack — Anthropic built it in deliberately so developers could test against local servers and proxies. OpenRouter just happens to be a perfect fit for it.
One thing to be aware of: some Claude Code features depend on Anthropic-specific extensions or behaviors. Prompt caching, for instance, works differently across providers. Tool use generally works fine across OpenRouter, but if you notice unexpected behavior in complex multi-step tasks, the model’s native function-calling implementation may differ slightly from Claude’s.
Adding Web Search to Your GLM 5.2 Setup
Web search is one of the most useful capabilities to layer onto an agentic coding setup. GLM models support tool use, and OpenRouter makes it possible to pass tool definitions in your requests.
There are two main approaches:
Approach 1: Use Claude Code’s Built-in Web Tool (Where Supported)
Claude Code has a built-in web fetch capability for some workflows. With OpenRouter routing, whether this works depends on whether the target model honors the tool call correctly. GLM models generally do support function calling, so you can pass a web search tool definition and the model will attempt to invoke it.
Approach 2: Use a System Prompt with Search Instructions
A simpler workaround is to include instructions in your system prompt that tell the model to request web searches explicitly. Combine this with Claude Code’s ability to run shell commands — you can pipe results from curl or a search API into the model’s context.
For example, in your Claude Code session:
Run a web search for the latest Next.js 15 release notes and summarize the breaking changes.
Claude Code will attempt to use curl or its fetch tools to retrieve that information. With GLM 5.2 handling the reasoning, the results can be surprisingly coherent.
Approach 3: Add a Search Tool via MindStudio’s Agent Skills Plugin
If you want a more robust, production-ready web search capability without wiring it up yourself, MindStudio’s Agent Skills Plugin gives Claude Code access to a searchGoogle() method as a simple function call. The plugin handles auth, rate limiting, and result formatting so you don’t have to. It takes about 10 minutes to set up and works with any model running through Claude Code — including GLM 5.2 via OpenRouter.
Where MindStudio Fits In
Once you’ve got GLM 5.2 running in Claude Code, you might start thinking about the next layer: connecting your agent to real tools and workflows.
Claude Code is excellent for reasoning and code tasks. But the moment you need your agent to send emails, update a CRM, pull from a database, or trigger a business process, you’re looking at significant plumbing work.
That’s where MindStudio becomes relevant. MindStudio’s Agent Skills Plugin (@mindstudio-ai/agent) is an npm SDK that exposes 120+ typed capabilities as simple method calls. When your Claude Code session (or any other agent) needs to call one, it’s as straightforward as:
await agent.sendEmail({ to: "team@company.com", subject: "Build complete", body: summary });
await agent.searchGoogle({ query: "GLM 5.2 benchmark results" });
await agent.runWorkflow({ workflowId: "summarize-pr-changes", input: diffText });
The plugin handles the infrastructure — retries, rate limits, authentication — so the agent focuses on reasoning rather than plumbing.
This makes MindStudio a natural complement to the OpenRouter + Claude Code setup described in this guide. You get model flexibility from OpenRouter, agentic reasoning from Claude Code’s harness, and real-world tool connectivity from MindStudio.
You can try MindStudio free at mindstudio.ai.
Troubleshooting Common Issues
Even with a straightforward setup, a few things can go wrong. Here’s what to check:
The model returns generic “I’m Claude” responses
This is expected behavior for some models. GLM 5.2 may respond to identity questions in different ways. It doesn’t mean the routing is broken — test with a functional task instead, like asking it to write a function.
Authentication errors (401)
Make sure ANTHROPIC_API_KEY is your OpenRouter key, not an Anthropic key. Run echo $ANTHROPIC_API_KEY to confirm the value is set correctly in your current shell session.
Model not found errors (404)
The model slug must match exactly what OpenRouter uses. Check the OpenRouter models page for the exact identifier. Slugs are case-sensitive.
Slow responses or timeouts
GLM models on OpenRouter can occasionally have latency spikes, especially for long-context requests. If you’re consistently hitting timeouts, try a smaller model variant or reduce the context you’re sending.
Tool use not working as expected
Not all models handle Anthropic’s tool-use format identically. If you’re relying on Claude Code’s built-in tools (file reads, shell commands), these should still work fine since they run locally. External tool calls that depend on the model’s function-calling behavior may need adjustment.
Environment variables not persisting
If your variables reset between sessions, confirm they’re in your shell config file (.zshrc, .bashrc, etc.) and that you’ve sourced the file after editing it.
FAQ
Can I use any model from OpenRouter with Claude Code, not just GLM 5.2?
Yes. The ANTHROPIC_BASE_URL trick works with any model that OpenRouter hosts. You just change the --model flag to the appropriate model ID. Mistral, Llama, Qwen, Gemma, and hundreds of others are all accessible this way. The only constraint is that the model needs to support the message format Claude Code uses — which OpenRouter normalizes for you.
Do I still need an Anthropic API key?
No. When you set ANTHROPIC_BASE_URL to OpenRouter’s endpoint and use your OpenRouter API key as ANTHROPIC_API_KEY, Anthropic’s servers are bypassed entirely. You’re billed through OpenRouter, not Anthropic.
How does GLM 5.2 compare to Claude for coding tasks?
GLM 5.2 performs well on code generation and reasoning tasks, particularly for multilingual projects. For pure English-language coding with complex, multi-step reasoning, Claude Sonnet or Claude Opus still tend to outperform GLM on the harder problems. That said, GLM 5.2 is competitive for standard coding tasks and significantly cheaper per token, which matters in high-volume agentic workflows.
Built like a system. Not vibe-coded.
Remy manages the project — every layer architected, not stitched together at the last second.
Is this setup stable enough for production use?
For personal projects and experimentation, yes. For production workloads, you should account for OpenRouter’s uptime (which is generally good but adds a hop), potential model availability changes, and the fact that Claude Code itself is evolving. Test thoroughly before relying on this in critical pipelines.
Can I switch models mid-session?
Not within an active session — the model is set at launch. You’d need to start a new Claude Code session with the new --model flag. Some teams use shell aliases to quickly switch between model configurations.
Does web search work out of the box with GLM 5.2 on OpenRouter?
Not automatically. GLM models support function calling, so you can define a web search tool and pass it in your requests. But Claude Code doesn’t have a native integration with GLM’s tool ecosystem. The most practical approaches are using Claude Code’s shell commands to run searches locally, or using a plugin like MindStudio’s Agent Skills to handle the search infrastructure cleanly.
Key Takeaways
- Claude Code’s
ANTHROPIC_BASE_URLenvironment variable lets you route model calls to OpenRouter, giving you access to GLM 5.2 and hundreds of other models inside the same agentic harness. - The setup takes about five minutes: get an OpenRouter key, export two environment variables, and launch Claude Code with
--model zhipuai/glm-4-plus(or the appropriate GLM 5.2 slug). - GLM 5.2 is a strong choice for multilingual codebases, cost-sensitive workflows, and model comparison experiments.
- Web search integration requires a bit of extra setup — either through Claude Code’s shell tools, tool definitions passed in requests, or a plugin like MindStudio’s Agent Skills.
- You don’t need an Anthropic API key when routing through OpenRouter — your OpenRouter key replaces it entirely.
If you want to extend this setup with real-world tool integrations — search, email, CRM updates, workflow triggers — MindStudio is the fastest way to add those capabilities to any agent, including one running GLM 5.2 through Claude Code.

