Using LiteLLM as a Unified Model Gateway for Claude Code
This guide shows how to run a local LiteLLM proxy so Claude Code can route requests to models from multiple providers through one endpoint. It covers installation and configuration, verifying each connection step, end-to-end testing, switching models, troubleshooting, and cost and security practices.
1. Who This Guide Is For and Prerequisites
This guide is for developers who already use Claude Code and want to manage models from several providers through a single entry point. Before you start, prepare the following:
- A computer that can run Python. macOS, Linux, and Windows are all supported.
- An API key from at least one model provider.
- Claude Code installed.
For version requirements of Python, LiteLLM, and Claude Code, see the official documentation for each tool.
2. Install LiteLLM and Write the Configuration
Create a separate virtual environment for the proxy. This keeps its dependencies from conflicting with other Python projects.
macOS or Linux:
python -m venv .venvsource .venv/bin/activatepip install 'litellm[proxy]'
Windows PowerShell:
python -m venv .venv.\.venv\Scripts\Activate.ps1pip install 'litellm[proxy]'
Create a file named config.yaml in the project directory. The model_name value is the name Claude Code will use. The litellm_params.model value is the model ID from your provider. The model IDs below are placeholders, so replace them with the actual values from your provider’s documentation. The prefix before each model ID tells LiteLLM which protocol to use. The LiteLLM documentation lists the supported providers and their prefixes.
model_list:- model_name: coding-mainlitellm_params:model: anthropic/<primary-model-ID>api_key: os.environ/ANTHROPIC_API_KEY- model_name: coding-backuplitellm_params:model: openai/<backup-model-ID>api_key: os.environ/OPENAI_API_KEYgeneral_settings:master_key: os.environ/LITELLM_MASTER_KEY
You define the proxy’s master key yourself. LiteLLM requires it to start with sk-, for example sk-local- followed by a random string. Set the upstream provider keys as environment variables before you start the proxy.
macOS or Linux:
export ANTHROPIC_API_KEY="<your-provider-key>"export OPENAI_API_KEY="<your-backup-provider-key>"export LITELLM_MASTER_KEY="sk-local-<random-string>"
Windows PowerShell:
$env:ANTHROPIC_API_KEY = "<your-provider-key>"$env:OPENAI_API_KEY = "<your-backup-provider-key>"$env:LITELLM_MASTER_KEY = "sk-local-<random-string>"
3. Start the Proxy and Verify the Connection
Start the proxy inside the virtual environment, and keep this terminal running:
litellm --config config.yaml --host 127.0.0.1 --port 4000
In a second terminal, confirm that the proxy can list models. The returned list should include coding-main and coding-backup:
curl.exe http://127.0.0.1:4000/v1/models -H "Authorization: Bearer $env:LITELLM_MASTER_KEY"
Next, send a real message request. In PowerShell, quotation marks are easy to escape incorrectly, so it is more reliable to put the request body in a file. Create body.json in the project directory:
{"model":"coding-main","max_tokens":64,"messages":[{"role":"user","content":"ping"}]}
Then run:
curl.exe http://127.0.0.1:4000/v1/messages -H "x-api-key: $env:LITELLM_MASTER_KEY" -H "anthropic-version: 2023-06-01" -H "content-type: application/json" -d "@body.json"
If the returned JSON contains the model’s reply, the proxy, the master key, and the provider connection are all working.
4. End-to-End Testing with Claude Code
Before you start Claude Code, set three environment variables.
macOS or Linux:
export ANTHROPIC_BASE_URL=http://127.0.0.1:4000export ANTHROPIC_AUTH_TOKEN="$LITELLM_MASTER_KEY"export ANTHROPIC_MODEL=coding-mainclaude
Windows PowerShell:
$env:ANTHROPIC_BASE_URL = "http://127.0.0.1:4000"$env:ANTHROPIC_AUTH_TOKEN = $env:LITELLM_MASTER_KEY$env:ANTHROPIC_MODEL = "coding-main"claude
ANTHROPIC_BASE_URL should contain only the host and port. Do not add /v1 at the end. After Claude Code starts, enter a simple question. The terminal running the proxy should show a request log entry for coding-main. If Claude Code replies normally and the proxy log records the request, the full chain is working.
5. Switching Models and Choosing a Strategy
To switch models, exit the current Claude Code session, change the value of ANTHROPIC_MODEL, and restart it. For example, to switch to the backup model:
ANTHROPIC_MODEL=coding-backup claude
In Windows PowerShell, set the variable first, then start Claude Code:
$env:ANTHROPIC_MODEL = "coding-backup"claude
You do not need to change Claude Code’s own configuration. All model mappings are stored in the proxy’s config.yaml.
When choosing a model, compare the following factors for each type of task:
| Task type | Key concern | How to evaluate |
|---|---|---|
| Large code changes across multiple files | Code quality and stability of tool calls | Run the same real change with both models and check whether the resulting edits pass the tests without further fixes |
| Reading long documents or large codebases | Context window length | Check the context limit in the provider’s documentation, then test with real files to see whether content gets truncated |
| Debugging complex errors and reasoning through logic | Reasoning quality | Test with a past bug whose root cause is known, and see whether the model identifies it |
| Routine small edits and Q&A | Response speed and cost | Log input tokens, output tokens, and total latency for each call, then estimate monthly spending |
Prices and performance change as providers adjust their offerings, so this article does not list specific figures. Check each provider’s pricing page and official documentation. Providers usually bill input and output tokens separately. Long-context tasks often cost much more than ordinary Q&A, so factor that into your choice.
Also note that LiteLLM converts Anthropic-format requests into the format of other providers. During this conversion, the details of tool calls and streaming output may differ from the native API. After you switch to a non-Anthropic model, run small tests before using it in a production project.
6. Verification Checklist After Adding or Switching Models
Each time you add or switch a model, check the following in order:
- Call /v1/models and confirm that the list includes the new model_name, spelled exactly as it appears in config.yaml.
- Change the model field in body.json to the new name, send a /v1/messages request, and confirm that the response contains text.
- In Claude Code, enter a simple question and confirm that it replies normally.
- Check the terminal log for the litellm process and confirm that the request refers to the new model name.
- If the new model is not from the Anthropic family, test tool calls and streaming output with a small task before using it in a real project.
7. Troubleshooting Common Problems
Work through the following checks in order and do not skip steps. First, confirm the port is not in use. Then check that the keys match. Next, check that the model names are spelled correctly. Then check whether ANTHROPIC_BASE_URL ends with /v1. Finally, check your official login status.
| Symptom | Common cause | How to check | |
|---|---|---|---|
| The proxy fails to start because the port is already in use | Another program is using port 4000 | On macOS and Linux, run lsof -i :4000. On Windows, run `netstat -ano \ |
findstr :4000`. Stop the process that is using the port, or switch to another port and update ANTHROPIC_BASE_URL to match |
| 401 Unauthorized | The client key does not match the proxy’s master key, or the variable is not set in the current terminal | Run echo $LITELLM_MASTER_KEY to confirm the variable has a value. Confirm that ANTHROPIC_AUTH_TOKEN and LITELLM_MASTER_KEY have the same value |
|
| 404, with a message that the model does not exist | ANTHROPIC_MODEL does not match a model_name in config.yaml | Call /v1/models and compare the returned names with your value character by character | |
| Unexpected request paths, such as /v1/v1/messages | ANTHROPIC_BASE_URL ends with /v1 | Change the address to contain only the host and port, for example http://127.0.0.1:4000 | |
| Claude Code still connects to the official service | The variables only apply to an older terminal, or an official login is saved | Set the variables again in a new terminal, run /logout inside Claude Code, then start it again | |
| The proxy receives requests but returns provider errors | The upstream key is missing or invalid, or the model ID is wrong | Check the terminal log for the litellm process, which shows the error the provider returned. Confirm the upstream keys were set before the proxy started | |
| The litellm command is not found | The Python virtual environment is not activated | Run the activation command again. On Windows, use ..venv\Scripts\Activate.ps1 | |
| PowerShell cannot activate the virtual environment | The execution policy blocks scripts | Run Set-ExecutionPolicy -Scope CurrentUser RemoteSigned, then try again |
If you still cannot find the cause, add the --detailed_debug flag to the startup command to get more detailed request logs. Remove the flag after you finish troubleshooting, because detailed logs may include request content.
8. Cost and Security Recommendations
For cost control, set a monthly spending limit in each provider’s console, and review your usage details regularly. Create a separate key for each use case. If one key is compromised or misused, you can revoke it without affecting other services. For instructions on setting monthly limits and virtual keys, follow the LiteLLM documentation and your provider’s dashboard.
For security, keep the following points in mind.
First, the proxy should serve only the local machine. Add --host 127.0.0.1 at startup so that the proxy accepts connections only from localhost:
```bash
litellm —
