Skip to content
Coder Docs

Sign in to Claude Code with a Claude subscription

Sign in to Claude Code in your Coder workspace with your Claude subscription instead of an API key.

On this page

Use your Claude subscription to authenticate Claude Code in a Coder workspace, instead of an Anthropic API key. This guide is for developers who run the Claude Code CLI in a workspace terminal.

Claude Code in a workspace is the same CLI that you run on your local machine. When no other credential is set, Claude Code uses your subscription sign-in. Anthropic's authentication precedence lists the order in which Claude Code picks a credential.

You can authenticate in one of two ways:

Prerequisites

Sign in interactively

  1. Open a terminal in your workspace:

    coder ssh <workspace-name>
  2. Start the sign-in:

    claude auth login

    Claude Code prints a sign-in URL and waits for a code:

    Opening browser to sign in…
    If the browser didn't open, visit: https://claude.com/cai/oauth/authorize?...
    Paste code here if prompted >

    You can also run claude and then type /login at the Claude Code prompt.

  3. Open the sign-in URL in the browser on your local machine.

  4. Sign in with your Claude account and approve access.

  5. Copy the code that the browser shows.

  6. Paste the code at the Paste code here if prompted prompt in the workspace terminal.

  7. Confirm the sign-in:

    claude auth status

    The output shows "loggedIn": true and "authMethod": "claude.ai".

Claude Code saves your sign-in to ~/.claude/.credentials.json in the workspace. If your template does not keep your home directory between workspace restarts, sign in again after each restart, or use a long-lived token.

Forward the sign-in port

When Claude Code can open a browser on your local machine, for example from your IDE's terminal, the sign-in redirects to a localhost address instead of showing a code. That address points to a port in the workspace, such as http://localhost:39437/callback. Claude Code picks a new port for each sign-in. Some IDEs forward the port for you.

If the localhost page fails to load after you approve access:

  1. Note the port number in the localhost address.

  2. On your local machine, forward the port from the workspace and leave the command running:

    coder port-forward <workspace-name> --tcp <port>
  3. Reload the localhost page in your browser.

You can also forward the port over SSH with ssh -L.

Use a long-lived token

A long-lived token lets Claude Code authenticate without a browser, so every workspace you own can start Claude Code already signed in.

  1. Generate a token with Claude Code, on your local machine or in a workspace:

    claude setup-token

    The command opens the same sign-in flow as claude auth login and prints a token when you approve access. Claude Code does not save the token. Refer to Anthropic's long-lived token documentation for its requirements and limits.

  2. Copy the token.

  3. In the Coder dashboard, select your avatar, then select Account > Secrets.

  4. Select Add secret.

  5. Enter a Name, such as claude-code-oauth-token.

  6. Paste the token into Value.

  7. Enter CLAUDE_CODE_OAUTH_TOKEN in Environment variable.

  8. Select Save.

  9. Restart your workspace.

  10. Confirm that Claude Code reads the token:

    claude auth status --text
    Auth token: CLAUDE_CODE_OAUTH_TOKEN

Coder injects the secret into every workspace you own. To manage the secret from the CLI instead, refer to User secrets.

Set the token in a template

Template administrators can pass a token to the Claude Code module with its claude_code_oauth_token input. The module sets CLAUDE_CODE_OAUTH_TOKEN in every workspace that the template creates, so every user of the template shares the same subscription. For a token per user, have each user store it as a user secret. A user secret overrides an environment variable with the same name that the template sets.

variable "claude_code_oauth_token" {
  type      = string
  sensitive = true
}

module "claude-code" {
  source                  = "registry.coder.com/coder/claude-code/coder"
  version                 = "~> 5.2"
  agent_id                = coder_agent.main.id
  claude_code_oauth_token = var.claude_code_oauth_token
}

Set only one authentication input on the module. The module rejects claude_code_oauth_token together with enable_ai_gateway or api_key_helper.

Troubleshooting

Claude Code uses an API key instead of your subscription

When ANTHROPIC_API_KEY is set, it takes precedence over your subscription sign-in and your token. In an interactive session, Claude Code asks once whether to use the key. If the key belongs to a disabled or expired organization, requests fail with authentication errors.

  1. Check which credential Claude Code uses:

    claude auth status --text

    This output shows that the API key overrides the token:

    Auth token: CLAUDE_CODE_OAUTH_TOKEN · not in use
    API key: ANTHROPIC_API_KEY
  2. Remove the API key from your shell:

    unset ANTHROPIC_API_KEY
  3. Start Claude Code and type /status to confirm that it uses your subscription.

unset affects only the current shell. If ANTHROPIC_API_KEY returns in new shells, remove it where it's set, such as a user secret, your dotfiles, or the template. If your template sets it, ask your template administrator to remove the anthropic_api_key input from the Claude Code module.

ANTHROPIC_AUTH_TOKEN also overrides your subscription. When a template enables AI Gateway for Claude Code, Claude Code authenticates with AI Gateway through this variable.

Learn more