Choosing a Claude Code Model & Routing via Provider
This guide explains how to select a specific AI model for Claude Code (Anthropic’s terminal-based coding assistant) and how to route your requests through an alternative provider (such as a proxy or gateway) when you need a different model, custom configuration, or centralized management. You’ll learn the exact commands and configuration files to control both model choice and request routing.
What You’ll Need
- Claude Code installed (via
npm install -g @anthropic-ai/claude-codeor the native installer). - An Anthropic API key or an API key from your provider (if routing through a proxy).
- Basic familiarity with the terminal and environment variables.
Step-by-Step
Choosing a Model
- Check available models. Run
claude modelin your terminal. This lists all models available to your account, including aliases likeopus,sonnet, andhaiku. - Select a model interactively. In an active Claude Code session, type
/modeland press Enter. Use the arrow keys to choose from the list, then press Enter to confirm. The selection applies to the current session only. - Set a default model for all sessions. Create or edit
~/.claude/settings.jsonand add:{ "model": "sonnet" }Replace
sonnetwith any alias or full model ID (e.g.,claude-sonnet-4-20250514). This becomes the default for every new session. - Override per project. Place a
.claude/settings.jsonfile in your project root with the same structure. This takes precedence over the global settings for that directory. - Use environment variable. Set
ANTHROPIC_MODELto a model name. This overrides both settings files. Example:export ANTHROPIC_MODEL=haiku.
Routing Requests Through a Provider
- Understand routing. Routing means sending your Claude Code requests to a server that then forwards them to Anthropic (or another backend). This is useful for using a different model, adding logging, or managing API keys centrally.
- Set the base URL. Claude Code uses the
ANTHROPIC_BASE_URLenvironment variable to point to a custom endpoint. For example, if you use a proxy like LiteLLM or a corporate gateway, set:export ANTHROPIC_BASE_URL=https://your-proxy.example.comReplace with your actual proxy URL.
- Provide the API key. If your proxy requires a different key, set
ANTHROPIC_API_KEYaccordingly. If the proxy handles authentication, you can set a dummy value likeANTHROPIC_API_KEY=not-needed. - Test the connection. Run
claudeand send a simple message like “hi”. If the connection fails, check the proxy logs and your network settings. - Configure model via provider. Some providers require you to specify the model in the request. You can set the
ANTHROPIC_MODELvariable to match what your proxy expects (e.g.,claude-sonnet-4-20250514). - Use a configuration file for complex routing. If you need per-project routing, create a
.claude/settings.jsonin your project and add:{ "env": { "ANTHROPIC_BASE_URL": "https://proxy.example.com", "ANTHROPIC_MODEL": "sonnet" } }This sets environment variables automatically when Claude Code starts in that directory.
Common Mistakes
- Using the wrong model name. Providers may use different aliases. Always check your provider’s documentation for the exact model ID.
- Forgetting to export the environment variable. If you set
ANTHROPIC_BASE_URLwithoutexport, it won’t be available to child processes. - Overriding settings incorrectly. The order of precedence is: environment variable > project settings > global settings. If you set
ANTHROPIC_MODELin your shell, it will override any JSON config. - Assuming the proxy works without testing. Always test with a simple request before starting a heavy session.
Tips
- Use aliases for quick switching. Create shell aliases like
alias claude-opus='ANTHROPIC_MODEL=opus claude'to quickly start with a specific model. - Check current model. In a session, type
/statusto see which model is active. - Keep settings in version control. For team projects, commit the
.claude/settings.jsonto ensure everyone uses the same model and routing. - Use a local proxy for debugging. Tools like LiteLLM can log requests and responses, helping you debug issues.
Troubleshooting
- Connection refused. Check that the proxy URL is correct and reachable. Try
curlto the base URL. - Authentication errors. Verify the API key is set and valid. If using a proxy, ensure the proxy’s key requirements are met.
- Model not found. The provider may not support the model you specified. Check the provider’s model list and update
ANTHROPIC_MODEL. - Slow responses. If routing through a proxy, network latency may increase. Check the proxy’s performance and consider using a closer server.
- Settings not applied. Ensure the JSON is valid and the file is in the correct location. Restart Claude Code after editing.
Frequently Asked Questions
Can I use a model that isn’t from Anthropic?
Yes, if your proxy supports it. Claude Code sends requests to the base URL; the proxy can translate them to any model API. However, some features may not work if the model doesn’t support tool use.
How do I know which model is currently active?
Type /status in the session. It shows the model name and other session details.
What’s the difference between /model and ANTHROPIC_MODEL?
/model changes the model for the current session only. ANTHROPIC_MODEL sets a default for all sessions (unless overridden by /model).
Can I route only some requests through a proxy?
Not directly. You can switch between direct and proxied by changing the environment variable and restarting Claude Code. For more granular control, you’d need a custom proxy that filters requests.
Is it safe to use a public proxy?
Only if you trust it. Your code and prompts are sent through the proxy. Use a proxy you control or a reputable service with clear privacy policies.
What if I get a ‘model not found’ error after setting ANTHROPIC_MODEL?
Check the exact model ID. Some aliases like ‘sonnet’ may not be recognized by your proxy. Use the full model ID (e.g., claude-sonnet-4-20250514).
Related
Method: the standard formula for this calculation, rounded as each result states. See how the calculators work or report an error.