Skip to main content
Claude Code is Anthropic’s agentic coding tool for the terminal, IDEs, and desktop. It speaks the Anthropic Messages API, so pointing its base URL at the AI Gateway sends every model request through ngrok.ai, where it shows up in observability and follows your access key configuration.

What you’ll need

Point the CLI at the gateway

Set two environment variables in your shell:
  • ANTHROPIC_BASE_URL is the gateway URL without /v1, since Claude Code appends /v1/messages itself
  • ANTHROPIC_AUTH_TOKEN is your access key, which Claude Code sends as an Authorization: Bearer header
ANTHROPIC_AUTH_TOKEN takes precedence over a saved claude.ai login, so Claude Code uses your access key as soon as you start it. If ANTHROPIC_API_KEY is also set in your environment, unset it to avoid an auth conflict warning. To keep the configuration across terminals and for background agents, move the same values into the env block of ~/.claude/settings.json:
Don’t put your access key in a project’s .claude/settings.json. That file is committed and shared with everyone who clones the repository.

Test the connection

Before you start Claude Code, send a short request straight to the gateway from the same shell:
A JSON response that starts with {"id":"msg_ means the URL and access key work. A 401 means the gateway rejected the access key. Then start claude, run /status, and check two lines:
  • Anthropic base URL shows https://gateway.ngrok.ai
  • Auth token names ANTHROPIC_AUTH_TOKEN
Send any prompt, then confirm the request in AI Gateway observability in app.ngrok.ai.

Choose a model

Claude Code’s built-in model aliases, such as opus, sonnet, and haiku, resolve to Anthropic model IDs like claude-sonnet-5-5. The gateway finds those IDs in the model catalog, so /model and --model work without extra setup. To pin the model behind each alias, set these variables alongside the base URL:
Use the catalog’s model IDs rather than dated snapshot IDs like claude-haiku-4-5-20251001, which the gateway doesn’t match. To add the gateway’s Claude models to the /model picker, turn on Claude Code’s gateway model discovery:
At startup, Claude Code reads the gateway’s model list, which only includes models your access key is allowed to call, and adds the Claude models in it, such as anthropic/claude-sonnet-5-5. The gateway doesn’t serve Anthropic’s token counting endpoint, so /context shows estimated token counts.
Claude Code only works with Claude models through the gateway. /v1/messages only reaches providers that support the Anthropic Messages API, and Anthropic doesn’t support routing Claude Code to other models.

Use other surfaces

The CLI reads the shell variables and ~/.claude/settings.json. Other surfaces need the same two values in a different place:
  • VS Code extension: set them in claudeCode.environmentVariables in your VS Code user settings
  • Claude Agent SDK: pass them in the SDK’s env option, which sets the environment of the Claude Code process it spawns
  • Claude desktop app: turn on Developer Mode, then enter the gateway details under Developer > Configure Third-Party Inference
See Claude Code’s guide to connecting to an LLM gateway for each surface.

Troubleshooting

For gateway-side errors, see Debug failed requests.

Next steps