Claude Code
Claude Code ↗ reads its endpoint and credentials from environment variables. This configuration sends requests to AI Gateway's Anthropic endpoint, authenticated with your Cloudflare gateway token. The Anthropic endpoint exposes the same /v1/messages API that Claude Code expects. With Unified Billing, Cloudflare pays the provider and bills you, so you do not need an Anthropic API key.
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.
- Credentials for the provider you route to:
- Anthropic: Unified Billing credits loaded on your Cloudflare account. No Anthropic API key is required.
- Amazon Bedrock or Google Vertex AI: your provider credentials stored in AI Gateway as a provider key (BYOK).
- Claude Code ↗ installed and updated to the latest version.
-
Set the base URL to your gateway's Anthropic endpoint and send your gateway token in the
cf-aig-authorizationheader. SetANTHROPIC_API_KEYto the same token, since Claude Code requires the variable to be set. The following commands 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>, 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="<CF_AIG_TOKEN>"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 = "<CF_AIG_TOKEN>"$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
To run Claude models through Amazon Bedrock instead, point Claude Code at your gateway's Amazon Bedrock endpoint. AI Gateway authenticates to Bedrock with the AWS credentials you store as a provider key, so you can skip Claude Code's own AWS authentication.
-
Replace
<ACCOUNT_ID>,<GATEWAY_ID>,<AWS_REGION>(for example,us-east-1), and<CF_AIG_TOKEN>with your values.Terminal window export CLAUDE_CODE_USE_BEDROCK="1"export ANTHROPIC_BEDROCK_BASE_URL="https://gateway.ai.cloudflare.com/v1/<ACCOUNT_ID>/<GATEWAY_ID>/aws-bedrock/bedrock-runtime/<AWS_REGION>/"export CLAUDE_CODE_SKIP_BEDROCK_AUTH="1"export ANTHROPIC_CUSTOM_HEADERS="cf-aig-authorization: Bearer <CF_AIG_TOKEN>"PowerShell $env:CLAUDE_CODE_USE_BEDROCK = "1"$env:ANTHROPIC_BEDROCK_BASE_URL = "https://gateway.ai.cloudflare.com/v1/<ACCOUNT_ID>/<GATEWAY_ID>/aws-bedrock/bedrock-runtime/<AWS_REGION>/"$env:CLAUDE_CODE_SKIP_BEDROCK_AUTH = "1"$env:ANTHROPIC_CUSTOM_HEADERS = "cf-aig-authorization: Bearer <CF_AIG_TOKEN>" -
Start Claude Code and send a prompt. Requests now route through AI Gateway to Amazon Bedrock.
Terminal window claude
To run Claude models through Google Vertex AI instead, point Claude Code at your gateway's Google Vertex AI endpoint. AI Gateway authenticates to Vertex AI with the Google Cloud credentials you store as a provider key, so you can skip Claude Code's own Vertex authentication.
-
Replace
<ACCOUNT_ID>,<GATEWAY_ID>,<GCP_PROJECT_ID>,<GCP_REGION>(for example,us-east5), and<CF_AIG_TOKEN>with your values.Terminal window export CLAUDE_CODE_USE_VERTEX="1"export ANTHROPIC_VERTEX_BASE_URL="https://gateway.ai.cloudflare.com/v1/<ACCOUNT_ID>/<GATEWAY_ID>/google-vertex-ai/v1"export ANTHROPIC_VERTEX_PROJECT_ID="<GCP_PROJECT_ID>"export CLOUD_ML_REGION="<GCP_REGION>"export CLAUDE_CODE_SKIP_VERTEX_AUTH="1"export ANTHROPIC_CUSTOM_HEADERS="cf-aig-authorization: Bearer <CF_AIG_TOKEN>"PowerShell $env:CLAUDE_CODE_USE_VERTEX = "1"$env:ANTHROPIC_VERTEX_BASE_URL = "https://gateway.ai.cloudflare.com/v1/<ACCOUNT_ID>/<GATEWAY_ID>/google-vertex-ai/v1"$env:ANTHROPIC_VERTEX_PROJECT_ID = "<GCP_PROJECT_ID>"$env:CLOUD_ML_REGION = "<GCP_REGION>"$env:CLAUDE_CODE_SKIP_VERTEX_AUTH = "1"$env:ANTHROPIC_CUSTOM_HEADERS = "cf-aig-authorization: Bearer <CF_AIG_TOKEN>" -
Start Claude Code and send a prompt. Requests now route through AI Gateway to Google Vertex AI.
Terminal window claude
To confirm traffic reaches AI Gateway, refer to Verify it works.