How to Use Different Agents in Claude Code

Claude Code isn't locked to Anthropic's API. With one flag and a JSON file, you can route it through DeepSeek, GLM, Qwen, OpenRouter, or any provider with an Anthropic-compatible endpoint. Here's exactly how.

The Problem

Claude Code is the best agentic coding CLI available. The tool use, the context management, the way it handles multi-file edits — nothing else comes close. But Anthropic's API costs money, and sometimes you want a cheaper model for grunt work. Maybe you want DeepSeek for boilerplate generation. Maybe you want to test how GLM handles your codebase. Maybe you want access to 200+ models through OpenRouter.

The good news: Claude Code already supports this. The bad news: the documentation doesn't make it obvious.


The Mechanism: --settings

Claude Code accepts a --settings flag that points to a JSON file. That file can override environment variables — including the ones that control which API endpoint Claude Code talks to and which model names it uses.

The key variables:

Variable Purpose
ANTHROPIC_BASE_URL The API endpoint. Point this at any Anthropic-compatible provider.
ANTHROPIC_AUTH_TOKEN Your API key for that provider.
ANTHROPIC_DEFAULT_OPUS_MODEL What model to use when Claude Code requests “opus.”
ANTHROPIC_DEFAULT_SONNET_MODEL What model to use when Claude Code requests “sonnet.”
ANTHROPIC_DEFAULT_HAIKU_MODEL What model to use when Claude Code requests “haiku.”

Claude Code internally thinks in tiers: opus (best), sonnet (balanced), haiku (fast/cheap). When you map those tiers to a different provider's models, Claude Code doesn't know the difference. It asks for “opus,” your settings file translates that to whatever model you've configured.


Setting Up DeepSeek

DeepSeek offers an Anthropic-compatible endpoint. Create a settings file at ~/.claude/deepseek.json:

~/.claude/deepseek.json
{
  "env": {
    "ANTHROPIC_BASE_URL": "https://api.deepseek.com/anthropic",
    "ANTHROPIC_AUTH_TOKEN": "your-deepseek-api-key",
    "ANTHROPIC_API_KEY": "your-deepseek-api-key"
  }
}

That's it. DeepSeek's endpoint handles the model routing internally, so you don't need to set the model override variables. Run it:

terminal
claude --settings ~/.claude/deepseek.json --model opus -p "refactor this function"

Setting Up GLM (Z.AI)

GLM has a full model lineup that maps cleanly to Claude Code's tier system. Create ~/.claude/glm.json:

~/.claude/glm.json
{
  "env": {
    "ANTHROPIC_BASE_URL": "https://api.z.ai/api/anthropic",
    "ANTHROPIC_AUTH_TOKEN": "your-zai-api-key",
    "ANTHROPIC_API_KEY": "your-zai-api-key",
    "ANTHROPIC_DEFAULT_OPUS_MODEL": "glm-5.1",
    "ANTHROPIC_DEFAULT_SONNET_MODEL": "glm-5-turbo",
    "ANTHROPIC_DEFAULT_HAIKU_MODEL": "glm-4.5-air",
    "API_TIMEOUT_MS": "3000000"
  }
}

Now when Claude Code asks for opus, it gets GLM-5.1. Ask for sonnet, it gets GLM-5-Turbo. The tier mapping means all of Claude Code's internal logic about when to use which tier still works — it just hits different models.


Setting Up OpenRouter

OpenRouter is the wildcard. It proxies hundreds of models through a single API, and many are free. Create ~/.claude/openrouter.json:

~/.claude/openrouter.json
{
  "env": {
    "ANTHROPIC_BASE_URL": "https://openrouter.ai/api",
    "ANTHROPIC_AUTH_TOKEN": "your-openrouter-key",
    "ANTHROPIC_API_KEY": "your-openrouter-key"
  }
}

With OpenRouter, you pick the model at runtime using an environment variable:

terminal
ANTHROPIC_MODEL="deepseek/deepseek-v4-flash:free" \
  claude --settings ~/.claude/openrouter.json --model sonnet -p "explain this code"

That sends your request through OpenRouter to DeepSeek V4 Flash — for free. You can swap in any model OpenRouter supports.


Setting Up Qwen

Qwen uses its own CLI (qwen) rather than piggybacking on Claude Code, but the pattern is similar — it speaks the OpenAI-compatible protocol:

terminal
qwen --auth-type openai \
  --openai-base-url "https://dashscope-intl.aliyuncs.com/compatible-mode/v1" \
  --openai-api-key "your-dashscope-key" \
  -m qwen3-coder-plus \
  --yolo

The Alias Layer

Nobody wants to type those flags every time. Bash aliases turn this into single commands. Add these to your ~/.bashrc:

~/.bashrc
# DeepSeek — cheap grunt work
alias cheap='claude --bare --dangerously-skip-permissions \
  --settings ~/.claude/deepseek.json --model opus'

# GLM — full tier mapping
alias glm='claude --bare --dangerously-skip-permissions \
  --settings ~/.claude/glm.json --model opus'
alias glm-turbo='claude --bare --dangerously-skip-permissions \
  --settings ~/.claude/glm.json --model sonnet'

# OpenRouter — free models
alias orcheap='ANTHROPIC_MODEL="deepseek/deepseek-v4-flash:free" \
  claude --bare --dangerously-skip-permissions \
  --settings ~/.claude/openrouter.json --model sonnet'

Now your workflow looks like this:

terminal
# Quick boilerplate generation
cheap "generate a Laravel migration for a posts table"

