Claude.ai OAuth (default)
When you runclaude for the first time without any API key configured, Claude Code starts an OAuth flow using your claude.ai account.
1
Run Claude Code
Open your terminal and run:
2
Follow the login prompt
Claude Code will display a URL and prompt you to open it in your browser. Visit the URL, sign in to your claude.ai account, and grant authorization.
3
Return to the terminal
After authorizing in the browser, Claude Code receives the OAuth token automatically and stores it in secure storage (macOS Keychain on macOS, or a credentials file on other platforms). You’re now authenticated.
OAuth tokens are refreshed automatically before they expire. You do not need to re-authenticate unless you explicitly log out or revoke access.
API key
You can authenticate using an Anthropic API key instead of OAuth.- Environment variable
- Settings file
Set the When this variable is set, Claude Code uses it directly and does not prompt for OAuth.
ANTHROPIC_API_KEY environment variable in your shell profile or before running claude:AWS Bedrock
To use Claude through Amazon Bedrock, set theCLAUDE_CODE_USE_BEDROCK environment variable and configure your AWS credentials.
1
Enable Bedrock mode
2
Configure AWS credentials
Claude Code uses the standard AWS credential chain. Any of the following work:
- AWS credentials file (
~/.aws/credentials) - Environment variables:
AWS_ACCESS_KEY_ID,AWS_SECRET_ACCESS_KEY,AWS_SESSION_TOKEN - IAM roles (EC2 instance profiles, ECS task roles, etc.)
- AWS SSO (
aws sso login)
3
Set your region and model (optional)
Automated AWS credential refresh
If your AWS session expires mid-session (for example, with short-lived SSO tokens), configureawsAuthRefresh in your settings to run a command that refreshes credentials automatically:
aws sts assume-role), use awsCredentialExport:
Credentials.AccessKeyId, Credentials.SecretAccessKey, and Credentials.SessionToken).
GCP Vertex AI
To use Claude through Google Cloud Vertex AI, set theCLAUDE_CODE_USE_VERTEX environment variable and configure Application Default Credentials.
1
Enable Vertex mode
2
Configure GCP credentials
Claude Code uses Google Application Default Credentials (ADC). Any of the following work:
gcloud auth application-default login(for interactive use)- Service account key file via
GOOGLE_APPLICATION_CREDENTIALS - Workload Identity (for GKE)
3
Set your GCP project and region (optional)
Automated GCP credential refresh
Similar to Bedrock, configuregcpAuthRefresh to run a command when credentials expire:
Switching accounts
Log in to a different account
Run the/login command from within Claude Code to start a new OAuth flow. This replaces any stored tokens with those for the new account:
Log out
Run the/logout command to remove stored credentials:
Token expiry and refresh
OAuth tokens expire automatically. Claude Code handles refresh silently:- Before each API request, Claude Code checks whether the access token is expired.
- If it is, Claude Code acquires a lock and refreshes using the stored refresh token.
- Multiple concurrent Claude Code instances coordinate via a lock file to avoid redundant refreshes.
- If a
401response arrives from the API (for example, due to a clock skew between when the token was issued and the local check), Claude Code forces an immediate refresh without waiting for the local expiry time.
/login.
Authentication priority
When multiple authentication sources are configured, Claude Code resolves them in this order:ANTHROPIC_AUTH_TOKENenvironment variableCLAUDE_CODE_OAUTH_TOKENenvironment variable- OAuth token from file descriptor (for managed deployments)
apiKeyHelperfrom settings- Stored claude.ai OAuth tokens (keychain or credentials file)
ANTHROPIC_API_KEYenvironment variable