start_within in config, so an agent key is optional here. The key is still the simpler path, because it changes without a restart. See agent keys.
Set this up with an agent
Open the block below and copy it into any coding agent. The prompt never asks for your API key: the agent configures everything else, then prints the one export line for you to run yourself.Copy agent setup prompt
Copy agent setup prompt
Configure OpenCode
-
Export the key in the shell that starts OpenCode.
-
Write the provider entry to
opencode.json. -
Start OpenCode against that file.
npm picks the OpenAI-compatible package. options.baseURL sends requests to the router. options.apiKey reads the key from the environment. Each key under models is a slug we run.
Add a model by adding a key under models. Everything else in the entry is display and limits.
Global or per project
Put the provider entry in one of two places, depending on how widely you want it.
Project config wins over global. OpenCode looks for
opencode.json in the working directory and walks up to the nearest Git directory, so a repo can override your machine-wide provider without touching it.
Put the provider entry in the global file and the deadline stays the same everywhere. Put it in a project file and only that repo routes through us.
Set OPENCODE_DISABLE_PROJECT_CONFIG to stop the upward search.
Two more sources exist for bundles and automation. OPENCODE_CONFIG_DIR names a directory OpenCode reads config out of, and OPENCODE_CONFIG_CONTENT holds the JSON inline. Use OPENCODE_CONFIG for a file you opt into per run.
Set the deadline in config instead
OpenCode can sendstart_within itself. Both places are plain JSON, and both beat the key.
Set it per model. The options block goes into the request body for that model.
--variant on opencode run, or cycle them in the TUI with ctrl+t.
Confirm the key applied
Every response comes withx-flexinference-defaults-applied. OpenCode doesn’t show response headers in the TUI, so read the request in the dashboard under Logs instead.
Troubleshooting
Requests never reach us. The top-levelmodel field is what selects the provider, so defining a provider without naming it there leaves OpenCode on its built-in default. Set "model": "flexinference/<slug>".
401 invalid_api_key. FLEXINFERENCE_API_KEY is unset in the shell that started OpenCode. Export it before launching, since options.apiKey reads it from the environment.
400 unsupported_parameter. A Chat Completions parameter we don’t translate was in a model’s options, such as seed or stop. Remove it from the entry.
400 key_default_not_applicable. Your key holds a duration and one of those parameters came beside it. Edit the key to a tier, or remove the parameter.
400 flex_unsupported_for_anthropic. A duration in options reached a claude-* model on your own Anthropic key. The race a Claude duration runs takes managed keys. Remove the duration and let the key hold the deadline, since a key duration falls back instead.
A config edit changed nothing. A later config source overrode it. Check the search order above, and prefer OPENCODE_CONFIG to opt into one file per run.
See errors for every refusal we return, and agent keys for the ones that are not specific to OpenCode. OpenWork and OpenScience read the same provider entry.