Coding agents
Coding agents send model requests to a provider on your behalf. By pointing the agent at AI Gateway instead of the provider, you observe and control that traffic without changing how you work.
This page uses Claude Code ↗ as the worked example.
Routing a coding agent through AI Gateway gives you:
- Observability — view every request, token count, and latency in the dashboard.
- Caching — return cached responses for repeated prompts.
- Rate limiting — cap request volume with rate limiting.
- Cost tracking — attribute spend across sessions and models.
- Data Loss Prevention — scan prompts and responses for secrets, credentials, and other sensitive data with DLP.
Before you start, you need:
- An authenticated gateway and its gateway token. The gateway token must have
Runpermissions. - Your Cloudflare account ID. To find it, refer to Find your account and zone IDs.
- An Anthropic API key. To create one, go to Account Settings ↗ in the Claude Console. For more information, refer to Anthropic's API overview ↗.
- Claude Code installed and updated to the latest version.
Claude Code reads its endpoint and credentials from environment variables. This configuration sends requests to AI Gateway's Anthropic endpoint using your own Anthropic API key, passed directly in the ANTHROPIC_API_KEY environment variable. The Anthropic endpoint exposes the same /v1/messages API that Claude Code expects.
-
Set the base URL to your gateway's Anthropic endpoint, pass your Anthropic API key, and send your gateway token in the
cf-aig-authorizationheader. The commands below set these as shell environment variables for the current session. To persist them, add them to your shell profile (for example,~/.zshrcor~/.bashrc) or to Claude Code'ssettings.json↗ under theenvkey.Replace
<ACCOUNT_ID>,<GATEWAY_ID>,<ANTHROPIC_API_KEY>, and<CF_AIG_TOKEN>with your values.Terminal window export ANTHROPIC_BASE_URL="https://gateway.ai.cloudflare.com/v1/<ACCOUNT_ID>/<GATEWAY_ID>/anthropic"export ANTHROPIC_API_KEY="<ANTHROPIC_API_KEY>"export ANTHROPIC_CUSTOM_HEADERS="cf-aig-authorization: Bearer <CF_AIG_TOKEN>"PowerShell $env:ANTHROPIC_BASE_URL = "https://gateway.ai.cloudflare.com/v1/<ACCOUNT_ID>/<GATEWAY_ID>/anthropic"$env:ANTHROPIC_API_KEY = "<ANTHROPIC_API_KEY>"$env:ANTHROPIC_CUSTOM_HEADERS = "cf-aig-authorization: Bearer <CF_AIG_TOKEN>" -
Start Claude Code and send a prompt. Requests now route through AI Gateway.
Terminal window claude
Coding agents routinely send source code, configuration files, and snippets to model providers. That traffic can include API keys, customer data, or other sensitive material. Because AI Gateway sits between the agent and the provider, you can inspect and control it without changing the agent.
Data Loss Prevention (DLP) scans request and response bodies against detection profiles and either flags or blocks matches. Use it to catch secrets, credentials, or regulated data leaving (or returning to) the agent.
After you configure a tool, confirm that traffic reaches AI Gateway.
-
Send a prompt from the coding agent.
-
In the Cloudflare dashboard, go to the AI Gateway page.
Go to AI Gateway -
Select your gateway, then select Logs. Confirm that the request appears with its model, token count, and latency.
For more information on logs, refer to Logging.