# OpenCode (/docs/integrations/opencode)

> 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="opencode" />

OpenCode reaches custom providers through an OpenAI-compatible runtime, which uses Spare's
`/v1/chat/completions`. You add Spare to `opencode.json`, then pick one of two setups:

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

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

* A Spare API key, exported in your shell profile:

  ```bash
  export SPARE_API_KEY="sk-spare-..."
  ```

* For the Router: the CLI, signed in to your Claude model with an Anthropic key or a
  Claude subscription.

Put the settings in your global config, `~/.config/opencode/opencode.json`, or in `opencode.json` at the
root of a project. `{env:...}` reads a value from your environment, so keys stay out of the file.

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

```jsonc title="opencode.json"
{
  "$schema": "https://opencode.ai/config.json",
  "model": "spare/deepseek-v4.1-flash",
  "providers": {
    "spare": {
      "name": "Spare Inference",
      "env": ["SPARE_API_KEY"],
      "package": "@opencode/ai/providers/openai-compatible",
      "settings": {
        "baseURL": "https://api.spareinference.ai/v1"
      },
      "models": {
        "deepseek-v4.1-flash": {
          "name": "DeepSeek V4.1 Flash",
          "capabilities": { "tools": true, "input": ["text"], "output": ["text"] },
          "limit": { "context": 1000000, "output": 384000 }
        }
      }
    }
  }
}
```

* `env` names the environment variable OpenCode reads the key from.
* Set `limit` and `capabilities`. Without them OpenCode assumes a 200K context and image input, which
  this model doesn't take.

### 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).

To run a [routing preset](https://bitrouter.ai/docs/fleet/provider-selection), add a model whose id is `@<name>` under
`models` and pick it in `/models`.

## 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 run `spare start`. Add the CLI as a
provider and pick its `spare/auto` model:

```jsonc title="opencode.json"
{
  "$schema": "https://opencode.ai/config.json",
  "model": "router/spare/auto",
  "providers": {
    "router": {
      "name": "Router (local)",
      "package": "@opencode/ai/providers/openai-compatible",
      "settings": { "baseURL": "http://127.0.0.1:4356/v1" },
      "models": { "spare/auto": { "name": "Auto" } }
    }
  }
}
```

The CLI listens on `127.0.0.1:4356` by default and holds your keys, so this block needs none.

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

Run `/models` and pick **Spare Inference → DeepSeek V4.1 Flash**, or run one prompt from the shell:

```bash
opencode run "Reply with OK"
```

To get a request id for [settlement](https://bitrouter.ai/docs/api/settlement), send a request directly:

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

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

* **Prompt caching is automatic,** so OpenCode's long, repeated system prompt and tool list are billed
  at the cached input price after the first request.
* **Timeouts:** OpenCode waits up to five minutes for response headers. Long batch-style prompts may
  need a higher `settings.headerTimeout` on the provider.