Skip to content
AI GatewayAI Gateway Manual

Connect Claude Code to AI Gateway

How to connect Claude Code CLI to AI Gateway, set the Gateway Key and environment variables, and choose a model.

This guide explains how to connect Claude Code CLI to AI Gateway and choose the model to use.

Follow steps 1-3 below to use a Provider API key registered by your administrator. To use your own Claude subscription, follow Use a personal Claude subscription. Claude Code launched with your subscription can also select models from other Providers the project allows. Those requests use the credentials your administrator registered for that Provider. A request that fails in one mode does not switch to the other automatically.

  • Install Claude Code CLI.
  • Issue a Gateway Key from the Gateway Keys menu of the project you want to use. See Issue a Gateway Key for the steps.
  • Check that the model you want appears in the Models menu of the same project. That list is the models allowed in the project.

To configure in the terminal, set the Gateway Key as an environment variable in the terminal where you run Claude Code. Example for bash/zsh on macOS or Linux. If you use the settings file instead, you can skip this step.

Terminal window
export AI_GATEWAY_API_KEY='<your Gateway Key>'

A new terminal may not have this setting. To keep it, set AI_GATEWAY_API_KEY in your shell configuration or secret manager.

In AI Gateway, open the project you want to use, then open Models in the top menu. In the row of the model you want, select the gear icon in the Installation column and check the Gateway base URL and model ID.

To configure in the terminal, run the commands below in the same terminal as step 1. Replace <gateway-host> and <provider-name>/<model-id> to match the Gateway base URL and model ID you checked above.

Terminal window
export ANTHROPIC_BASE_URL='https://<gateway-host>/anthropic'
export ANTHROPIC_API_KEY="$AI_GATEWAY_API_KEY"
export ANTHROPIC_MODEL='<provider-name>/<model-id>'
export CLAUDE_CODE_ENABLE_GATEWAY_MODEL_DISCOVERY=1

ANTHROPIC_BASE_URL includes the Gateway’s /anthropic path. ANTHROPIC_API_KEY passes the Gateway Key from step 1. ANTHROPIC_MODEL is the model Claude Code starts with. With CLAUDE_CODE_ENABLE_GATEWAY_MODEL_DISCOVERY=1, Claude Code asks the Gateway for its model list. For which models appear in the /model list, see Limitations.

To save the configuration in a file, put the same values in the env field of ~/.claude/settings.json. If the file already exists, keep the other settings and merge only the env field. This user setting applies to every Claude Code project on this computer. The Code tab in Claude Desktop also reads this file, but when Claude Desktop is signed in with an Anthropic account, the app sets the Anthropic address itself, so requests from the Code tab do not go through the Gateway.

{
"env": {
"ANTHROPIC_BASE_URL": "https://<gateway-host>/anthropic",
"ANTHROPIC_API_KEY": "<your Gateway Key>",
"ANTHROPIC_MODEL": "<provider-name>/<model-id>",
"CLAUDE_CODE_ENABLE_GATEWAY_MODEL_DISCOVERY": "1"
}
}

Replace <your Gateway Key> with the actual key value. In JSON, "$AI_GATEWAY_API_KEY" is not expanded into the key value the way a shell command is. To apply the settings to one project only, you can use the project’s .claude/settings.local.json; if you create it yourself, make sure it is not committed to Git.

Go to the directory you want to work in and run Claude Code. If you configured in the terminal, use the terminal where the environment variables are set.

Terminal window
claude

If Claude Code was already running, quit it and start it again. If you are asked to confirm the API key, approve using the Gateway Key. Run /model inside Claude Code to check the current model and the model list. Also send a short request and check that a response comes back.

You can also start Claude Code with a model ID given directly by the --model option. The model you give overrides ANTHROPIC_MODEL for that run only.

Terminal window
claude --model '<provider-name>/<model-id>'

Official Claude Code sends requests using your own signed-in subscription. The Gateway applies Project permissions and usage limits, then forwards the request’s OAuth token to Anthropic. Claude Code manages login, renewal, and logout. You do not register a personal token with the Gateway.

