Skip to main content

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

Prerequisites

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 situationAuthenticationAPI key
Paid Claude subscription (Pro, Max, Team, Enterprise)Browser sign-inNot needed
No subscription, pay per usageClaude Console API keyRequired
Your company uses Bedrock, Vertex, or FoundryCloud provider credentialsSeparate path
Subscribers can stop here

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.

The free plan doesn't include Claude Code

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.


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.

When the browser doesn't open

In WSL2, SSH sessions, or containers, the browser may not open automatically.

  1. Press c to copy the login URL
  2. Paste it into a browser and sign in
  3. 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.

PriorityAuthentication method
1Cloud provider credentials (when Bedrock, Vertex, or Foundry is configured)
2ANTHROPIC_AUTH_TOKEN
3ANTHROPIC_API_KEY
4apiKeyHelper script
5CLAUDE_CODE_OAUTH_TOKEN
6Subscription 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 method row and no API key row โ†’ you're on your subscription. Nothing to do.
  • If an API key row 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.

How interactive mode behaves

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")
Subscribers should skip this

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 /model to 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-credits to continue at standard API rates (with configurable monthly spending caps).
Exact per-plan limits

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)โ€‹

ModelInput (/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
Token counting has changed

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โ€‹

  1. Set a spending limit โ€” configure a monthly cap in the Console
  2. Use Haiku for light work โ€” /model haiku
  3. Check usage with /usage โ€” /cost is 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:

  1. Immediately go to Console โ†’ API Keys โ†’ deactivate the key
  2. Generate a new key
  3. 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 /login to switch accounts.
  • If you have a subscription and ANTHROPIC_API_KEY is set, the key takes precedence once approved โ€” and you get billed twice. Check with /status and 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

Get weekly Claude Code tips by email โ€” real usage insights, delivered free. Subscribe โ†’