ThisCalculator

Everyday

Choosing a Claude Code Model & Routing via Provider

Updated · Standard formula, shown below

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-code or 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

  1. Check available models. Run claude model in your terminal. This lists all models available to your account, including aliases like opus, sonnet, and haiku.
  2. Select a model interactively. In an active Claude Code session, type /model and press Enter. Use the arrow keys to choose from the list, then press Enter to confirm. The selection applies to the current session only.
  3. Set a default model for all sessions. Create or edit ~/.claude/settings.json and add:
    {
      "model": "sonnet"
    }

    Replace sonnet with any alias or full model ID (e.g., claude-sonnet-4-20250514). This becomes the default for every new session.

  4. Override per project. Place a .claude/settings.json file in your project root with the same structure. This takes precedence over the global settings for that directory.
  5. Use environment variable. Set ANTHROPIC_MODEL to a model name. This overrides both settings files. Example: export ANTHROPIC_MODEL=haiku.

Routing Requests Through a Provider

  1. 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.
  2. Set the base URL. Claude Code uses the ANTHROPIC_BASE_URL environment 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.com

    Replace with your actual proxy URL.

  3. Provide the API key. If your proxy requires a different key, set ANTHROPIC_API_KEY accordingly. If the proxy handles authentication, you can set a dummy value like ANTHROPIC_API_KEY=not-needed.
  4. Test the connection. Run claude and send a simple message like “hi”. If the connection fails, check the proxy logs and your network settings.
  5. Configure model via provider. Some providers require you to specify the model in the request. You can set the ANTHROPIC_MODEL variable to match what your proxy expects (e.g., claude-sonnet-4-20250514).
  6. Use a configuration file for complex routing. If you need per-project routing, create a .claude/settings.json in 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_URL without export, 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_MODEL in 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 /status to see which model is active.
  • Keep settings in version control. For team projects, commit the .claude/settings.json to 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 curl to 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.