INTEGRITY Cloudflare Docs

Claude Code

By pointing Claude Code at AI Gateway instead of a provider directly, you get observability, caching, rate limiting, and centralized credentials for Anthropic, Amazon Bedrock, or Google Vertex AI, without changing how you invoke claude. Claude Code reads its endpoint and credentials from environment variables. If your gateway is protected by Cloudflare Access, refer to Use with Cloudflare Access. 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. When AI Gateway supplies the Anthropic credentials for you — using either an Anthropic API key you store as a provider key (BYOK) or Unified Billing credits — the ANTHROPIC_API_KEY that Claude Code requires can be any placeholder value.

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.

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

    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.

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

    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.

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

    claude

Use with Cloudflare Access

If your gateway is protected by Cloudflare Access, Claude Code can authenticate with a short-lived Access token instead of a gateway token. Point ANTHROPIC_BASE_URL at your custom domain and use Claude Code's apiKeyHelper to fetch the token with cloudflared. Claude Code sends the token as the API key, and Access verifies it at the edge.

Add the following to Claude Code's settings.json, replacing ai-gateway.example.com with your custom domain:

~/.claude/settings.json
{
	"apiKeyHelper": "cloudflared access login --no-verbose https://ai-gateway.example.com",
	"env": {
		"ANTHROPIC_BASE_URL": "https://ai-gateway.example.com/anthropic"
	}
}

The first request opens your identity provider's login flow. After you authenticate, requests route through AI Gateway with your Access identity attached as cf.user_id.

To confirm traffic reaches AI Gateway, refer to Verify it works.