Skip to content

Using Cloudflare AI Gateway with OpenClaw

I often find myself flying blind when sending requests to model providers. It is hard to track usage, manage costs, or see exactly what is happening behind the scenes without writing a lot of custom code. Putting a gateway between your application and your provider is usually the best way to get that visibility back.

If you want to use Cloudflare AI Gateway to manage your Anthropic requests, here is how I set it up.

  • A Cloudflare Account ID and Gateway ID.
  • An Anthropic API key.
  • The OpenClaw CLI installed.
  • Access to your configuration files.

You can get this running in about five minutes. OpenClaw uses the Anthropic Messages API through your specific Gateway endpoint.

First, set your API key and gateway details using the interactive CLI:

Terminal window
openclaw onboard --auth-choice cloudflare-ai-gateway-api-key

Update your configuration to use the gateway provider and the default model:

{
agents: {
defaults: {
model: { primary: "cloudflare-ai-gateway/claude-sonnet-4-5" },
},
},
}

If you prefer scripts or want to skip the prompts, use this command. Replace the placeholders with your actual IDs:

Terminal window
openclaw onboard --non-interactive \
--mode local \
--auth-choice cloudflare-ai-gateway-api-key \
--cloudflare-ai-gateway-account-id "your-account-id" \
--cloudflare-ai-gateway-gateway-id "your-gateway-id" \
--cloudflare-ai-gateway-api-key "$CLOUDFLARE_AI_GATEWAY_API_KEY"

If you turned on Gateway authentication in your Cloudflare settings, you need to pass an extra header. Add the cf-aig-authorization token to your config:

{
models: {
providers: {
"cloudflare-ai-gateway": {
headers: {
"cf-aig-authorization": "Bearer <cloudflare-ai-gateway-token>",
},
},
},
},
}

If you run the gateway as a daemon using launchd or systemd, the process might fail to find your API key. I recommend making sure CLOUDFLARE_AI_GATEWAY_API_KEY is explicitly available to that process. You can place it in ~/.openclaw/.env or provide it via env.shellEnv.

If you have more questions about the setup, check out the AI Setup Assistant.

OpenClaw

OpenClaw Expert

Still stuck?

If this page didn't answer your case, ask OpenClaw Expert for step-by-step guidance.