OpenCode
Add Spare as a custom provider in OpenCode, or keep Claude and let the CLI's Router send the easy turns to Spare.
/v1/chat/completionsConfigopencode.jsonOpenCode 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. Every request goes to Spare, at the live price.
- Keep Claude, add the Router. OpenCode talks to the CLI's Cost routing, which sends easy turns to Spare and keeps the rest on Claude.
Before you start
-
A Spare API key, exported in your shell profile:
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
{
"$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 }
}
}
}
}
}envnames the environment variable OpenCode reads the key from.- Set
limitandcapabilities. Without them OpenCode assumes a 200K context and image input, which this model doesn't take.
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.
To run a routing preset, add a model whose id is @<name> under
models and pick it in /models.
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, 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:
{
"$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
Run /models and pick Spare Inference → DeepSeek V4.1 Flash, or run one prompt from the shell:
opencode run "Reply with OK"To get a request id for settlement, send a request directly:
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
- 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.headerTimeouton the provider.
How is this guide?