A personal subscription works only in Claude Code launched from a terminal or from Companion. It does not work in Claude Desktop.

  • When Claude Desktop is signed in with an Anthropic account, its Code tab launches Claude Code with the Anthropic address set by the app. ANTHROPIC_BASE_URL in settings.json does not apply, and requests do not go through the Gateway.
  • The Gateway setting under Claude Desktop’s Third-Party Inference authenticates only with a Gateway Key. Requests sent through it use the Provider credentials your administrator registered, not your personal subscription.

In Claude Desktop, only the method that runs with a Provider API key registered by your administrator is available. Set up Claude Desktop’s Gateway connection with Companion. See Connect Claude Desktop to AI Gateway.

Do not put personal subscription settings in env of ~/.claude/settings.json. The Code tab in Claude Desktop also reads this file. When Claude Desktop is signed in with an Anthropic account, the Code tab ignores the address setting but applies ANTHROPIC_CUSTOM_HEADERS, so the Gateway Key header is sent to Anthropic. Set the values as environment variables in the terminal that runs Claude Code, as in step 3.

Ask your administrator for an Anthropic Provider configured with Personal Claude subscription authentication and the model ID to use. This Provider has no server API key, and its models are registered from AI Gateway’s built-in Claude model list. The model must also appear in the Project’s Models menu. Registering a Provider does not verify your subscription login or model access.

Quit Claude Code and review the terminal environment and applicable settings.json files. Existing ANTHROPIC_API_KEY, ANTHROPIC_AUTH_TOKEN, apiKeyHelper, Bedrock, or other cloud authentication settings may take precedence over your subscription login. Confirm which authentication mode you want and remove conflicting settings yourself. Check user settings, the project’s .claude/settings.json and .claude/settings.local.json, and organization-managed settings.

Replace the Gateway address and model ID in this example, then run it in the same terminal. AI_GATEWAY_API_KEY is the Project Gateway Key you issued earlier.

Terminal window
export ANTHROPIC_BASE_URL='https://<gateway-host>/anthropic'
export ANTHROPIC_CUSTOM_HEADERS="${ANTHROPIC_CUSTOM_HEADERS:+${ANTHROPIC_CUSTOM_HEADERS}
}x-ai-gateway-api-key: ${AI_GATEWAY_API_KEY}"
export ANTHROPIC_MODEL='<subscription-provider>/<raw-claude-model-id>'
claude

This example appends the Gateway Key header to existing custom headers. Before running it, check that the existing custom headers contain neither the same header nor Authorization or x-api-key. Avoid adding duplicate headers when running the setup again. env.ANTHROPIC_CUSTOM_HEADERS in settings.json overrides the terminal value, so review and resolve that setting yourself as well. Do not put the Gateway Key in ANTHROPIC_API_KEY or ANTHROPIC_AUTH_TOKEN.

In auto mode, Claude Code sends a separate request to check whether a tool may run before running it. This check requests a Sonnet model ID without a Provider name, which the Gateway cannot resolve, so Claude Code sends the same check again with the model in ANTHROPIC_MODEL. The check keeps working, but every tool run uses that model’s subscription usage. To use Sonnet for the check, ask your administrator to register a Sonnet model in the subscription Provider and allow it in the Project. Then set this value in the same terminal as well.

Terminal window
export ANTHROPIC_DEFAULT_SONNET_MODEL='<subscription-provider>/<raw-claude-sonnet-model-id>'

Use only the <subscription-provider>/<model-id> form, with the Provider name, for this value.

4. Sign in with the official client and check a response

Section titled “4. Sign in with the official client and check a response”

Run /login in Claude Code and sign in with your own subscription account. Specify an allowed model with /model or --model '<subscription-provider>/<raw-claude-model-id>', then send a short request. If a response asks you to renew your login, use Claude Code’s official login flow. The Gateway does not switch to another user’s token or a server API key.

Claude Code versions that authenticate to a Gateway using only custom headers may skip model discovery. A working Gateway model-list API does not guarantee that models appear automatically in /model. Specify the model ID directly instead of adding an API key or apiKeyHelper to force discovery.

