Login & API Key Setup
What you'll learn in this chapter: Choosing the right authentication method, signing in with a subscription, creating an API key, and avoiding the double-billing trap
You should complete the Installation Guide first.
First: Which One Are You?โ
The most common misconception is that you need an API key to use Claude Code. You don't.
| Your situation | Authentication | API key |
|---|---|---|
| Paid Claude subscription (Pro, Max, Team, Enterprise) | Browser sign-in | Not needed |
| No subscription, pay per usage | Claude Console API key | Required |
| Your company uses Bedrock, Vertex, or Foundry | Cloud provider credentials | Separate path |
If you have a subscription โ Pro or Max โ you do not need to create an API key. Just read "Signing In With Your Subscription" below. No console signup, no card registration, no credit purchase.
Per the official documentation, Claude Code works with Pro, Max, Team, Enterprise, and Console accounts. The free Claude.ai plan does not include Claude Code access.
Method 1: Signing In With Your Subscription (Recommended)โ
First launchโ
After installing, run this in your terminal and a browser window opens.
claude
Sign in with your Claude account and you're done. Your credentials are saved, so you won't need to sign in every time.
In WSL2, SSH sessions, or containers, the browser may not open automatically.
- Press
cto copy the login URL - Paste it into a browser and sign in
- Paste the login code the browser shows into the terminal's
Paste code here if prompted
What you paste there is a login code, not an API key.
Switching accounts and signing outโ
/login # Switch accounts or re-authenticate
/logout # Sign out
/status # Check which authentication method is currently active
Environments without a browser (CI, etc.)โ
For CI pipelines or automation scripts where browser sign-in isn't possible, you can use a subscription-based long-lived token instead of an API key.
claude setup-token
This issues a token valid for one year, used via the CLAUDE_CODE_OAUTH_TOKEN environment variable. It requires a Pro, Max, Team, or Enterprise plan.
The Trap Subscribers Fall Intoโ
If you have a subscription but also have ANTHROPIC_API_KEY set, the API key wins.
The official authentication precedence is as follows. Higher entries win.
| Priority | Authentication method |
|---|---|
| 1 | Cloud provider credentials (when Bedrock, Vertex, or Foundry is configured) |
| 2 | ANTHROPIC_AUTH_TOKEN |
| 3 | ANTHROPIC_API_KEY |
| 4 | apiKeyHelper script |
| 5 | CLAUDE_CODE_OAUTH_TOKEN |
| 6 | Subscription sign-in (/login) โ lowest |
So even while paying for a subscription, an old API key left in your environment routes billing there instead โ once you've approved it. Your subscription usage stays untouched while token-based charges accrue separately.
How to check and revertโ
/status
- If you see a
Login methodrow and noAPI keyrow โ you're on your subscription. Nothing to do. - If an
API keyrow appears โ you're being billed through that key.
If you'd rather not use the key, remove the environment variable.
unset ANTHROPIC_API_KEY # macOS/Linux, current session only
Remove-Item Env:ANTHROPIC_API_KEY # Windows PowerShell, current session only
Both clear it only in the current terminal. If you saved it permanently in .zshrc, .bashrc, or Windows user environment variables, delete it there too and open a new terminal.
In interactive mode, Claude Code asks once whether to use the key found in your environment, and remembers your choice. You can change it later with the "Use custom API key" toggle in /config (only visible when the environment variable is set). In non-interactive mode (-p), the key is always used when present.
This is not a sign-out โ removing the environment variable leaves your subscription sign-in intact.
Method 2: Creating an API Key (No Subscription)โ
If you'd rather pay only for what you use, create an API key.
Step 1: Go to the Claude Consoleโ
Visit platform.claude.com and sign up or log in.
Step 2: Create a keyโ
Go to API Keys โ Create Key โ enter a name (e.g., claude-code-personal) โ create.
Step 3: Copy and store itโ
Copy the generated key (sk-ant-api03-...). You won't be able to see it again after leaving this page, so store it somewhere safe.
Step 4: Set the environment variableโ
export ANTHROPIC_API_KEY="sk-ant-api03-..."
claude
To save it permanently:
# macOS (zsh, the default shell)
echo 'export ANTHROPIC_API_KEY="sk-ant-api03-..."' >> ~/.zshrc && source ~/.zshrc
# macOS/Linux (bash)
echo 'export ANTHROPIC_API_KEY="sk-ant-api03-..."' >> ~/.bashrc && source ~/.bashrc
# Windows PowerShell
[System.Environment]::SetEnvironmentVariable("ANTHROPIC_API_KEY", "sk-ant-api03-...", "User")
If you have a subscription and save ANTHROPIC_API_KEY permanently, you'll be routed to per-token API billing instead of your subscription, as explained above.
Understanding Pricingโ
On a subscriptionโ
There is no additional charge within your plan's usage limits. Session cost figures aren't relevant to billing.
- Limits work as a 5-hour rolling window plus a weekly window, shared across all models. Switching models does not restore access.
- Opus has its own additional limit. If you only hit the Opus limit, switch models with
/modelto keep working. - Claude Code usage shares the same limits as claude.ai on the web and Desktop.
- After exhausting your limits, you can enable usage credits with
/usage-creditsto continue at standard API rates (with configurable monthly spending caps).
The specific weekly limits for Pro and Max aren't published in the official documentation. Check claude.com/pricing for plan pricing.
On an API key (per-token billing)โ
| Model | Input (/MTok) | Output (/MTok) |
|---|---|---|
| Haiku 4.5 | $1 | $5 |
| Sonnet 5 | $2 (introductory, through 2026-08-31) | $10 |
| Sonnet 5 (from 2026-09-01) | $3 | $15 |
| Opus 4.8 | $5 | $25 |
| Fable 5 | $10 | $50 |
Models after Opus 4.7 โ including Sonnet 5 and Fable 5 โ use a new tokenizer, so the same text counts as roughly 30% more tokens. Keep this in mind when comparing costs with earlier models.
The only usage benchmark the official documentation provides is approximately $13 per developer per active day and $150โ250 per month, with 90% of users under $30 per active day (enterprise environments).
Prompt cachingโ
Caching reduces cost when you send the same context repeatedly.
- Cache reads: 0.1x the base input price (90% cheaper per input token)
- Cache writes: 1.25x (5-minute cache) / 2x (1-hour cache)
Because writes cost more upfront, a 5-minute cache pays off after one read, and a 1-hour cache after two. Caching does not apply to output tokens.
Cost managementโ
- Set a spending limit โ configure a monthly cap in the Console
- Use Haiku for light work โ
/model haiku - Check usage with
/usageโ/costis an alias for the same thing
Security Best Practicesโ
Make sure to add these to your .gitignore:
.env
*.env
Never do the following:
- Hardcode API keys in your source code
- Upload keys to public repositories on GitHub, GitLab, etc.
If your key is compromised:
- Immediately go to Console โ API Keys โ deactivate the key
- Generate a new key
- Replace the key in all environments
Key Takeawaysโ
- Subscribers (Pro, Max, Team, Enterprise) don't need an API key. Run
claude, sign in through the browser, done. - First launch opens a browser sign-in, not an API key prompt. Use
/loginto switch accounts. - If you have a subscription and
ANTHROPIC_API_KEYis set, the key takes precedence once approved โ and you get billed twice. Check with/statusand remove the variable if you don't need it. - Subscription limits are a 5-hour plus weekly window, shared with claude.ai on the web. Opus has a separate limit.
- API key billing is per token โ and the new tokenizer counts the same text as roughly 30% more tokens.
Get weekly Claude Code tips by email โ real usage insights, delivered free. Subscribe โ