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.
What You’ll Need
Section titled “What You’ll Need”- A Cloudflare Account ID and Gateway ID.
- An Anthropic API key.
- The OpenClaw CLI installed.
- Access to your configuration files.
Quick Start
Section titled “Quick Start”You can get this running in about five minutes. OpenClaw uses the Anthropic Messages API through your specific Gateway endpoint.
1. Run Onboarding
Section titled “1. Run Onboarding”First, set your API key and gateway details using the interactive CLI:
openclaw onboard --auth-choice cloudflare-ai-gateway-api-key2. Set Your Default Model
Section titled “2. Set Your Default Model”Update your configuration to use the gateway provider and the default model:
{ agents: { defaults: { model: { primary: "cloudflare-ai-gateway/claude-sonnet-4-5" }, }, },}3. Non-interactive Setup
Section titled “3. Non-interactive Setup”If you prefer scripts or want to skip the prompts, use this command. Replace the placeholders with your actual IDs:
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"4. Authenticated Gateways
Section titled “4. Authenticated Gateways”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>", }, }, }, },}Troubleshooting
Section titled “Troubleshooting”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.
What’s Next
Section titled “What’s Next”OpenClaw Expert
Still stuck?
If this page didn't answer your case, ask OpenClaw Expert for step-by-step guidance.