From 8b3d0d00c9bbf9a18a49857247a17752a589a1d0 Mon Sep 17 00:00:00 2001 From: Matt Silverlock Date: Sat, 6 Dec 2025 12:26:38 -0500 Subject: [PATCH 1/4] providers: add Cloudflare AI Gateway --- packages/opencode/src/provider/provider.ts | 19 ++++++++ packages/web/src/content/docs/providers.mdx | 48 +++++++++++++++++++++ 2 files changed, 67 insertions(+) diff --git a/packages/opencode/src/provider/provider.ts b/packages/opencode/src/provider/provider.ts index 0ee95de49..47879cba0 100644 --- a/packages/opencode/src/provider/provider.ts +++ b/packages/opencode/src/provider/provider.ts @@ -310,6 +310,25 @@ export namespace Provider { }, } }, + "cloudflare-ai-gateway": async () => { + const accountId = Env.get("CLOUDFLARE_ACCOUNT_ID") + const gateway = Env.get("CLOUDFLARE_GATEWAY_NAME") + const apiKey = Env.get("CLOUDFLARE_API_TOKEN") + + if (!accountId || !gateway) return { autoload: false } + + return { + autoload: true, + options: { + baseURL: `https://gateway.ai.cloudflare.com/v1/${accountId}/${gateway}/openai`, + apiKey, + headers: { + "HTTP-Referer": "https://opencode.ai/", + "X-Title": "opencode", + }, + }, + } + }, } export const Model = z diff --git a/packages/web/src/content/docs/providers.mdx b/packages/web/src/content/docs/providers.mdx index 3b4bed757..51fd478ef 100644 --- a/packages/web/src/content/docs/providers.mdx +++ b/packages/web/src/content/docs/providers.mdx @@ -319,6 +319,54 @@ Or if you already have an API key, you can select **Manually enter API Key** and --- +### Cloudflare AI Gateway + +Cloudflare AI Gateway lets you proxy requests to AI providers through Cloudflare's infrastructure with caching, rate limiting, and analytics. + +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 name as environment variables: + + ```bash + CLOUDFLARE_ACCOUNT_ID=your-account-id CLOUDFLARE_GATEWAY_NAME=your-gateway-name opencode + ``` + + Or add them to your bash profile: + + ```bash title="~/.bash_profile" + export CLOUDFLARE_ACCOUNT_ID=your-account-id + export CLOUDFLARE_GATEWAY_NAME=your-gateway-name + ``` + +3. If your gateway has authentication enabled, also set your API token: + + ```bash title="~/.bash_profile" + export CLOUDFLARE_API_TOKEN=your-api-token + ``` + +4. Many Cloudflare AI Gateway models are preloaded by default, run the `/models` command to select the one you want. + + ```txt + /models + ``` + + You can also add additional models through your opencode config. + + ```json title="opencode.json" {6} + { + "$schema": "https://opencode.ai/config.json", + "provider": { + "cloudflare-ai-gateway": { + "models": { + "some-other-model": {} + } + } + } + } + ``` + +--- + ### Cortecs 1. Head over to the [Cortecs console](https://cortecs.ai/), create an account, and generate an API key. From 63a2ab9a21c2cdcf56c4d113eb6ec915fd92428c Mon Sep 17 00:00:00 2001 From: Ming Lu Date: Fri, 12 Dec 2025 13:54:07 -0800 Subject: [PATCH 2/4] fix baseURL --- packages/opencode/src/provider/provider.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/opencode/src/provider/provider.ts b/packages/opencode/src/provider/provider.ts index 47879cba0..97bb16190 100644 --- a/packages/opencode/src/provider/provider.ts +++ b/packages/opencode/src/provider/provider.ts @@ -320,7 +320,7 @@ export namespace Provider { return { autoload: true, options: { - baseURL: `https://gateway.ai.cloudflare.com/v1/${accountId}/${gateway}/openai`, + baseURL: `https://gateway.ai.cloudflare.com/v1/${accountId}/${gateway}/compat`, apiKey, headers: { "HTTP-Referer": "https://opencode.ai/", From b95b514ade1153a1c446f32ac1f4a85a1fba3c3d Mon Sep 17 00:00:00 2001 From: Ming Lu Date: Fri, 12 Dec 2025 13:14:16 -0800 Subject: [PATCH 3/4] update env variable to match what's in models.dev https://github.com/sst/models.dev/blob/dev/providers/cloudflare-ai-gateway/provider.toml#L2 hasit called GATEWAY_ID, not GATEWAY_NAME --- packages/opencode/src/provider/provider.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/opencode/src/provider/provider.ts b/packages/opencode/src/provider/provider.ts index 97bb16190..4a1b3918c 100644 --- a/packages/opencode/src/provider/provider.ts +++ b/packages/opencode/src/provider/provider.ts @@ -312,7 +312,7 @@ export namespace Provider { }, "cloudflare-ai-gateway": async () => { const accountId = Env.get("CLOUDFLARE_ACCOUNT_ID") - const gateway = Env.get("CLOUDFLARE_GATEWAY_NAME") + const gateway = Env.get("CLOUDFLARE_GATEWAY_ID") const apiKey = Env.get("CLOUDFLARE_API_TOKEN") if (!accountId || !gateway) return { autoload: false } From dbc5ebbda5b6ae75ca5896644f86ad6bae34b44b Mon Sep 17 00:00:00 2001 From: Matt Silverlock Date: Mon, 15 Dec 2025 08:34:38 -0500 Subject: [PATCH 4/4] providers: update CF provider docs --- packages/web/src/content/docs/providers.mdx | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/packages/web/src/content/docs/providers.mdx b/packages/web/src/content/docs/providers.mdx index 721c80e15..4c36d561f 100644 --- a/packages/web/src/content/docs/providers.mdx +++ b/packages/web/src/content/docs/providers.mdx @@ -329,17 +329,17 @@ Cloudflare AI Gateway lets you proxy requests to AI providers through Cloudflare 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 name as environment variables: +2. Set your Account ID and Gateway ID as environment variables: ```bash - CLOUDFLARE_ACCOUNT_ID=your-account-id CLOUDFLARE_GATEWAY_NAME=your-gateway-name opencode + export CLOUDFLARE_ACCOUNT_ID=your-32-character-account-id && export CLOUDFLARE_GATEWAY_ID=your-gateway-id ``` Or add them to your bash profile: ```bash title="~/.bash_profile" - export CLOUDFLARE_ACCOUNT_ID=your-account-id - export CLOUDFLARE_GATEWAY_NAME=your-gateway-name + export CLOUDFLARE_ACCOUNT_ID=your-32-character-account-id + export CLOUDFLARE_GATEWAY_ID=your-gateway-id ``` 3. If your gateway has authentication enabled, also set your API token: