# Claude Code

Claude Code can read endpoint and authentication settings from environment variables or settings files. Use those settings when Claude Code should call AISIX as an Anthropic-compatible gateway instead of calling Anthropic directly.

In this guide, you will point Claude Code at AISIX and authenticate with an AISIX caller API key. You will also select an AISIX model alias that is available on the Anthropic Messages route.

## Prerequisites[​](#prerequisites "Direct link to Prerequisites")

Before starting, prepare the following:

* Claude Code installed.
* A running AISIX gateway that Claude Code can reach.
* An AISIX caller API key.
* An AISIX model alias that the caller API key can access through [Anthropic Messages](https://docs.api7.ai/ai-gateway/endpoints/anthropic-messages.md). Use an alias backed by a native Anthropic-protocol route to preserve Anthropic-specific request and response behavior.

If AISIX is already deployed for your organization, obtain the gateway URL, model alias, and caller API key from the team that manages it. Otherwise, follow the [AISIX OSS getting-started guide](https://docs.api7.ai/ai-gateway/getting-started/gateway-quickstart.md) or [AISIX Enterprise getting-started guide](https://docs.api7.ai/ai-gateway/getting-started/aisix-cloud-quickstart.md), or [contact API7](https://api7.ai/contact) for AISIX Cloud access.

## Configure Claude Code[​](#configure-claude-code "Direct link to Configure Claude Code")

Set the endpoint, caller key, and model alias before launching Claude Code:

```
# ANTHROPIC_BASE_URL is the gateway origin without a trailing slash or /v1

# The local quickstarts use http://127.0.0.1:3000

export ANTHROPIC_BASE_URL="YOUR_AISIX_GATEWAY_URL"

export ANTHROPIC_AUTH_TOKEN="YOUR_CALLER_API_KEY"

export ANTHROPIC_MODEL="claude-sonnet-prod"

export ANTHROPIC_DEFAULT_HAIKU_MODEL="claude-sonnet-prod"



claude
```

`ANTHROPIC_BASE_URL` points Claude Code at AISIX. Claude Code sends `ANTHROPIC_AUTH_TOKEN` as a bearer token, so AISIX receives the value as the caller API key. `ANTHROPIC_MODEL` and `ANTHROPIC_DEFAULT_HAIKU_MODEL` should be AISIX model aliases, not upstream provider model IDs. You can point both variables to the same alias, or use a separate fast alias for Claude Code background behavior.

When `ANTHROPIC_MODEL` is a semantic router, point `ANTHROPIC_DEFAULT_HAIKU_MODEL` at a direct model. This keeps Claude Code's background requests out of semantic classification; see [How Pins Behave](https://docs.api7.ai/ai-gateway/routing/semantic-routing.md#how-pins-behave).

To apply the same settings every time Claude Code starts, put them in the `env` block of a Claude Code settings file:

\~/.claude/settings.json

```
{

  "model": "claude-sonnet-prod",

  "env": {

    "ANTHROPIC_BASE_URL": "YOUR_AISIX_GATEWAY_URL",

    "ANTHROPIC_AUTH_TOKEN": "YOUR_CALLER_API_KEY",

    "ANTHROPIC_DEFAULT_HAIKU_MODEL": "claude-sonnet-prod"

  }

}
```

## Verify the Integration[​](#verify-the-integration "Direct link to Verify the Integration")

Start Claude Code and send a short prompt.

When the request succeeds, verify the following results:

* Claude Code prints a model response.
* AISIX records a successful `POST /v1/messages` request for the selected model alias.

AISIX authenticates the caller API key, checks model access, resolves the model alias, applies configured policies, and dispatches to the upstream provider behind the alias.

Verify the request in the AISIX gateway logs. You can also use configured metrics or upstream provider logs.

## Troubleshoot Claude Code Requests[​](#troubleshoot-claude-code-requests "Direct link to Troubleshoot Claude Code Requests")

Claude Code can send Anthropic-specific fields and beta headers. Use an AISIX alias backed by a native Anthropic-protocol route for direct protocol compatibility, and validate the Claude Code workflow your team plans to deploy.

If Claude Code reports an endpoint, authentication, or model error, check these items:

| Symptom                                | Check                                                                                                                  |
| -------------------------------------- | ---------------------------------------------------------------------------------------------------------------------- |
| Authentication fails                   | Confirm `ANTHROPIC_AUTH_TOKEN` is the AISIX caller API key and that the key can access the selected model alias.       |
| Model is not found                     | Confirm `ANTHROPIC_MODEL` and `ANTHROPIC_DEFAULT_HAIKU_MODEL` are AISIX model aliases available to the caller API key. |
| Requests reach AISIX but fail upstream | Confirm the alias points to a compatible upstream model and provider key.                                              |
| Tool or beta behavior fails            | Validate whether the Anthropic-specific feature is supported by the selected AISIX route and upstream provider.        |

For endpoint behavior, see [Anthropic Messages](https://docs.api7.ai/ai-gateway/endpoints/anthropic-messages.md).

## Next Steps[​](#next-steps "Direct link to Next Steps")

* [Anthropic Messages](https://docs.api7.ai/ai-gateway/endpoints/anthropic-messages.md): review route behavior and compatibility boundaries.
* [Semantic Routing](https://docs.api7.ai/ai-gateway/routing/semantic-routing.md#pin-a-session-to-its-route): route each session by topic. Claude Code sends a session ID the gateway reads, so session affinity keeps a session on one route with no extra configuration.
* [Metrics and Usage Events](https://docs.api7.ai/ai-gateway/observability/metrics-and-usage-events.md): review gateway metrics and per-attempt usage events.
* [Guardrails](https://docs.api7.ai/ai-gateway/traffic-controls/guardrails/overview.md): add request or response policy checks.
