Pointing Claude Code at a Different Backend per Repo on One Aggregator Key
Learn how to configure Claude Code to use different model backends on a per-repository basis while sharing a single aggregator API key. This guide covers environment variable scoping, shell wrappers, and project-level configuration files to route requests to different models without key duplication.
Why Use a Single Aggregator Key Across Repos
When you work with multiple repositories, each may have different model requirements: one project might benefit from Claude's reasoning, another from GPT's general capabilities, and a third from DeepSeek's cost efficiency. Managing separate API keys for each backend adds overhead and risk. An aggregator key lets you access multiple models through one credential, simplifying billing and access control.
How Claude Code Selects a Backend
Claude Code respects standard environment variables for API configuration. The two key variables are:
ANTHROPICBASEURL: the endpoint to which requests are sent.ANTHROPICAPIKEY: the credential used for authentication.
By default, Claude Code points to Anthropic's API. To use an aggregator, you override these variables to point to the aggregator's endpoint and supply your aggregator key.
Per-Repo Backend Selection Strategies
You can achieve per-repo backend routing in several ways, depending on your workflow.
1. Environment Variables in Shell Initialization
If you frequently switch between repositories in the same terminal session, you can set environment variables conditionally based on the current directory. For example, add logic to your shell's startup file that checks $PWD and exports different ANTHROPICBASEURL values when you cd into specific project roots.
This approach is simple but requires discipline: the variables persist until changed, so you must ensure they are set correctly before running Claude Code.
2. Project-Level Configuration Files
Many teams commit a .env file or a project-specific configuration snippet that Claude Code can read. While Claude Code does not natively load .env files automatically, you can use a wrapper script or a tool like direnv to load environment variables when entering a directory.
For example, create a .envrc file in each repository that exports the desired ANTHROPICBASEURL and ANTHROPICAPIKEY. With direnv installed, these variables are set automatically when you cd into the repo and unloaded when you leave.
3. Shell Wrapper Functions
Define shell functions that set the environment for a single command. For instance:
claude-deepseek() {
ANTHROPIC_BASE_URL="https://api.aggregator.example/v1" \
ANTHROPIC_API_KEY="$AGGREGATOR_KEY" \
claude "$@"
}
You can create one function per backend or per repository. This method gives you explicit control and avoids persistent environment changes.
4. Using a Configuration Manager
Tools like direnv, asdf, or custom scripts can manage environment variables per directory. These are especially useful if you want automatic switching without manual intervention.
Setting Up the Aggregator Key
- Obtain your aggregator API key from your provider's dashboard.
- Store it securely, e.g., in your shell's private environment or a secrets manager.
- Export it as
ANTHROPICAPIKEYwhen needed, or reference it in your per-repo configuration.
Remember that the aggregator key is used for all models; you do not need separate keys for Claude, GPT, DeepSeek, etc.
Choosing the Right Model per Repository
Different repositories may have different priorities:
- Reasoning-heavy tasks: Use Claude models for complex code analysis and generation.
- General-purpose tasks: Use GPT models for broad compatibility.
- Cost-sensitive tasks: Use DeepSeek or Qwen models for routine code completion.
- Specialized tasks: Use GLM or Kimi models for specific language or domain needs.
You can document the chosen backend in the repository's README or a CLAUDE.md file to inform collaborators.
Example: Using direnv for Automatic Switching
- Install
direnvand hook it into your shell. - In each repository, create a
.envrcfile with:
export ANTHROPIC_BASE_URL="https://api.aggregator.example/v1"
export ANTHROPIC_API_KEY="your-aggregator-key"
- Run
direnv allowin the repository to approve the file.
Now, whenever you enter the directory, the environment is set; when you leave, it is unset. Claude Code will automatically use the specified backend.
Security and Collaboration Considerations
- Never commit your aggregator key to version control. Use
.gitignorefor.envfiles and prefer secrets injection in CI/CD. - If sharing repositories, document the required environment variables without including actual keys.
- Consider using a secrets manager or environment-specific keys for different team members.
Billing and Cost Awareness
With an aggregator, you pay the official model price multiplied by 1.3. If you contribute keys to the aggregator, you receive credits at official price × 1.1 (or × 1.2 for premium models) in USDC. This means your effective cost depends on your usage and contributions. There is no net cost reduction to a small fraction of official price; the multiplier is transparent and consistent.
Conclusion
Pointing Claude Code at different backends per repository on a single aggregator key is achievable through environment variable management. Whether you use shell functions, direnv, or custom scripts, the key is to override ANTHROPICBASEURL and ANTHROPICAPIKEY appropriately. This approach streamlines access to multiple models while keeping billing and key management simple.