Gateway costs for subscription requests are estimates based on API list prices and are not deducted from Project budgets. Project request and token limits still apply to subscription requests. Check your Claude account for subscription charges and usage limits. In Model Logs, open a request to see Personal Claude subscription under Authentication source in its details.

Claude Code launched with your subscription can also use models from other Providers the project allows, such as GPT, Gemini, or Bedrock.

Requests to another Provider’s model do not use your personal OAuth token. The Gateway sends them with the credentials your administrator registered for that Provider, and the cost is charged to your organization’s Provider account. Model Logs shows Gateway-managed credentials under Authentication source. When you switch back to a subscription model, requests use your own subscription again.

In a conversation, enter /model followed by the model ID to switch to that model right away. You do not need to restart Claude Code, and the conversation continues.

/model <provider-name>/<model-id>

Claude Code also saves the model you choose this way in ~/.claude/settings.json as the default for new sessions. In a terminal where ANTHROPIC_MODEL is set, as in step 3, the next launch starts with the ANTHROPIC_MODEL model again. In a terminal without it, the next launch starts with the saved model, so choose your subscription model again with /model to go back.

For a model ID it does not recognize, Claude Code shows a notice that it assumes a 200k-token context window. If the model’s actual context window differs, set the value as described in Claude Code model configuration.

To choose a model you use often without typing its ID, add it to the /model list with modelPicker.options in ~/.claude/settings.json. Set model to the model ID you would enter after /model, and label to the name to show in the list. If the file already exists, add only the modelPicker entry instead of overwriting the file.

{
"modelPicker": {
"options": [
{ "model": "<provider-name>/<model-id>", "label": "<name shown in the list>" }
]
}
}

The models you add appear below the built-in Claude models in the /model list. Choosing one with Enter also saves it as the default for new sessions; choosing it with s applies it to the current session only. Claude Code reads this setting only from user settings and settings your organization manages. It ignores the setting in a project’s .claude/settings.json.

To start with another model from the beginning, run claude --model '<provider-name>/<model-id>'. This applies only to that launch and does not change the default model.

If you launch with Companion, use Companion’s personal subscription settings. Sign in separately with the official client in Companion’s isolated environment too.

For authentication and model discovery behavior, see Claude Code subscriptions and gateways and the Gateway protocol.

Of the models the Gateway returns, Claude Code automatically lists in /model only those whose ID contains claude or anthropic. Claude Code leaves other models out of the list even when the Gateway returns them.

To use such a model, give its model ID directly in one of these ways:

  • Enter /model <provider-name>/<model-id> in a conversation to switch right away.
  • Add the model to modelPicker.options in ~/.claude/settings.json and choose it from the /model list. See Use models from other Providers for an example.
  • Set ANTHROPIC_MODEL to the model ID, with export in the terminal or in env of ~/.claude/settings.json. Claude Code then starts with that model.
  • Give the model when you start Claude Code: claude --model '<provider-name>/<model-id>'.

Models of a Provider that connects ChatGPT accounts (type openai_codex) cannot be used in Claude Code, even when you give the model directly.

  1. If you configured in the terminal, check that AI_GATEWAY_API_KEY and ANTHROPIC_API_KEY are both set in the same terminal. If you use the settings file, check that ANTHROPIC_API_KEY in env is the actual Gateway Key, then restart Claude Code.
  2. Check that ANTHROPIC_BASE_URL ends with the Gateway’s /anthropic path and that CLAUDE_CODE_ENABLE_GATEWAY_MODEL_DISCOVERY is 1.
  3. Check in the AI Gateway Models menu that the model is allowed in the project. A Gateway Key belongs to the project it was issued in, so another project’s key gives a different list.
  4. If the model list request returns a 404 error, ask the administrator to check that /anthropic/v1/models is deployed in the environment you connect to and that the path is routed to the Gateway.
  5. If the list request succeeds but a model is missing from /model, check whether its ID contains claude or anthropic. If not, Claude Code leaves it out of the automatic list, so give the model directly as described in Limitations.

For what each environment variable means and how model IDs are filtered, see the Claude Code environment variables guide and the gateway model discovery description.