Claude Code with an API key
Claude Code can run on a subscription login or on an API key billed per token. This guide shows both routes, the exact environment variables, and how to fix the usual login conflicts. Setup takes about five minutes.
Subscription login or API key
Claude Code accepts three kinds of credentials. Which one it uses depends on what is set in your environment, so it helps to know the difference before you paste anything.
| Route | Credential | Billing | Variables |
|---|---|---|---|
| Subscription | claude.ai login via /login | Flat monthly plan with usage limits | none |
| Console API key | Key from the Claude Console | Per token on your Anthropic account | ANTHROPIC_API_KEY |
| Gateway key | Key from a gateway such as OneProvider | Per token from a prepaid balance | ANTHROPIC_BASE_URL and ANTHROPIC_AUTH_TOKEN |
At the time of writing (November 2026) Pro is listed at $20 a month ($17 with annual billing), Max at $100 and Ultra at $200. Check claude.com/pricing for current numbers. With a subscription, Claude Code shares the usage limit with the chat. With an API key there is no plan limit: you pay for the tokens you use.
When several credentials exist, Claude Code picks one in a fixed order: cloud provider variables, then ANTHROPIC_AUTH_TOKEN, then ANTHROPIC_API_KEY, then apiKeyHelper, and only then your subscription login.
Use a Claude Console API key
This is the direct route: you pay Anthropic per token and the key belongs to your Console account. You need a Console account with billing credit added.
Create a key in the Console
Open platform.claude.com, go to Settings, then API Keys, and click Create Key. Copy it at once, because it is shown only one time.
Export the key
Set it in the terminal where you will start Claude Code.
export ANTHROPIC_API_KEY=sk-...Start Claude Code and approve the key
On first launch in interactive mode Claude Code asks whether to use the key. Choose yes. The choice is remembered and can be changed later in /config.
claudeConfirm which credential is active
Run /status inside Claude Code. It shows the login method in use and marks the credential that is not used. To go back to your subscription, run unset ANTHROPIC_API_KEY.
Use a OneProvider key instead
OneProvider is a pay-as-you-go gateway with a native Anthropic Messages endpoint, so Claude Code talks to it without any adapter. You top up one USD balance, issue a key with its own name, spend limit and term, and point Claude Code at the endpoint.
Get a key
Open app.oneprovider.dev or the Telegram bot @oneprovider_robot, top up the balance, then press Issue a key. Three keys are available by default.
Install Claude Code
Use the installer for your system, then check the version.
# macOS and Linux curl -fsSL https://claude.ai/install.sh | bash # Windows PowerShell irm https://claude.ai/install.ps1 | iex claude --versionSet the endpoint and the key
Use the root URL without /v1. The values last for the current terminal session.
export ANTHROPIC_BASE_URL=https://api.oneprovider.dev export ANTHROPIC_AUTH_TOKEN=sk-...Keep it for every session
Put the same two values in the env block of ~/.claude/settings.json (on Windows, %USERPROFILE%\.claude\settings.json), then open a new session.
{ "env": { "ANTHROPIC_BASE_URL": "https://api.oneprovider.dev", "ANTHROPIC_AUTH_TOKEN": "sk-..." } }Check that it works
Run a one-line request without opening the interactive screen.
claude -p "say hi in one sentence"
Choose a model and keep costs under control
Claude Code uses the model you pick with the --model flag or the ANTHROPIC_MODEL variable. Larger models cost more per token, and a coding session sends a lot of context, so the model choice is the biggest cost lever.
- Claude Sonnet 5.5: Input / 1M tokens $2.00, Output / 1M tokens $10.00, Context 1M
- Claude Opus 5.5: Input / 1M tokens $4.00, Output / 1M tokens $20.00, Context 1M
- Claude Haiku 5.5: Input / 1M tokens $0.10, Output / 1M tokens $0.50, Context 1M
The balance does not expire and there is no subscription fee.
- Issue one key per machine or project and set a spend limit on each key in the cabinet.
- Keys are valid for up to 90 days, so put the expiry date in your calendar.
- Start new tasks in a fresh session: long histories are sent with every request.
- Never commit the key to a repository. Keep it in your shell profile or in the user-level settings file.
Fix common errors
| Symptom | Likely cause | Fix |
|---|---|---|
| Auth conflict warning | A saved claude.ai login and a key are both present | Run /logout. The variables stay in place. |
| Login screen still appears | Variables set in another terminal | Set them in this session or in settings.json, then start a new session. |
| Not found or path errors | Base URL ends with /v1 | Use https://api.oneprovider.dev for Claude Code. |
| Streaming stops mid answer | A VPN or proxy cuts the stream | Turn the VPN or proxy off and retry. |
| Requests start failing later | Key term ended or spend limit reached | Check the key in the cabinet and issue a new one. |
Frequently asked questions
Can Claude Code use an API key instead of a subscription?
Yes. Set ANTHROPIC_API_KEY with a Console key, or ANTHROPIC_AUTH_TOKEN with a gateway key. Requests are then billed per token and the subscription usage limits do not apply to them.
What is the difference between ANTHROPIC_API_KEY and ANTHROPIC_AUTH_TOKEN?
ANTHROPIC_API_KEY is sent as an x-api-key header and is meant for direct Anthropic access. ANTHROPIC_AUTH_TOKEN is sent as a Bearer token and is meant for gateways. The token takes precedence when both are set.
Why does Claude Code still use my subscription?
Usually only the base URL was set, or the key was never approved. Set the credential variable too, check /status, and run /logout if an Auth conflict warning appears.
Does the key work in the VS Code extension?
Yes. The extension shares ~/.claude/settings.json with the CLI, so the same env block applies. If it still shows a sign-in screen, set claudeCode.disableLoginPrompt in the VS Code user settings.
Can I use GPT or Gemini models inside Claude Code?
Claude Code is built for Claude models. For GPT models use a client built for the OpenAI API, such as Codex CLI. One OneProvider key works on both endpoints.
How much does Claude Code cost with an API key?
You pay input and output tokens at the model rate, so the total depends on the model and on how much context your sessions send. Multiply tokens by the per-million rate in the price table above.
Claude, ChatGPT, GPT, Cursor and other product names are trademarks of their owners and are used only to describe compatibility. We do not give legal advice: following local rules stays with the user.