summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorMatt Silverlock <[email protected]>2025-12-23 18:31:58 -0500
committerGitHub <[email protected]>2025-12-23 17:31:58 -0600
commit9898fbe8ef7584002691e1e9b6d6da4b526caa63 (patch)
tree4eb8f446630891414831d8f0df915ad9cd4ea8bd
parent1bd8e617198b7e1592142849accbc7889ae2526b (diff)
downloadopencode-9898fbe8ef7584002691e1e9b6d6da4b526caa63.tar.gz
opencode-9898fbe8ef7584002691e1e9b6d6da4b526caa63.zip
providers: add Cloudflare AI Gateway (#5174)
-rw-r--r--packages/opencode/src/provider/provider.ts39
-rw-r--r--packages/web/src/content/docs/providers.mdx58
2 files changed, 97 insertions, 0 deletions
diff --git a/packages/opencode/src/provider/provider.ts b/packages/opencode/src/provider/provider.ts
index b11ca9368..0fdf26392 100644
--- a/packages/opencode/src/provider/provider.ts
+++ b/packages/opencode/src/provider/provider.ts
@@ -338,6 +338,45 @@ export namespace Provider {
},
}
},
+ "cloudflare-ai-gateway": async (input) => {
+ const accountId = Env.get("CLOUDFLARE_ACCOUNT_ID")
+ const gateway = Env.get("CLOUDFLARE_GATEWAY_ID")
+
+ if (!accountId || !gateway) return { autoload: false }
+
+ // Get API token from env or auth prompt
+ const apiToken = await (async () => {
+ const envToken = Env.get("CLOUDFLARE_API_TOKEN")
+ if (envToken) return envToken
+ const auth = await Auth.get(input.id)
+ if (auth?.type === "api") return auth.key
+ return undefined
+ })()
+
+ return {
+ autoload: true,
+ async getModel(sdk: any, modelID: string, _options?: Record<string, any>) {
+ return sdk.chat(modelID)
+ },
+ options: {
+ baseURL: `https://gateway.ai.cloudflare.com/v1/${accountId}/${gateway}/compat`,
+ headers: {
+ // Cloudflare AI Gateway uses cf-aig-authorization for authenticated gateways
+ // This enables Unified Billing where Cloudflare handles upstream provider auth
+ ...(apiToken ? { "cf-aig-authorization": `Bearer ${apiToken}` } : {}),
+ "HTTP-Referer": "https://opencode.ai/",
+ "X-Title": "opencode",
+ },
+ // Custom fetch to strip Authorization header - AI Gateway uses cf-aig-authorization instead
+ // Sending Authorization header with invalid value causes auth errors
+ fetch: async (input: RequestInfo | URL, init?: RequestInit) => {
+ const headers = new Headers(init?.headers)
+ headers.delete("Authorization")
+ return fetch(input, { ...init, headers })
+ },
+ },
+ }
+ },
cerebras: async () => {
return {
autoload: false,
diff --git a/packages/web/src/content/docs/providers.mdx b/packages/web/src/content/docs/providers.mdx
index 5f9b040d4..e38c0dff1 100644
--- a/packages/web/src/content/docs/providers.mdx
+++ b/packages/web/src/content/docs/providers.mdx
@@ -323,6 +323,64 @@ If you encounter "I'm sorry, but I cannot assist with that request" errors, try
---
+### Cloudflare AI Gateway
+
+Cloudflare AI Gateway lets you access models from OpenAI, Anthropic, Workers AI, and more through a unified endpoint. With [Unified Billing](https://developers.cloudflare.com/ai-gateway/features/unified-billing/) you don't need separate API keys for each provider.
+
+1. Head over to the [Cloudflare dashboard](https://dash.cloudflare.com/), navigate to **AI** > **AI Gateway**, and create a new gateway.
+
+2. Set your Account ID and Gateway ID as environment variables.
+
+ ```bash title="~/.bash_profile"
+ export CLOUDFLARE_ACCOUNT_ID=your-32-character-account-id
+ export CLOUDFLARE_GATEWAY_ID=your-gateway-id
+ ```
+
+3. Run the `/connect` command and search for **Cloudflare AI Gateway**.
+
+ ```txt
+ /connect
+ ```
+
+4. Enter your Cloudflare API token.
+
+ ```txt
+ ┌ API key
+ │
+ │
+ └ enter
+ ```
+
+ Or set it as an environment variable.
+
+ ```bash title="~/.bash_profile"
+ export CLOUDFLARE_API_TOKEN=your-api-token
+ ```
+
+5. Run the `/models` command to select a model.
+
+ ```txt
+ /models
+ ```
+
+ You can also add models through your opencode config.
+
+ ```json title="opencode.json"
+ {
+ "$schema": "https://opencode.ai/config.json",
+ "provider": {
+ "cloudflare-ai-gateway": {
+ "models": {
+ "openai/gpt-4o": {},
+ "anthropic/claude-sonnet-4": {}
+ }
+ }
+ }
+ }
+ ```
+
+---
+
### Cortecs
1. Head over to the [Cortecs console](https://cortecs.ai/), create an account, and generate an API key.