# How to use Cloudflare AI Gateway with Subconscious

> To use Subconscious through Cloudflare AI Gateway, call `deepseek-ai/deepseek-v4.1-flash-1017` or `z-ai/glm-5.3` with a Cloudflare API token and set the `cf-aig-allowed-providers` header to `subconscious`. Requests then run on Subconscious and bill through your Cloudflare account, with no Subconscious key.

Canonical page: https://www.subconscious.dev/integrations/gateways/cloudflare-ai-gateway
Category: [Gateways](https://www.subconscious.dev/integrations/gateways)
Last verified: 2026-12-03
Cloudflare AI Gateway: https://developers.cloudflare.com/ai-gateway/

## Before you start

- A Cloudflare account with AI Gateway credits loaded
- Node.js and npm, for the OpenAI SDK example

## Set up Cloudflare AI Gateway

### 1. Create a Cloudflare API token

In the Cloudflare dashboard, create an API token with the Account > Workers AI > Read permission. Cloudflare requires it for third-party models too. Export it with your account ID, which `wrangler whoami` prints.

```bash
export CLOUDFLARE_API_TOKEN="your_token"
export CLOUDFLARE_ACCOUNT_ID="your_account_id"
```

### 2. Route the request to Subconscious

Call the OpenAI compatible endpoint and set `cf-aig-allowed-providers` to `subconscious`. Without it, AI Gateway can send the request to another provider that serves the model. The `subconscious` field on the response confirms where it ran.

```bash
curl https://api.cloudflare.com/client/v4/accounts/$CLOUDFLARE_ACCOUNT_ID/ai/v1/chat/completions \
  -H "Authorization: Bearer $CLOUDFLARE_API_TOKEN" \
  -H "Content-Type: application/json" \
  -H "cf-aig-allowed-providers: subconscious" \
  -d '{
    "model": "z-ai/glm-5.3",
    "messages": [{ "role": "user", "content": "Hello" }]
  }'
```

### 3. Or use the OpenAI SDK

Point any OpenAI client at the same endpoint and send the provider header with every request.

```ts
import OpenAI from "openai";

const client = new OpenAI({
  apiKey: process.env.CLOUDFLARE_API_TOKEN,
  baseURL: "https://api.cloudflare.com/client/v4/accounts/" + process.env.CLOUDFLARE_ACCOUNT_ID + "/ai/v1",
  defaultHeaders: { "cf-aig-allowed-providers": "subconscious" },
});

const completion = await client.chat.completions.create({
  model: "deepseek-ai/deepseek-v4.1-flash-1017",
  messages: [{ role: "user", content: "Summarize the open issues in this repo." }],
});

console.log(completion.choices[0].message.content);
```

## Frequently asked questions

### Which models does Subconscious serve on Cloudflare AI Gateway?

DeepSeek V4.1 1017 (deepseek-ai/deepseek-v4.1-flash-1017) and GLM-5.3 (z-ai/glm-5.3). Both list Subconscious as a provider in Cloudflare’s model catalog: https://developers.cloudflare.com/ai/models/z-ai/glm-5.3/

### Do I need a Subconscious account?

No. Cloudflare bills you through Unified Billing at the prices in its model catalog. Talk to us directly for higher rate limits or a dedicated deployment.

### Can I prefer Subconscious but fall back to other providers?

Yes. Send cf-aig-provider-order: subconscious instead. AI Gateway tries Subconscious first and can fall back to another provider that serves the model.
