melwyn.class
← All playbooks
Free
14 minUpdated 2026-06-30

Run Claude Code on open-source models via OpenRouter

Point the Claude Code harness at GLM and other open models through OpenRouter, step by step, and know when it is worth it.

The outcome

By the end you'll have the Claude Code harness driving an open model through OpenRouter, with the trade-offs mapped so you know which work to keep on Claude.

  • Claude Code installed (the terminal CLI, or the VS Code extension)
  • An OpenRouter account with a few dollars of credit
  • Ten minutes and a text editor for one config file
I work in:

Claude Code is a harness, not a model. Under the hood it speaks one language, the Anthropic Messages format, and it does not much care who answers. Point it at any endpoint that speaks back and an open model like GLM, Kimi, or DeepSeek drives the whole thing: the edits, the tool calls, the agent loop. The reason to bother is cost. Open models run a fraction of the price, and for routine work the gap in quality is small. The trick that keeps this painless is one config file, so the setup follows you from the plain terminal to the one inside VS Code without redoing it. The VS Code chat sidebar is the one place that needs an extra line, and we cover that too.

In a hurry? Let Claude set it up

Copy this and paste it into Claude Code or claude.ai. It asks about your machine, then walks you through every step below, one at a time. Prefer to do it by hand? The steps are right here.

I want to run Claude Code on an open-source model instead of Claude, to cut my costs. Walk me through it on my own machine, one step at a time, and wait for me to confirm each step before moving on.

Here is the plan:
1. Ask me which operating system I'm on (macOS, Windows, or Linux) and whether I use the terminal or the VS Code extension.
2. Help me create or open the config file ~/.claude/settings.json, with the exact commands for my setup. Remind me not to type the bare file path into the terminal, since that just tries to run it.
3. I want to use OpenRouter. Walk me through getting an API key and adding a few dollars of credit.
4. Help me add this to the file's "env" block: ANTHROPIC_BASE_URL set to https://openrouter.ai/api, ANTHROPIC_AUTH_TOKEN set to my OpenRouter key, ANTHROPIC_API_KEY left empty, the three ANTHROPIC_DEFAULT_OPUS_MODEL / SONNET / HAIKU values all set to z-ai/glm-5.2, CLAUDE_CODE_DISABLE_1M_CONTEXT set to 1 (so Claude Code doesn't tag the model [1m], which OpenRouter rejects), and CLAUDE_CODE_DISABLE_ADAPTIVE_THINKING set to 1 (so the VS Code sidebar doesn't error with redacted_thinking).
5. Tell me to fully quit and reopen Claude Code (it reads the file only at startup), then run /status to confirm it shows z-ai/glm-5.2 with no [1m] and the openrouter.ai endpoint. If it errors, tell me to run /logout once and restart.
6. Show me how to confirm it is really running by checking my OpenRouter activity page, and how to undo everything later by removing the env block.
7. If I'm in VS Code, explain the integrated terminal versus the sidebar and how to make each one use the open model.

Start by asking me about my setup.
  1. 1. Put your config in one file

    All your settings live in one file: ~/.claude/settings.json. Claude Code reads it whenever it runs in a terminal, including the terminal inside VS Code, so you set it up once. The VS Code chat sidebar is the one surface that needs an extra setting, and step 5 handles it. Below is the shape you are filling in: an env block that holds the address, the key, and the model. Every later step only changes the values inside it. Never opened this file? The walkthrough below has the exact commands, or skip the typing and copy the prompt at the top to let Claude do it for you.

    ~/.claude/settings.json. One file, read on every surface. The next steps fill in the values.
    {
      "env": {
        "ANTHROPIC_BASE_URL": "...",
        "ANTHROPIC_AUTH_TOKEN": "...",
        "ANTHROPIC_API_KEY": ""
      }
    }
    Never opened this file? Create and open it, step by step

    The file sits in a hidden .claude folder in your home directory. Run these in a terminal to create it, then open it in an editor. On Windows, use PowerShell.

    macOS or Linux: create the folder and the empty file
    mkdir -p ~/.claude
    touch ~/.claude/settings.json
    Then open it in an editor
    code ~/.claude/settings.json     # opens it in VS Code
    
    # "code" not found? In VS Code press Cmd+Shift+P and run
    # "Shell Command: Install 'code' command in PATH", then try again.
    # No VS Code? Open it in TextEdit instead:
    open -e ~/.claude/settings.json

    Watch out

    Do not type ~/.claude/settings.json on its own and press enter. The shell treats it as a program, tries to run the file, and you get "permission denied". A path always needs a command in front of it, like code or open -e.

    Windows (PowerShell): create it, then open it in Notepad
    mkdir "$HOME\.claude" -Force
    notepad "$HOME\.claude\settings.json"

    Paste the env block above into the file, then save and close. If the file already had something in it, keep it and add the env block inside the outer curly braces. From here, every step only changes the values inside that block.

  2. 2. The fast path: OpenRouter, one key for any model

    OpenRouter is one account and one key that reaches almost every open model, and it speaks Claude Code's language natively, so there is no proxy to run. The whole setup is the block below, pasted into your settings file. It sets the address (ANTHROPIC_BASE_URL), your key (ANTHROPIC_AUTH_TOKEN), a blank ANTHROPIC_API_KEY so an old Anthropic key can't take over, the model in all three slots (GLM 5.2, a strong coder at roughly a tenth of the price), and two safety lines for running a non-Claude model. CLAUDE_CODE_DISABLE_1M_CONTEXT stops Claude Code from tagging the model [1m] for its 1M-context mode, which OpenRouter rejects with "issue with the selected model." CLAUDE_CODE_DISABLE_ADAPTIVE_THINKING turns off the thinking mode that the VS Code sidebar can choke on with a "redacted_thinking" error. Paste it, swap in your own key, and you're done.

    Paste this into the env block from step 1, then replace the key with your own.
    {
      "env": {
        "ANTHROPIC_BASE_URL": "https://openrouter.ai/api",
        "ANTHROPIC_AUTH_TOKEN": "sk-or-your-openrouter-key",
        "ANTHROPIC_API_KEY": "",
        "ANTHROPIC_DEFAULT_OPUS_MODEL": "z-ai/glm-5.2",
        "ANTHROPIC_DEFAULT_SONNET_MODEL": "z-ai/glm-5.2",
        "ANTHROPIC_DEFAULT_HAIKU_MODEL": "z-ai/glm-5.2",
        "CLAUDE_CODE_DISABLE_1M_CONTEXT": "1",
        "CLAUDE_CODE_DISABLE_ADAPTIVE_THINKING": "1"
      }
    }
    Walk me through it, click by click
    1. Open openrouter.ai and create an account (the Sign up chip above).
    2. Add a few dollars of credit under Settings, then Credits. A little goes a long way on open models.
    3. Open the Keys page (the Create an API key chip), click Create Key, and copy it. You only see it once.
    4. Open the file the way step 1 showed it (run code ~/.claude/settings.json, or open -e on a Mac without VS Code). Don't type the bare path on its own.
    5. Paste the block above inside the file, replacing the empty env block if you made one.
    6. Replace sk-or-your-openrouter-key with the key you copied. Keep the quotes around it.
    7. Leave ANTHROPIC_API_KEY as empty quotes. That blank is what stops an old Anthropic key from overriding OpenRouter.
    8. Save the file and close it.
    9. Start Claude Code fresh so it reads the new file: close and reopen it, or run claude again in the terminal.

    About those three model lines: OPUS, SONNET, and HAIKU are the slots Claude Code switches between for heavy, routine, and quick work. Setting all three to z-ai/glm-5.2 means every mode uses GLM. Later you can put a smaller, cheaper model on the HAIKU line so background tasks cost even less.

  3. 3. Run a different model, or mix them

    GLM 5.2 was just the example. Those three model lines are the knob: whatever OpenRouter id you put there is what runs, and OpenRouter carries over 400 models, so you swap in Kimi, DeepSeek, Qwen, or anything else by changing the id. Each id is a vendor/model slug, like z-ai/glm-5.2; you copy the exact string from the model's page on OpenRouter, no guessing. The three lines also map to how hard Claude Code is working: the OPUS line covers its heavy and default work, SONNET its routine work, HAIKU its small background calls. Set all three the same and one model does everything, which is the simplest start. Set them differently and you get a cheap split by hand: a capable model on the OPUS line, a cheaper one on HAIKU, so the constant little calls cost almost nothing. Either way the change is the same two steps, edit the id then fully restart Claude Code. The in-app /model menu won't reliably reach OpenRouter's full list, so the file is the dependable way to choose.

    Mixing models: a capable one for real work, a cheaper one for the background calls. Same three lines, different ids.
    {
      "env": {
        "ANTHROPIC_BASE_URL": "https://openrouter.ai/api",
        "ANTHROPIC_AUTH_TOKEN": "sk-or-your-openrouter-key",
        "ANTHROPIC_API_KEY": "",
        "ANTHROPIC_DEFAULT_OPUS_MODEL": "z-ai/glm-5.2",
        "ANTHROPIC_DEFAULT_SONNET_MODEL": "z-ai/glm-5.2",
        "ANTHROPIC_DEFAULT_HAIKU_MODEL": "openai/gpt-oss-120b:free",
        "CLAUDE_CODE_DISABLE_1M_CONTEXT": "1",
        "CLAUDE_CODE_DISABLE_ADAPTIVE_THINKING": "1"
      }
    }
  4. 4. Check it actually worked

    The first try usually skips this, and then nothing seems different. Two things matter. Claude Code reads the settings file once, when it starts, so saving is not enough: fully quit and reopen it. Then run /status. It should show the openrouter.ai endpoint and the model z-ai/glm-5.2, with no [1m] after it. For hard proof, send one message, then open your OpenRouter activity page and watch the request land.

    If something's off

    Note

    "There's an issue with the selected model (...[1m])": Claude Code added its 1M-context tag to the model name and OpenRouter has no such id. Make sure CLAUDE_CODE_DISABLE_1M_CONTEXT is set to "1" in the env block, then fully restart.

    Note

    You edited the file but nothing changed: settings load only at startup. Quit Claude Code completely and reopen it. In VS Code, restart the integrated terminal too.

    Note

    It keeps showing the Anthropic login or odd model errors: a cached Claude login is fighting your OpenRouter key. Run /logout once, then restart.

    Note

    "Unsupported content type: redacted_thinking" in the VS Code sidebar: a known extension bug with custom endpoints. CLAUDE_CODE_DISABLE_ADAPTIVE_THINKING set to "1" (in the block above) calms it, and the integrated terminal does not hit it at all. The plain terminal is the reliable home for open-model work.

    Note

    Double-check the file is valid JSON: the env block sits inside one outer pair of curly braces, with a comma after every line except the last. A missing brace makes Claude Code ignore the whole file.

    Run /status to confirm the harness is on the open model and the OpenRouter endpoint.
    Claude Code

    /status

    Endpointopenrouter.ai/api
    Modelz-ai/glm-5.2
    AuthOpenRouter key
  5. 5. Use it in VS Code, terminal and sidebar

    VS Code has two ways to run Claude Code, and they read your settings differently. The integrated terminal is just a terminal: open it, type claude, and you're on the open model exactly like the native one, because it runs the same command that reads your file. The sidebar, the chat panel, is the catch. It checks your login before it ever reads ~/.claude/settings.json, so on a custom endpoint it never sees your key and bounces to the Anthropic sign-in screen (or errors with "Unsupported content type: redacted_thinking," a known extension bug). The fix is to give the extension its own copy of the values: open VS Code's own settings with Cmd+Shift+P and "Preferences: Open User Settings (JSON)," add them under claudeCode.environmentVariables as below, then reload with Cmd+Shift+P and "Developer: Reload Window." If you don't need the sidebar, the simpler move is to keep your open-model work in the integrated terminal, which just works.

    To put the VS Code sidebar on the open model too, add this to VS Code's own settings (not the file above), then reload the window.
    // VS Code settings.json, not ~/.claude/settings.json
    {
      "claudeCode.environmentVariables": [
        { "name": "ANTHROPIC_BASE_URL", "value": "https://openrouter.ai/api" },
        { "name": "ANTHROPIC_AUTH_TOKEN", "value": "sk-or-your-openrouter-key" },
        { "name": "ANTHROPIC_API_KEY", "value": "" }
      ]
    }
    The sidebar on a custom endpoint can bounce to this. Use the integrated terminal, or set the extension's env values too.
    Claude Code
    Sign in to Claude
    The extension didn't pick up your custom endpoint.
    Fix: set claudeCode.environmentVariables, or use the terminal.
  6. 6. Want it automatic? Route per task

    One model for everything is a blunt instrument. The sharper setup sends cheap, high-volume work to an open model and keeps the hard thinking on Claude, automatically, deciding which is which per request. That takes a small local router that stays running, so it is its own guide rather than a quick env tweak. Reach for it once a single model starts feeling either too expensive on the easy work or too weak on the hard work.

  7. 7. Know which work to keep on Claude

    Open models are genuinely good now, but the harness was tuned for Claude and a few things slip. Prompt caching may not carry through, so a long session on a cheap model can quietly cost more than you expect. Tool calls and long agent runs are where weaker models wander, redoing work or losing the thread. And your code now leaves Anthropic's walls, so for anything sensitive check where the model is hosted. The honest split: send routine edits, drafting, and high-volume grunt work to the open model, and keep deep multi-file changes and plan-mode work on Claude. One more lever: once you have settled on a single model, you can skip OpenRouter's cut and point Claude Code straight at the provider for less.

  8. 8. Go back to Claude anytime

    This is fully reversible, which is what makes it safe to try. To return to normal Claude, open the same file, remove the env block you added (or delete the whole file if it holds nothing else), save, and restart Claude Code. Run /status and you'll see your Anthropic account again. Nothing about your Claude setup was touched; you only borrowed the file to point it elsewhere.

    Back to Claude: empty the file to this (or delete it), then restart.
    {}

Watch out

Prompt caching often does not survive the trip to a third party, so a long run can cost more than the sticker price suggests. Watch the provider's own dashboard, not a Claude usage meter, and remember your code is leaving Anthropic's walls.

Tip

Set it once in ~/.claude/settings.json and every terminal is covered: the native one and the terminal inside VS Code. Only the VS Code sidebar needs the extra claudeCode.environmentVariables setting on top.