# Claude Code (/docs/integrations/claude-code)

> For AI agents, the [documentation index](https://bitrouter.ai/llms.txt) lists all pages. Append .md to a documentation URL to read its Markdown.

<IntegrationHeader id="claude-code" />

Claude Code talks to the Anthropic Messages API, which Spare serves at `/v1/messages`. You point it at
Spare with two environment variables, then pick one of two setups:

* **[Run on DeepSeek V4.1 Flash](https://bitrouter.ai/docs/integrations/claude-code#run-on-deepseek-v41-flash).** Every request goes to Spare, at the
  live price.
* **[Keep Claude, add the Router](https://bitrouter.ai/docs/integrations/claude-code#keep-claude-add-the-router).** The CLI's [Cost routing](https://bitrouter.ai/docs/fleet/cost-routing) keeps
  hard turns on your Claude model and sends the rest to Spare.

## Before you start \[#before-you-start]

* A Spare API key from the console.
* For the Router: the CLI, signed in to your Claude model with an Anthropic key or a
  Claude Pro or Max subscription.

Put the settings below in the `env` block of `~/.claude/settings.json`, so they apply in every project
and to background agents. Don't put keys in a project's `.claude/settings.json`: that file is committed.

## Run on DeepSeek V4.1 Flash \[#run-on-deepseek-v41-flash]

```json title="~/.claude/settings.json"
{
  "env": {
    "ANTHROPIC_BASE_URL": "https://api.spareinference.ai",
    "ANTHROPIC_AUTH_TOKEN": "sk-spare-...",
    "ANTHROPIC_MODEL": "deepseek-v4.1-flash",
    "ANTHROPIC_DEFAULT_OPUS_MODEL": "deepseek-v4.1-flash",
    "ANTHROPIC_DEFAULT_SONNET_MODEL": "deepseek-v4.1-flash",
    "ANTHROPIC_DEFAULT_HAIKU_MODEL": "deepseek-v4.1-flash"
  }
}
```

`ANTHROPIC_MODEL` sets the main model. The three `DEFAULT_*` variables cover the model aliases Claude
Code uses for subagents and background tasks, so nothing falls through to a Claude model.

### Fallbacks \[#fallbacks]

If a provider fails, the request moves to the next provider serving DeepSeek V4.1 Flash on its own. To fall
back to Claude instead of failing, chain the two models in the CLI; see
[Fall back to a different model](https://bitrouter.ai/docs/fleet/fallbacks).

### Use a routing preset \[#use-a-routing-preset]

Set `ANTHROPIC_MODEL` to `@<name>` to run a [routing preset](https://bitrouter.ai/docs/fleet/provider-selection), so you can
change the model or providers behind Claude Code from the console without touching its settings.

## Keep Claude, add the Router \[#keep-claude-add-the-router]

Spare's API runs the model you name and doesn't route between models; the Router runs in the CLI on your machine. Set it up once, following [Cost routing](https://bitrouter.ai/docs/fleet/cost-routing#set-up), with your Claude model as
the strong route and `deepseek-v4.1-flash` as the economy route. Then start Claude Code through the CLI:

```bash
spare claude --model spare/auto
```

Claude Code keeps its interface and tools. `spare requests` shows which model served each turn.

## Check it works \[#check-it-works]

Start `claude` and run `/status`. The **Anthropic base URL** line should show
`https://api.spareinference.ai`, and the auth line should name `ANTHROPIC_AUTH_TOKEN`.

To get a request id you can look up, send one from the same shell with your key exported as `SPARE_API_KEY`:

```bash
curl -si https://api.spareinference.ai/v1/messages \
  -H "Authorization: Bearer $SPARE_API_KEY" \
  -H "anthropic-version: 2023-06-01" \
  -H "content-type: application/json" \
  -d '{"model":"deepseek-v4.1-flash","max_tokens":16,"messages":[{"role":"user","content":"Reply with OK"}]}' \
  | grep -i '^x-bitrouter-request-id'
```

Look the id up with [settlement](https://bitrouter.ai/docs/api/settlement), or find the request on the console's **Activity** page.

## Good to know \[#good-to-know]

* **Anthropic doesn't support Claude Code with non-Claude models.** It works through Spare's Messages
  API, but features that depend on Anthropic's servers, like web search and server-side tools, aren't
  available on DeepSeek V4.1 Flash.
* **Remote Control and voice dictation turn off** while `ANTHROPIC_AUTH_TOKEN` is set, because they
  need a claude.ai login. Unset it to use them.
* **Prompt caching is automatic.** Claude Code's `cache_control` markers are accepted and ignored.
* **The desktop app** reads gateway settings from **Developer → Configure Third-Party Inference**, not
  from `settings.json`. The VS Code extension reads them from `claudeCode.environmentVariables`.