When you use an AI model in Pi, your request goes directly to the model provider: Anthropic, OpenAI, Google, or whoever hosts the model. That means a separate account, a separate API key, and a separate bill for each provider. An AI gateway sits between Pi and the providers, giving you a single point of control: one bill, one set of logs, one place to manage access.
In this exercise, you’ll set up Cloudflare AI Gateway and connect it to Pi. With Unified Billing, you load credits into your Cloudflare account and use models from Anthropic, OpenAI, and Workers AI without needing their API keys. Cloudflare handles the provider payments.
Why use a gateway?
Without a gateway, every provider is a separate relationship: sign up, get an API key, manage billing, check usage. With a gateway:
- One bill — load credits once, use models from any supported provider
- Logging — every request and response is logged in one place, useful for debugging and understanding usage
- Provider switching — try different models without creating new accounts or managing new API keys
- Caching — repeated identical requests can be served from cache, saving money and reducing latency
This is a different pattern from the free OpenRouter :free models covered in the Models lesson — those are genuinely free, no-account models for learning and experimenting. A gateway is for when you want production-grade access to paid flagship models (Claude, GPT) without juggling a separate account and API key per provider. In this exercise, you’ll set up your own gateway with Cloudflare, giving you direct control over which providers you use, how much you spend, and what gets logged.
Prerequisites
You’ll need a free Cloudflare account and Node.js installed (for the wrangler CLI). If you completed the Build a website exercise, you already have both.
You’ll also load at least $5 in credits into the gateway for Unified Billing. This is real money, but $5 goes a long way: a typical Claude Sonnet conversation costs a few cents.
What you’ll do
- Create a gateway in the Cloudflare dashboard with authentication and logging enabled
- Load credits so Cloudflare handles payments to Anthropic, OpenAI, and other providers on your behalf via Unified Billing
- Store the gateway token with
/loginso Pi can authenticate with the gateway - Try models from at least two different providers through the gateway (e.g. Claude Sonnet and a GPT model)
- Test it by chatting through the gateway and checking the logs in the Cloudflare dashboard
A note on environment variables
The gateway uses a token (starting with cfut_) that’s different from a regular Cloudflare API token. If you already have CLOUDFLARE_API_KEY set as a plain environment variable for other Cloudflare tools, Pi will use that instead of the token you stored with /login. Pi will help you sort this out during setup. Wrangler itself doesn’t need that environment variable if you’ve run wrangler login — it has its own OAuth session.
Automate the dashboard setup
The gateway setup involves several clicks in the Cloudflare dashboard: creating the gateway, enabling authentication, loading credits, generating a token. If you completed the Drive a browser exercise and have the Faster Chrome DevTools skill installed, you can let the agent do the clicking for you. Describe what you want and let it drive the dashboard.