Skip to content

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.

Prerequisites

Before you start, you need:

  1. Set the base URL to your gateway's Anthropic endpoint and send your gateway token in the cf-aig-authorization header. Set ANTHROPIC_API_KEY to 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, ~/.zshrc or ~/.bashrc) or to Claude Code's settings.json under the env key.

    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>"
  2. Start Claude Code and send a prompt. Requests now route through AI Gateway.

    Terminal window
    claude

Use Amazon Bedrock

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.

  1. 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>"
  2. Start Claude Code and send a prompt. Requests now route through AI Gateway to Amazon Bedrock.

    Terminal window
    claude

Use Google Vertex AI

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.

  1. 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>"
  2. 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.