Claude Code with an API key: setup, and switching to GLM, DeepSeek or Kimi

The short version
- Claude Code runs on an API key as well as on a plan login. Four environment variables set it up, and billing becomes per token.
- Two settings break it: the key belongs in
ANTHROPIC_AUTH_TOKEN, andANTHROPIC_BASE_URLtakes no/v1. - Change
ANTHROPIC_MODELand Claude Code runs on GLM, DeepSeek or Kimi, with the same key and one bill. - Credit is prepaid, so what you have loaded is the most you can spend.
If you use Claude Code every day, you will hit your plan's usage limit sooner or later. An API key removes that limit and swaps it for pay-as-you-go. Here is how to set it up with a FastMetal key, run it on other models, and keep the spend under control.
How API key auth works in Claude Code
Claude Code authenticates one of two ways: log in with a claude.ai account and work within a Pro or Max plan, or point it at an API key and endpoint through environment variables.
When an API key is set, Claude Code uses it instead of the logged-in plan, and the plan's usage limits stop applying (per Anthropic's docs). Billing switches to per-token.
The key can come from the Anthropic console or from an AI gateway. FastMetal is an AI gateway based in Japan: one key reaches 95+ models from 25 providers. It has an Anthropic-compatible API, so Claude Code can use it as its endpoint directly.
Four environment variables
Create a FastMetal account and issue an API key. Keys start with sk- (sk-usd- on a dollar account) and are shown once, when you create them. Credit is prepaid by card, with a $5 minimum top-up. Then set:
export ANTHROPIC_AUTH_TOKEN="$FASTMETAL_API_KEY"
export ANTHROPIC_BASE_URL="https://api.fastmetal.ai"
export ANTHROPIC_MODEL="anthropic-claude-sonnet-5"
export ANTHROPIC_DEFAULT_HAIKU_MODEL="anthropic-claude-haiku-4-5"
| Variable | Value | Purpose |
|---|---|---|
ANTHROPIC_AUTH_TOKEN | Your FastMetal API key | Authentication |
ANTHROPIC_BASE_URL | https://api.fastmetal.ai | Endpoint |
ANTHROPIC_MODEL | A model id (optional) | The main model |
ANTHROPIC_DEFAULT_HAIKU_MODEL | A model id (required) | Background tasks |
Put them in ~/.zshrc or similar to keep them. Run /status inside Claude Code to see which credentials it is using. The getting started guide and the Claude Code guide cover the same steps.
The two settings that break it
Use ANTHROPIC_AUTH_TOKEN, not ANTHROPIC_API_KEY
Claude Code has two variables for a key. ANTHROPIC_API_KEY is sent in an x-api-key header, which FastMetal rejects. ANTHROPIC_AUTH_TOKEN is sent as Authorization: Bearer, which is what FastMetal expects.
If you were using an Anthropic key directly before, check that ANTHROPIC_API_KEY is gone: unset ANTHROPIC_API_KEY.
Leave /v1 off ANTHROPIC_BASE_URL
Claude Code appends /v1/messages to the base URL itself. Set it to https://api.fastmetal.ai/v1 and requests go to /v1/v1/messages, which returns a 404. Claude Code then reports that 404 as a model access problem, which sends you looking in the wrong place.
Tools that connect over the OpenAI-compatible API, such as Codex and Cline, are the opposite: their base URL includes /v1. Setup for other agents is in the guides.
Picking a model with ANTHROPIC_MODEL
ANTHROPIC_MODEL accepts any model id in the FastMetal catalog. Anthropic models carry an anthropic- prefix; everything else uses its plain id.
| Model | Model id |
|---|---|
| Claude Sonnet 5 | anthropic-claude-sonnet-5 |
| Claude Opus 5 | anthropic-claude-opus-5 |
| GLM 5.2 | glm-5.2 |
| DeepSeek V4 Pro | deepseek-v4-pro |
| Kimi K3 | kimi-k3 |
| Kimi K2.7 Code (inference in Japan) | japan-kimi-k2.7-code |
Running Claude Code on GLM, DeepSeek or Kimi
Change ANTHROPIC_MODEL. Set it on the command line and only that session uses the other model:
ANTHROPIC_MODEL="glm-5.2" claude
ANTHROPIC_MODEL="deepseek-v4-pro" claude
ANTHROPIC_MODEL="kimi-k3" claude
Z.ai and DeepSeek publish their own Claude Code setups, but each one means a separate account and a separate bill. Through FastMetal it is one key and one balance, so splitting the work (Claude for design, GLM for routine fixes) is a one-line change. The model pages for DeepSeek, GLM 5.2 and Kimi K3 describe each one. japan-kimi-k2.7-code runs its inference inside Japan.
Anthropic does not support running Claude Code on non-Claude models through a gateway (as of October 2026). Tool calling and other behavior vary by model, so try a small task first.
Always set the background model
According to Anthropic's docs, Claude Code uses a separate Haiku-class model for background tasks, chosen by ANTHROPIC_DEFAULT_HAIKU_MODEL. If the variable is empty, Claude Code sends its built-in Haiku model id, FastMetal rejects it, and the background tasks fail (confirmed October 2026). Set it to a FastMetal model id, such as anthropic-claude-haiku-4-5 or glm-5.2.
Check ids against /v1/models
List the model ids your key can use:
curl https://api.fastmetal.ai/v1/models \
-H "Authorization: Bearer $FASTMETAL_API_KEY"
Ids keep their dots (deepseek-v4.1-flash). Pages under /models replace dots with hyphens in the URL, so the end of a page URL is not the model id. OpenRouter ids use a slash (anthropic/claude-opus-5, as of October 2026); FastMetal ids use a hyphen (anthropic-claude-opus-5).
Keeping spend under control
A coding agent calls the model dozens to hundreds of times per task and resends the history on every call, so input tokens make up most of the bill. A model with prompt caching makes a real difference there.
On FastMetal you have three controls:
- Your balance is the cap. You can't be billed past the credit you bought; when it runs out, requests are refused. You get an email when the balance is low and when it is used up.
- Separate keys, separate limits. Issue several API keys and give each its own spending limit, one per project or team.
- Usage by model and by day is on the dashboard.
Per-model rates are listed per million tokens on the pricing page, and the cost calculator gives you an estimate for your workload.
Adding FastMetal as an MCP server
Everything above changes the model Claude Code runs on. You can also keep your current model provider and add FastMetal as an MCP server:
claude mcp add --transport http fastmetal https://mcp.fastmetal.ai/mcp --header "Authorization: Bearer ${FASTMETAL_API_KEY}"
That gives Claude Code tools for asking other models (ask), comparing them (compare), generating images and video (generate_image, generate_video) and analyzing images (analyze_image) in the middle of a task. It suits a second opinion or image generation on the side. Setup is in the MCP guide.
FAQ
Can I use Claude Code with an API key and no plan?
Yes. Set the API key and endpoint as environment variables and Claude Code runs without a subscription. You pay per token.
Can I switch between my Pro or Max plan and the API key?
Yes. While the API key variables are set they take priority and the plan's usage limits don't apply. Unset them and Claude Code goes back to the logged-in plan.
Can I try it for free?
Creating an account is free. There is no free credit on signup, so running Claude Code needs a top-up ($5 minimum).
Why does Claude Code say I can't access the model?
Check the end of ANTHROPIC_BASE_URL for /v1 first. With it, requests 404 and Claude Code reports that as a model access problem. Then compare ANTHROPIC_MODEL with the list from /v1/models.
Run Claude Code on FastMetal
Create a free account, add an API key and credit, and the setup above works as written. Rates are on the pricing page.