start_within in config, so an agent key is optional here. The key is still the simpler path, because it changes without a restart.
Set this up with an agent
Copy the block below into any coding agent. It never asks for your API key: you run the one export line 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.
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.
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.
Anything not specific to OpenCode is on agent keys. OpenWork and OpenScience read the same provider entry.