How to use Claude Code with OpenRouter

OpenRouter has an official Claude Code integration. Three environment variables and a logout are all it takes—plus a couple of gotchas worth knowing before you start.

The configuration

From OpenRouter's Claude Code guide:

Terminal
export OPENROUTER_API_KEY="your-openrouter-key"
export ANTHROPIC_BASE_URL="https://openrouter.ai/api"
export ANTHROPIC_AUTH_TOKEN="$OPENROUTER_API_KEY"
export ANTHROPIC_API_KEY=""
  • The base URL is https://openrouter.ai/api—no /v1.
  • ANTHROPIC_API_KEY must be explicitly empty to avoid conflicts.
  • Define OPENROUTER_API_KEY before referencing it, or the token ends up blank and you get an auth error.

Step by step

  1. Create an OpenRouter key

    Add credits and create a key in your OpenRouter dashboard.

  2. Set the variables

    Use the exports above, or put the same values under env in ~/.claude/settings.json or .claude/settings.local.json. Don't rely on a project .env file—the native install doesn't read it.

  3. Log out of Anthropic

    If you previously signed in, run /logout, quit, and start claude again.

  4. Verify

    /status should show Auth token: ANTHROPIC_AUTH_TOKEN and Anthropic base URL: https://openrouter.ai/api.

Things to know

OpenRouter states Claude Code "is only guaranteed to work with the Anthropic first-party provider." Routing to other providers or non-Anthropic models may not work correctly.

  • Fast mode is only available when requests are routed to Anthropic first-party.
  • OpenRouter can fail over between providers; pin them with provider.order or provider.only if you need consistency.
  • Using a gateway disables Anthropic's Remote Control and cloud sessions, which require a claude.ai login.

Run it on AlwaysAgent

AlwaysAgent gives you a dedicated Linux server with Claude Code and Codex preinstalled. Paste your OpenRouter key in settings and the workspace configures the variables for you. Your key stays on your server; we never proxy your requests or add a markup, and your tasks keep running after you close the browser.

FAQ

Why does Claude Code say my OpenRouter key is invalid?
Usually ANTHROPIC_API_KEY isn't blank, the key variable was referenced before being defined, or an old Anthropic login is still active. Blank the API key, run /logout, restart, and check /status.
Can I use non-Claude models through OpenRouter in Claude Code?
You can point the model aliases at other models, but OpenRouter only guarantees Claude Code with the Anthropic first-party provider. Test before relying on it.
Can I use Codex with OpenRouter too?
Codex uses its own config: define a custom provider in ~/.codex/config.toml with a base_url and env_key. AlwaysAgent sets up Claude Code and Codex separately.

Skip the setup

AlwaysAgent runs Claude Code on your own cloud server. Paste your OpenRouter key in settings and work from any browser, including your phone.

Try it for $0.05/h →