# Full-power GLM for complex work
glm "refactor the authentication middleware to support API tokens"

# Free model for simple questions
orcheap "what does this regex do: /^[a-z]+\d{2,4}$/"

The Flags That Matter

--bare — Strips Claude Code down to just input/output. No interactive UI, no file trees, no spinners. This is what you want when piping output or running from scripts.

--dangerously-skip-permissions — Claude Code normally asks before editing files, running commands, etc. For automated/scripted use where you've already decided to trust the output, this skips those prompts. Use it when you know what you're doing.

-p "prompt" — Passes a one-shot prompt instead of entering interactive mode. Combined with --bare, this makes Claude Code behave like a powerful command-line tool rather than a chat interface.


When to Use What

This isn't about replacing Claude with cheaper models. It's about having the right tool for the job:

Provider Best For Cost
Anthropic Complex multi-file refactors, architecture decisions, anything where you need the best reasoning $$$$
DeepSeek Boilerplate, migrations, test generation, repetitive transformations $
GLM General coding tasks with tier-based cost control (5.1 / Turbo / Air) $$
OpenRouter Quick questions, code explanations, simple generations Free
Qwen Specialized coding tasks (qwen3-coder-plus competes on code benchmarks) $

The Agentic Angle

This matters most when you're running agents. A TurnKey Agentic dispatch engine can route different task types to different providers automatically — send the expensive reasoning work to Claude, the bulk generation to DeepSeek, and the simple lookups to a free OpenRouter model. Your AI bill drops by 60–80% with zero quality loss on the work that matters.

The settings file pattern means each agent in your fleet can have its own provider configuration. Same CLI, same tool ecosystem, different backends. That's the whole point: Claude Code is the interface. The model behind it is a configuration decision, not a technology choice.


Skip the Manual Setup — Paste This Prompt

Everything above is explained so you understand what's happening. But if you just want it done, paste the prompt below into Claude Code or Codex. It will walk you through the whole setup interactively — ask which providers you want, collect your API keys, create the settings files, write the aliases, and test that everything works.

Copy & paste into Claude Code or Codex
I want to set up Claude Code to route through multiple AI providers so I can use cheaper or free models for different tasks. Walk me through the full setup interactively.

Here's what I need you to do:

## 1. Ask me which providers I want

Available providers:
- **DeepSeek** — cheap, Anthropic-compatible endpoint at `https://api.deepseek.com/anthropic`
- **GLM (Z.AI)** — full tier mapping (opus→GLM-5.1, sonnet→GLM-5-Turbo, haiku→GLM-4.5-Air), endpoint `https://api.z.ai/api/anthropic`
- **OpenRouter** — proxy to 200+ models (many free), endpoint `https://openrouter.ai/api`
- **Qwen** — uses its own CLI, OpenAI-compatible via DashScope

Ask me which ones I want to set up. I might want all of them or just one.

## 2. Collect my API keys

For each provider I chose, ask me for my API key. Tell me where to get one if I don't have it:
- DeepSeek: https://platform.deepseek.com/api_keys
- GLM/Z.AI: https://open.z.ai
- OpenRouter: https://openrouter.ai/keys
- Qwen/DashScope: https://dashscope.console.aliyun.com

## 3. Create the settings files

For each provider, create a JSON settings file at `~/.claude/.json` with this structure:

```json
{
  "env": {
    "ANTHROPIC_BASE_URL": "",
    "ANTHROPIC_AUTH_TOKEN": "",
    "ANTHROPIC_API_KEY": ""
  }
}
```

For GLM, also include the model tier mappings:
- `ANTHROPIC_DEFAULT_OPUS_MODEL`: `glm-5.1`
- `ANTHROPIC_DEFAULT_SONNET_MODEL`: `glm-5-turbo`
- `ANTHROPIC_DEFAULT_HAIKU_MODEL`: `glm-4.5-air`
- `API_TIMEOUT_MS`: `3000000`

## 4. Create bash aliases

Detect whether I'm using `~/.bashrc` or `~/.zshrc`. Add aliases for each provider I set up:

```bash
# DeepSeek — cheap grunt work
alias cheap='claude --bare --dangerously-skip-permissions --settings ~/.claude/deepseek.json --model opus'

# GLM — full tier mapping
alias glm='claude --bare --dangerously-skip-permissions --settings ~/.claude/glm.json --model opus'
alias glm-turbo='claude --bare --dangerously-skip-permissions --settings ~/.claude/glm.json --model sonnet'

# OpenRouter — free models (default: DeepSeek V4 Flash free)
alias orcheap='ANTHROPIC_MODEL="deepseek/deepseek-v4-flash:free" claude --bare --dangerously-skip-permissions --settings ~/.claude/openrouter.json --model sonnet'
```

For Qwen (uses its own CLI, not Claude Code):
```bash
alias qwen-plus='qwen --auth-type openai --openai-base-url "https://dashscope-intl.aliyuncs.com/compatible-mode/v1" --openai-api-key "" -m qwen3-coder-plus --yolo'
```

## 5. Source and test

After creating everything:
1. Source the shell config so aliases are available immediately
2. Run a quick test with each alias: send a simple prompt like "respond with OK" to verify the connection works
3. Report which providers are working and which failed

## Important
- Don't overwrite existing settings files without asking me first
- Don't overwrite existing aliases — append new ones
- Show me what you're about to create before writing anything
- If a test fails, help me debug it

That's it. Five minutes and you'll have cheap, glm, and orcheap aliases ready to go. Total setup time: one paste.