For the complete documentation index, see llms.txt. Markdown versions of all docs pages are available by appending .md to any docs URL.
OpenCode
Configure the OpenCode AI coding agent to use agentgateway
Route LLM requests from OpenCode through agentgateway. In this guide, OpenCode uses a virtual key, and the provider API key stays on the gateway host.
To connect OpenCode to MCP servers through agentgateway instead, see OpenCode MCP client.
Before you begin
- Install the
agentgatewaybinary. Get an OpenAI API key and save it in the
OPENAI_API_KEYenvironment variable. The configuration in this guide reads the key from that variable.export OPENAI_API_KEY=<your-api-key>Install OpenCode. This guide was tested with OpenCode 1.18.35.
Tip
If you manage models and virtual API keys in the standalone UI, Client Setup can generate the connection settings or snippet for this client. Review the gateway URL, select a model and key, choose the client from the Integration dropdown, and copy the recipe. Each recipe uses only the values that its client supports.
Client Setup generates client-side values from existing configuration. It does not create a route, model, authentication policy, virtual key, or provider credential. Follow the steps in this guide to configure those prerequisites or to set up the client manually.
Configure agentgateway
Configure an OpenAI model and a virtual key for OpenCode. Agentgateway replaces the virtual key with the OpenAI API key when forwarding requests.
Create a configuration file. List each model that OpenCode can use. A request for any other model is rejected.
cat > config.yaml << 'EOF' # yaml-language-server: $schema=https://agentgateway.dev/schema/config llm: policies: apiKey: mode: strict keys: - key: vk-opencode-4f9c2a metadata: user: alice models: - name: gpt-4o-mini provider: openAI params: apiKey: "$OPENAI_API_KEY" EOFSetting Description apiKey.keysThe virtual key for OpenCode. Use one per user or workspace to revoke access separately. See Virtual keys. params.apiKeyThe key for OpenAI. For file-based rotation without a restart, see Provider API keys. Start agentgateway.
agentgateway -f config.yaml
Connect OpenCode to agentgateway
Save the virtual key in an environment variable.
export AGENTGATEWAY_API_KEY='vk-opencode-4f9c2a'Create
opencode.jsonin your project directory. For settings that apply to all projects, use~/.config/opencode/opencode.json.cat > opencode.json <<'EOF' { "$schema": "https://opencode.ai/config.json", "model": "agentgateway/gpt-4o-mini", "provider": { "agentgateway": { "npm": "@ai-sdk/openai-compatible", "name": "Agentgateway", "options": { "baseURL": "http://localhost:4000/v1", "apiKey": "{env:AGENTGATEWAY_API_KEY}" }, "models": { "gpt-4o-mini": { "name": "gpt-4o-mini" } } } } } EOFSetting Description modelThe default model, in <provider>/<model>format.provider.agentgateway.npmThe AI SDK package for an OpenAI-compatible API. options.baseURLThe agentgateway address. The /v1suffix is required.options.apiKeyReads the virtual key from AGENTGATEWAY_API_KEYto keep it out of the file. You can also enter the key with OpenCode’s/connectcommand.modelsThe models that agentgateway serves. Use the same names as the modelsin the agentgateway configuration.Start OpenCode from the same directory.
opencode
Verify the connection
Send a prompt in OpenCode, or run a single prompt from the command line:
opencode run "Say hello"Verify that the request appears in the agentgateway logs.
Example output:
info request gateway=default/default listener=llm route=internal/llm:request endpoint=api.openai.com:443 http.method=POST http.path=/v1/chat/completions http.status=200 protocol=llm gen_ai.operation.name=chat gen_ai.provider.name=openai gen_ai.request.model=gpt-4o-mini ...
If the virtual key is missing, unknown, or revoked, OpenCode shows the 401 response from agentgateway.
Error: Unauthorized: api key authentication failure: invalid credentials