When you use an AI model in OpenCode, your request goes directly to the model provider: Anthropic, OpenAI, Google, or whoever hosts the model. That means a separate account, a separate API key, and a separate bill for each provider. An AI gateway sits between OpenCode and the providers, giving you a single point of control: one bill, one set of logs, one place to manage access.
In this exercise, you’ll set up Cloudflare AI Gateway and connect it to OpenCode. With Unified Billing, you load credits into your Cloudflare account and use models from Anthropic, OpenAI, and other providers without needing their API keys. Cloudflare handles the provider payments.
Why use a gateway?
Without a gateway, every provider is a separate relationship: sign up, get an API key, manage billing, check usage. With a gateway:
- One bill — load credits once, use models from any supported provider
- Logging — every request and response is logged in one place, useful for debugging and understanding usage
- Provider switching — try different models without creating new accounts or managing new API keys
- Caching — repeated identical requests can be served from cache, saving money and reducing latency
AI gateways are a common pattern. OpenCode’s own Zen and Go plans work the same way — they’re gateways that proxy your requests to multiple providers behind a single API key. In this exercise, you’ll set up your own gateway with Cloudflare, giving you direct control over which providers you use, how much you spend, and what gets logged.
Prerequisites
You’ll need a free Cloudflare account and Node.js installed (for the wrangler CLI). If you completed the Build a website exercise, you already have both.
You’ll also load at least $5 in credits into the gateway for Unified Billing. This is real money, but $5 goes a long way: a typical Claude Sonnet conversation costs a few cents.
What you’ll do
- Create a gateway in the Cloudflare dashboard with authentication and logging enabled
- Enable Unified Billing so Cloudflare handles payments to Anthropic, OpenAI, and other providers on your behalf
- Store the gateway token in OpenCode so it can authenticate with the gateway
- Add models from at least two different providers to your OpenCode config (e.g. Claude Sonnet and GPT-4.1)
- Test it by chatting through the gateway and checking the logs in the Cloudflare dashboard
A note on environment variables
The gateway uses a token (starting with cfut_) that’s different from a regular Cloudflare API token. If you already have CLOUDFLARE_API_TOKEN set in your shell for wrangler or other Cloudflare tools, OpenCode will use that instead of the gateway token. The agent will help you sort this out during setup. Wrangler itself doesn’t need that environment variable if you’ve run wrangler login — it has its own OAuth session.
Automate the dashboard setup
The gateway setup involves several clicks in the Cloudflare dashboard: creating the gateway, enabling authentication, loading credits, generating a token. If you completed the Drive a browser exercise and have Chrome DevTools MCP configured, you can let the agent do the clicking for you. Describe what you want in Plan Mode and let it drive the dashboard.