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 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.

Prerequisites

Before you start, you need:

  1. Set the base URL to your gateway's Anthropic endpoint, pass your Anthropic API key, and send your gateway token in the cf-aig-authorization header. 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>, <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>"
  2. Start Claude Code and send a prompt. Requests now route through AI Gateway.

    Terminal window
    claude

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