OpenCode
Configure OpenCode to route its OpenAI and Anthropic requests through AI Gateway.
On this page
Note
AI Gateway is part of AI Governance, which is included with a Premium license.
OpenCode supports both OpenAI and Anthropic models and can be configured to use AI Gateway by setting custom base URLs for each provider.
Centralized API Key
You can configure OpenCode to connect to AI Gateway by setting the following configuration options in your OpenCode configuration file (e.g., ~/.config/opencode/opencode.json):
{
"$schema": "https://opencode.ai/config.json",
"provider": {
"anthropic": {
"options": {
"baseURL": "https://coder.example.com/api/v2/ai-gateway/anthropic/v1"
}
},
"openai": {
"options": {
"baseURL": "https://coder.example.com/api/v2/ai-gateway/openai/v1"
}
}
}
}To authenticate with AI Gateway, get your Coder API token, then run:
opencode auth loginSelect anthropic (and repeat for openai if needed), then paste your Coder API token when prompted for the API key.
BYOK (Personal API Key)
Set the following in ~/.config/opencode/opencode.json, including the X-Coder-AI-Governance-Token header with your Coder API token:
{
"$schema": "https://opencode.ai/config.json",
"provider": {
"anthropic": {
"options": {
"baseURL": "https://coder.example.com/api/v2/ai-gateway/anthropic/v1",
"headers": {
"X-Coder-AI-Governance-Token": "<your-coder-api-token>"
}
}
},
"openai": {
"options": {
"baseURL": "https://coder.example.com/api/v2/ai-gateway/openai/v1",
"headers": {
"X-Coder-AI-Governance-Token": "<your-coder-api-token>"
}
}
}
}
}Set your personal API keys by running:
opencode auth loginSelect anthropic (and repeat for openai if needed), then paste the corresponding API key you obtained earlier when prompted.
Custom models through Bedrock Mantle
You can use OpenCode with custom model IDs through a Bedrock Mantle provider configured by your Coder administrator. AI Gateway signs requests with AWS credentials centrally, so you only need a Coder API token in OpenCode. Use a custom provider in OpenCode that matches your model's API, rather than its native Amazon Bedrock integration. The following example uses Astra with the Responses API. For other models, choose the SDK package that matches the API:
| Model API | OpenCode npm package |
|---|---|
| Responses | @ai-sdk/openai |
| Chat Completions | @ai-sdk/openai-compatible |
| Anthropic Messages | @ai-sdk/anthropic |
-
In a shell where you're logged in with the Coder CLI, set your token:
export CODER_TOKEN="$(coder login token)"This command sets
CODER_TOKENwithout printing the token. -
Merge the following configuration into
~/.config/opencode/opencode.json:{ "$schema": "https://opencode.ai/config.json", "provider": { "coder-mantle": { "npm": "@ai-sdk/openai", "name": "Coder Bedrock Mantle", "options": { "baseURL": "https://coder.example.com/api/v2/ai-gateway/bedrock-provider-name/v1", "apiKey": "{env:CODER_TOKEN}" }, "models": { "astra": { "id": "openai.gpt-6-astra", "name": "Astra", "tool_call": true } } } }, "model": "coder-mantle/astra", "small_model": "coder-mantle/astra" }Replace
coder.example.comwith your AI Gateway host andbedrock-provider-namewith the provider name configured in Coder. Keep the/v1suffix. Replaceopenai.gpt-6-astrawith the exact Mantle model ID available in your AWS account and region. Setnpmto the SDK package for that model's API. Settool_calltotrueonly if the model supports tools.The
astrakey is a local model alias in OpenCode. Theidfield is the model ID sent upstream. Preserve any vendor prefix in the model ID, such asopenai.oranthropic.. You can add more entries undermodelsto use model IDs outside OpenCode's built-in catalog.The optional
modelandsmall_modelfields select the default model and the model for lightweight tasks, such as title generation. This example uses Astra for both, so both types of request use the same gateway provider. To use a different small model, add its entry undermodelsand updatesmall_modeltocoder-mantle/<model-alias>. -
Start OpenCode from the same shell:
opencodeOpenCode uses the configured Astra model through AI Gateway.
All three SDK packages use the same gateway base URL ending in /v1.
The selected SDK sends requests to /v1/responses, /v1/chat/completions, or /v1/messages.
Use a model that supports the selected API.
For more configuration options, refer to OpenCode custom providers.
References: OpenCode Documentation