1. Introduction
This guide explains how to configure Claude Code with OpenRouter in Visual Studio Code (VS Code) using a dedicated VS Code profile.
The main advantage of this approach is isolation: your existing VS Code configuration, extensions, and development environment remain unchanged.
By creating a separate profile, you can maintain an independent Claude Code setup, use OpenRouter as an alternative API provider, and experiment with different supported AI models without affecting your regular development environment.
Why use a separate VS Code profile?
- Keep your existing development environment unchanged.
- Install AI coding extensions independently.
- Use different API providers and models in different profiles.
- Maintain separate configurations for different workflows.
2. Create a New VS Code Profile
First, create a dedicated profile for Claude Code.
- Open Visual Studio Code.
- Click the Manage (gear icon) button in the bottom-left corner.
- Navigate to Profiles → Create Profile.
- Enter the profile name:
Claude OpenRouter
- Select Empty Profile.
- Click Create.
An empty profile ensures that you start with a clean configuration without inheriting extensions or settings from your existing profile.
VS Code stores profile-specific extensions and settings separately.
Official documentation:
https://code.visualstudio.com/docs/configure/profiles
3. Install the Claude Code Extension
Once the new profile is active:
- Open the Extensions view using
Ctrl + Shift + X. - Search for Claude Code.
- Install the official extension published by Anthropic.
Extension marketplace:
https://marketplace.visualstudio.com/items?itemName=anthropic.claude-code
After installation, Claude Code will be available in your new VS Code profile.
4. Configure OpenRouter
Open the Command Palette:
Ctrl + Shift + P
Search for:
Preferences: Open User Settings (JSON)
Add the following configuration to the profile-specific settings.json file:
{
"claudeCode.disableLoginPrompt": true,
"claudeCode.environmentVariables": [
{
"name": "ANTHROPIC_BASE_URL",
"value": "https://openrouter.ai/api"
},
{
"name": "ANTHROPIC_API_KEY",
"value": ""
},
{
"name": "CLAUDE_CODE_ENABLE_GATEWAY_MODEL_DISCOVERY",
"value": "1"
}
],
"claudeCode.preferredLocation": "panel"
}
Important: We intentionally do not store ANTHROPIC_AUTH_TOKEN in this settings file. The OpenRouter API key will be provided through the operating system environment instead.
Configuration explanation
| Variable | Description |
|---|---|
ANTHROPIC_BASE_URL |
Directs Claude Code API requests to OpenRouter. |
ANTHROPIC_AUTH_TOKEN |
Receives the OpenRouter API key from the environment. |
ANTHROPIC_API_KEY |
Must explicitly be an empty string to prevent authentication conflicts. |
CLAUDE_CODE_ENABLE_GATEWAY_MODEL_DISCOVERY |
Enables model discovery through the gateway. |
claudeCode.disableLoginPrompt |
Disables the standard Claude login prompt. |
claudeCode.preferredLocation |
Opens Claude Code in the VS Code panel. |
The gateway model discovery option allows Claude Code to display available models provided by the gateway. It is optional.
5. Generate and Configure an OpenRouter API Key
To obtain an API key:
- Visit https://openrouter.ai/settings/keys.
- Sign in to your OpenRouter account.
- Create a new API key.
- Copy the generated key.
Configure the API key on Linux
For better security, avoid storing API keys directly in VS Code's settings.json file.
On Debian or other Linux distributions using Bash, open your shell configuration:
nano ~/.bashrc
Add the following lines:
# OpenRouter API configuration
export OPENROUTER_API_KEY="YOUR_OPENROUTER_API_KEY"
export ANTHROPIC_BASE_URL="https://openrouter.ai/api"
export ANTHROPIC_AUTH_TOKEN="$OPENROUTER_API_KEY"
export ANTHROPIC_API_KEY=""
Replace YOUR_OPENROUTER_API_KEY with your actual API key.
Save the file and reload the configuration:
source ~/.bashrc
Verify that the variables are configured without displaying the API key:
echo "Base URL: $ANTHROPIC_BASE_URL"
if [ -n "$ANTHROPIC_AUTH_TOKEN" ]; then
echo "OpenRouter authentication: Configured"
else
echo "OpenRouter authentication: Missing"
fi
Important: VS Code must inherit these environment variables. If you normally launch VS Code from the desktop application menu, close all VS Code windows and start it from a terminal after loading your shell configuration:
code
This ensures that the VS Code process and its Claude Code extension inherit the OpenRouter credentials.
The native Claude Code installation does not automatically load standard .env files. Use environment variables or another supported credential-management mechanism instead.
6. Choose an AI Model
OpenRouter provides access to multiple AI models through a unified API.
However, there is an important compatibility consideration:
Claude Code is designed around Anthropic's API and tool-use capabilities. OpenRouter guarantees compatibility with its Anthropic first-party provider. Other providers and models may not support every Claude Code feature.
For the most reliable experience, use Anthropic models through OpenRouter's first-party provider.
Examples of model aliases include:
~anthropic/claude-sonnet-latest~anthropic/claude-opus-latest~anthropic/claude-haiku-latest
Model availability and aliases may change over time.
Browse available models:
Configure model preferences
Claude Code supports environment variables for selecting different model classes.
For example, add the following to your ~/.bashrc:
export ANTHROPIC_DEFAULT_OPUS_MODEL="~anthropic/claude-opus-latest"
export ANTHROPIC_DEFAULT_SONNET_MODEL="~anthropic/claude-sonnet-latest"
export ANTHROPIC_DEFAULT_HAIKU_MODEL="~anthropic/claude-haiku-latest"
export CLAUDE_CODE_SUBAGENT_MODEL="~anthropic/claude-opus-latest"
These variables control the models used for different tasks:
| Variable | Purpose |
|---|---|
ANTHROPIC_DEFAULT_OPUS_MODEL |
Complex reasoning tasks. |
ANTHROPIC_DEFAULT_SONNET_MODEL |
General coding and development tasks. |
ANTHROPIC_DEFAULT_HAIKU_MODEL |
Faster, lightweight tasks. |
CLAUDE_CODE_SUBAGENT_MODEL |
Tasks delegated to sub-agents. |
You can also use the model discovery feature to explore available gateway models through Claude Code.
After changing environment variables, restart VS Code and Claude Code.
Inside Claude Code, use:
/model
to inspect and select available models.
7. Reduce Token Consumption with Auto-Compaction
When working with Claude Code on large development projects, conversations can become very long. Every new request may include a substantial amount of previous conversation context.
This can increase token consumption and API costs, especially when using OpenRouter with pay-per-token pricing.
One useful feature is Auto-Compaction.
What is Auto-Compaction?
Auto-Compaction automatically summarizes previous conversation context when it approaches the configured context window threshold.
Instead of continuously carrying the entire conversation history, Claude Code preserves the relevant information in a shorter summary and continues working with a reduced context.
This helps to:
- Reduce the amount of conversation history carried into subsequent requests.
- Avoid unnecessary context growth during long coding sessions.
- Keep development sessions running without manually restarting conversations.
- Potentially reduce input token consumption and API costs over long sessions.
Important: Auto-Compaction does not guarantee lower total token usage in every situation. The summarization process itself consumes tokens, and some context must still be retained. Its main benefit is reducing unnecessary context carried forward during long sessions.
Enable and manage Auto-Compaction
Auto-Compaction is enabled by default in Claude Code.
You can inspect its configuration using:
/config
You can also inspect or adjust the automatic compaction threshold:
/autocompact
To restore the model-recommended automatic threshold:
/autocompact auto
If you want to compact the current conversation manually, use:
/compact
You can also provide instructions about what information should be preserved:
/compact Keep the current implementation details, important decisions, unresolved issues, and next steps.
To inspect how much context your current conversation is using:
/context
My recommendation
Keep Auto-Compaction enabled, especially when working on large codebases or long-running development tasks.
Combining Auto-Compaction with appropriate model selection and regular context management can help control token usage and improve the efficiency of your OpenRouter workflow.
Official documentation:
https://code.claude.com/docs/en/model-config
https://code.claude.com/docs/en/costs
8. Clear Previous Anthropic Authentication
If you previously logged in to Claude Code using a direct Anthropic account, an existing authentication session may conflict with your OpenRouter configuration.
To resolve this:
- Start Claude Code.
- Execute:
/logout
- Quit Claude Code.
- Close VS Code completely.
- Restart VS Code from a terminal where the OpenRouter environment variables are available.
If you have never authenticated directly with Anthropic, you can skip this step.
9. Verify the Configuration
Open VS Code with the new profile and start Claude Code.
You can also use the integrated terminal:
claude
Once Claude Code starts, execute:
/status
A successful configuration should show information similar to:
Auth token: ANTHROPIC_AUTH_TOKEN
Anthropic base URL: https://openrouter.ai/api
This confirms that Claude Code is configured to use OpenRouter authentication and its API endpoint.
You can also inspect OpenRouter activity:
https://openrouter.ai/activity
The activity dashboard allows you to verify incoming API requests, selected models, token usage, and costs.
10. Locate the Profile Configuration
VS Code stores profile-specific settings separately.
On Linux, the profile settings are typically located at:
~/.config/Code/User/profiles/<PROFILE_ID>/settings.json
For example:
/home/ersin/.config/Code/User/profiles/66897934/settings.json
You can inspect the configuration using:
cat ~/.config/Code/User/profiles/66897934/settings.json
The profile ID may differ between installations.
You can also locate the active profile settings through the VS Code Command Palette:
Preferences: Open User Settings (JSON)
11. Security Recommendations
When using OpenRouter with Claude Code, consider the following recommendations:
- Never commit API keys to Git repositories.
- Avoid storing API keys directly in
settings.json. - Use separate API keys for different environments when practical.
- Monitor API usage and spending through the OpenRouter dashboard.
- Set appropriate usage limits for your API key.
- Restrict access to shell configuration files containing credentials.
- Rotate your API key immediately if it is accidentally exposed.
For additional protection, consider using an operating-system credential manager or a supported secrets-management solution.
12. Conclusion
Using a dedicated VS Code profile provides a clean and isolated environment for running Claude Code through OpenRouter.
This setup makes it possible to:
- Keep your existing VS Code environment unchanged.
- Use OpenRouter as an alternative API provider.
- Experiment with different compatible AI models.
- Manage Claude Code settings independently.
- Monitor API usage and costs through OpenRouter.
- Reduce unnecessary context consumption with Auto-Compaction.
- Maintain separate development environments for different AI workflows.
The same approach can be adapted to other AI coding extensions and API providers, provided that the extension supports the corresponding provider and authentication configuration.
Top comments (0)