OpenCode ↗ is an open source coding agent that supports custom provider configuration. Point its built-in providers at AI Gateway to observe and control model requests from OpenCode.
Before you start, you need:
- An AI Gateway and its gateway slug.
- Sufficient Unified Billing credits or a stored provider key with the
defaultalias for each provider. - OpenCode installed ↗.
To use this method, you also need an authenticated gateway and its gateway token. The token must have Run permissions. You also need your Cloudflare account ID. To find it, refer to Find your account and zone IDs.
-
Set your gateway token as the
CF_AIG_TOKENenvironment variable. The following commands set it for the current session. To persist it, add it to your shell profile.Replace
<CF_AIG_TOKEN>with your gateway token.export CF_AIG_TOKEN="<CF_AIG_TOKEN>"$env:CF_AIG_TOKEN = "<CF_AIG_TOKEN>" -
In your project root, create an
opencode.jsonfile. Replace<ACCOUNT_ID>and<GATEWAY_ID>with your account ID and gateway slug:opencode.jsonjson { "$schema": "https://opencode.ai/config.json", "share": "disabled", "disabled_providers": ["opencode"], "enabled_providers": ["anthropic", "openai", "google", "xai"], "provider": { "anthropic": { "name": "Anthropic through Cloudflare AI Gateway", "options": { "baseURL": "https://gateway.ai.cloudflare.com/v1/<ACCOUNT_ID>/<GATEWAY_ID>/anthropic/v1", "apiKey": "", "headers": { "cf-aig-authorization": "Bearer {env:CF_AIG_TOKEN}" } } }, "openai": { "name": "OpenAI through Cloudflare AI Gateway", "options": { "baseURL": "https://gateway.ai.cloudflare.com/v1/<ACCOUNT_ID>/<GATEWAY_ID>/openai", "apiKey": "", "headers": { "cf-aig-authorization": "Bearer {env:CF_AIG_TOKEN}" } } }, "google": { "name": "Google AI Studio through Cloudflare AI Gateway", "options": { "baseURL": "https://gateway.ai.cloudflare.com/v1/<ACCOUNT_ID>/<GATEWAY_ID>/google-ai-studio/v1beta", "apiKey": "", "headers": { "cf-aig-authorization": "Bearer {env:CF_AIG_TOKEN}" } } }, "xai": { "name": "xAI through Cloudflare AI Gateway", "options": { "baseURL": "https://gateway.ai.cloudflare.com/v1/<ACCOUNT_ID>/<GATEWAY_ID>/grok/v1", "apiKey": "", "headers": { "cf-aig-authorization": "Bearer {env:CF_AIG_TOKEN}" } } } } }Leave each
apiKeyvalue empty. AI Gateway supplies provider credentials through Unified Billing or your stored provider keys. To use a stored key without thedefaultalias, add thecf-aig-byok-aliasheader to that provider'sheadersobject. Remove providers that you do not use. For more configuration options, refer to OpenCode providers ↗. -
Start OpenCode and select a configured provider and model:
opencode
To confirm traffic reaches AI Gateway, refer to Verify it works.
If your gateway is protected by Cloudflare Access, OpenCode can authenticate with a short-lived Access token instead of a gateway token. You can also host the configuration centrally so users connect with one login command.
This setup requires an AI Gateway custom domain, cloudflared on each user's device, and a public HTTPS location for two configuration files. You can use an R2 bucket with a custom domain.
The files contain configuration, but no credentials. A Single Redirect sends /.well-known/opencode requests from your AI Gateway custom domain to the discovery file.
Users can override remote configuration in their global or project configuration. To enforce organization-wide settings, refer to OpenCode managed settings ↗.
The following example uses ai.example.com for the AI Gateway domain and config.example.com for the configuration host. Replace both hostnames with your own values.
-
Outside any OpenCode project, create a copy of the
opencode.jsonfile from Connect with a gateway token. Replace each providerbaseURLwith the corresponding value:Provider Base URL Anthropic https://ai.example.com/anthropic/v1OpenAI https://ai.example.com/openaiGoogle https://ai.example.com/google-ai-studio/v1betaxAI https://ai.example.com/grok/v1Replace each provider's
headersobject with the following value:{ "cf-access-token": "{env:TOKEN}", "X-Requested-With": "XMLHttpRequest" } -
Upload
opencode.jsonto your public HTTPS host. Confirm that its URL returns the expected JSON without authentication.If you use R2, upload the object to the bucket and connect a custom domain. The example file should be available at
https://config.example.com/opencode.json.Do not keep another copy in a project or global OpenCode configuration. Local configuration takes precedence over remote configuration and would prevent later hosted updates from applying.
-
Create an
opencodediscovery file. Setremote_config.urlto the public URL of theopencode.jsonfile:opencodejson { "auth": { "command": [ "cloudflared", "access", "login", "--no-verbose", "-app=https://ai.example.com/" ], "env": "TOKEN" }, "remote_config": { "url": "https://config.example.com/opencode.json" } }OpenCode runs the authentication command and makes its output available as
{env:TOKEN}. It then substitutes that value into the remote provider configuration. -
Upload
opencodeto your public HTTPS host. Confirm thathttps://config.example.com/opencodereturns the discovery JSON without authentication. -
In the zone for your AI Gateway custom domain, create a Single Redirect with these settings:
- Rule name:
OpenCode discovery - Custom filter expression:
(http.host eq "ai.example.com" and http.request.uri.path eq "/.well-known/opencode") - Target URL:
https://config.example.com/opencode - Status code:
302 - Preserve query string: Off
- Rule name:
-
In a browser without an active Access session, open
https://ai.example.com/.well-known/opencode. Confirm that the request returns the discovery JSON without an Access prompt. -
To connect OpenCode, run:
opencode auth login https://ai.example.comComplete the Access login flow when prompted. OpenCode stores the resulting credential locally and loads the remote configuration. Run the command again when the Access session expires.
-
Start OpenCode and select a configured provider and model:
opencode