LiteLLM proxy for Claude Code and Codex: setup, keys, safety
Run the LiteLLM proxy in front of Claude Code, Codex and OpenCode as of December 2026: install, config.yaml, virtual keys with budgets, safe binding, bad versions.
The LiteLLM proxy (LiteLLM calls it the AI Gateway) is an open-source server you run yourself that puts one address and one set of keys in front of many model providers. It serves OpenAI’s chat and Responses APIs and Anthropic’s /v1/messages, so Claude Code (through ANTHROPIC_BASE_URL), Codex (through a custom provider in config.toml) and OpenCode can all use it, each with its own budgeted key. As of December 1, 2026 the latest release is 1.111.1 (November 29); install it with uv tool install 'litellm[proxy]' or run the Docker image, and start it with --host 127.0.0.1, because by default it listens on every interface. Avoid 1.82.7, 1.82.8 and 1.107.0: those releases were compromised.
What the proxy does for a coding agent
You list models in a config.yaml, each pointing at a provider and a key the proxy reads from its environment. Clients then talk to the proxy in the format they already speak, and LiteLLM translates. Its /v1/messages page says it will “call all your LLM APIs in the Anthropic v1/messages format”, and the same is true of the Responses API that Codex uses. Your provider keys stay on the proxy; each agent gets a LiteLLM key instead, which can be limited to some models and given a spending budget. Each provider still bills you for what you use.
This is a different tool from Claude Code Router, which is a local router built for Claude Code alone. If you are choosing between running a gateway and paying a hosted one, the OpenRouter alternatives guide sets LiteLLM beside OpenRouter and the others.
Which version to run, and the two compromised releases
LiteLLM ships often. On PyPI and its GitHub releases, as of December 1, 2026:
- 1.111.1, November 29, 2026, is the latest stable release.
- 1.108.x is the long-term-support line (1.108.21 on November 28). LiteLLM’s release-cadence post says it “is for teams that want to stay on one version and keep getting fixes”, as a pilot running through December 31, 2026.
- Versions ending in
rcordev(1.112.0rc2, 1.113.0.dev2) are pre-releases.
Two releases this year were published by attackers, according to LiteLLM’s own incident posts:
- 1.82.7 and 1.82.8 on PyPI, March 24, 2026, published at 10:39 UTC and quarantined by PyPI about 40 minutes later, though the post gives the affected window as 10:39 to 16:00 UTC. LiteLLM says the official proxy Docker image was not affected, and 1.83.0 was the clean release that followed.
- 1.107.0, October 21, 2026: on PyPI from 11:39 to 13:23 UTC, and in Docker images. The
v1.107.0,latest,main-latestandmain-stabletags on Docker Hub and GitHub’s registry pointed at the compromised image from 11:46 UTC until 13:51 (GitHub) or 14:20 (Docker Hub). So a pull oflatestin that window got it too.
Check what a Python environment has with pip show litellm. If an environment ever ran one of those versions, LiteLLM’s posts say to treat it as compromised: for 1.107.0, do not run pip install in it first, because any Python start-up, pip’s included, re-runs the planted cook.pth and sitecustomize.py; rebuild it from scratch, and rotate every credential the machine could reach, including the provider keys stored in the proxy’s database, the master key, every virtual key and the database password. The incident post lists the full steps and the signs to look for.
The habit that limits this: install a pinned version rather than whatever is newest, and pin Docker images to a release tag, never latest.
Install the LiteLLM proxy
The quick-start installs it with uv, which also provides a suitable Python. Pinning the version is ordinary package syntax:
uv tool install 'litellm[proxy]==1.111.1'CopiedWith plain pip, check python3 --version first: LiteLLM 1.84.0 and later need Python 3.10 or newer, and the docs warn that on an older Python, pip “silently resolves down” to the last release that still allowed it.
For Docker, use a release tag. Each GitHub release gives a cosign command that checks the image was built by LiteLLM’s release workflow:
cosign verify \
--certificate-identity https://github.com/BerriAI/project-releaser/.github/workflows/release.yml@refs/heads/main \
--certificate-oidc-issuer https://token.actions.githubusercontent.com \
ghcr.io/berriai/litellm:v1.111.1CopiedA minimal config.yaml with two providers
Three Claude models on your Anthropic key and one OpenAI model. model_name is the name clients ask for; model is the provider and its model id. os.environ/ tells LiteLLM to read the value from an environment variable, so no secret sits in the file:
# ~/litellm/config.yaml
model_list:
- model_name: claude-opus-5-5
litellm_params:
model: anthropic/claude-opus-5-5
api_key: os.environ/ANTHROPIC_API_KEY
- model_name: claude-sonnet-5-5
litellm_params:
model: anthropic/claude-sonnet-5-5
api_key: os.environ/ANTHROPIC_API_KEY
- model_name: claude-haiku-5-5
litellm_params:
model: anthropic/claude-haiku-5-5
api_key: os.environ/ANTHROPIC_API_KEY
- model_name: gpt-6.2-sol
litellm_params:
model: openai/gpt-6.2-sol
api_key: os.environ/OPENAI_API_KEY
general_settings:
master_key: os.environ/LITELLM_MASTER_KEYCopiedThe master key is the proxy’s admin key: it can call every model, mint and delete keys, and is the admin UI’s password. Put it and the provider keys in a file only you can read, typed at a hidden prompt so they never reach your shell history. The master key is generated, so you never see or type it:
mkdir -p ~/litellm && cd ~/litellm
touch .env && chmod 600 .env
printf 'LITELLM_MASTER_KEY=sk-%s\n' "$(openssl rand -hex 32)" >> .env
printf 'Anthropic API key: '; read -rs k; echo; printf 'ANTHROPIC_API_KEY=%s\n' "$k" >> .env
printf 'OpenAI API key: '; read -rs k; echo; printf 'OPENAI_API_KEY=%s\n' "$k" >> .env; unset kCopiedThe virtual-keys page says the master key “must start with sk-”; the Docker quick-start calls the prefix optional. Keeping it satisfies both. Then start the proxy on the loopback address only:
cd ~/litellm
set -a; . ./.env; set +a
litellm --config config.yaml --host 127.0.0.1 --port 4000CopiedWithout --host, the default is 0.0.0.0 and the start-up line reads Proxy running on http://0.0.0.0:4000: every network the machine is on can reach it, and the only thing between them and your provider keys is the master key. LiteLLM’s own tutorials use http://0.0.0.0:4000 as the address in their examples; http://127.0.0.1:4000 is the same proxy from the same machine.
The same thing in Docker, with the port published on loopback only (-p 4000:4000 would publish it on every interface):
docker run -d --name litellm --env-file ~/litellm/.env \
-v "$HOME/litellm/config.yaml:/app/config.yaml:ro" \
-p 127.0.0.1:4000:4000 \
ghcr.io/berriai/litellm:v1.111.1 --config /app/config.yamlCopiedVirtual keys and budgets need a database
One key per agent is the point of a gateway: you can see what each one spends, cap it, and revoke it without touching the others. LiteLLM virtual keys need a Postgres database (DATABASE_URL), and the Docker quick-start has a Compose file that starts the proxy with Postgres beside it. Two things are worth adding to the .env it uses. The Compose file publishes the port as ${LITELLM_BIND:-}${LITELLM_PORT:-4000}:4000, so without LITELLM_BIND it listens on every interface; and the database password defaults to litellm unless POSTGRES_PASSWORD is set:
mkdir -p ~/litellm-db && cd ~/litellm-db
curl -sSLO https://github.com/BerriAI/litellm/raw/main/litellm/deploy/docker/docker-compose.quickstart.yml
touch .env && chmod 600 .env
printf 'LITELLM_MASTER_KEY=sk-%s\nLITELLM_SALT_KEY=sk-%s\nPOSTGRES_PASSWORD=%s\nLITELLM_BIND=127.0.0.1:\n' \
"$(openssl rand -hex 32)" "$(openssl rand -hex 32)" "$(openssl rand -hex 24)" >> .envCopiedBefore starting it, change the file’s image: docker.litellm.ai/berriai/litellm:latest line to a release tag (the file’s own header says to pin it for anything beyond local evaluation), then:
docker compose -f docker-compose.quickstart.yml up -dCopiedOpen http://localhost:4000/ui, sign in as admin with the master key from .env, and add models under Models + Endpoints. To keep provider keys out of the database, the docs suggest passing them to the litellm service in the Compose file (for example OPENAI_API_KEY: ${OPENAI_API_KEY}, with the key itself added to .env at a hidden prompt as above) and typing os.environ/OPENAI_API_KEY in the key field. Keys you paste instead are stored encrypted with LITELLM_SALT_KEY, which the production page says never to change after adding a model: a new salt key makes the stored provider keys unreadable. Back up the .env.
With the database up, mint one key per agent from the UI’s Virtual Keys page, or from the API. This one can reach only the Claude models and resets a $50 budget every 30 days:
cd ~/litellm-db && set -a; . ./.env; set +a
curl -sS http://127.0.0.1:4000/key/generate \
-H "Authorization: Bearer $LITELLM_MASTER_KEY" \
-H "Content-Type: application/json" \
-d '{"models": ["claude-opus-5-5", "claude-sonnet-5-5", "claude-haiku-5-5"],
"max_budget": 50, "budget_duration": "30d",
"metadata": {"agent": "claude-code"}}'CopiedThe response’s key field is the new key. The budget docs accept durations in seconds, minutes, hours or days (30s, 30m, 30h, 30d), and a key over its budget gets an error like:
Authentication Error, ExceededTokenBudget: Current spend for token: 7.2e-05; Max Budget for Token: 2e-07CopiedGive the agents these keys, never the master key. If you run the pip-installed proxy instead, it uses the same database through a DATABASE_URL you set, pointing at any Postgres.
Point Claude Code at LiteLLM
Claude Code speaks Anthropic’s Messages API, and the gateway guide asks a gateway for /v1/messages (with /v1/messages/count_tokens optional), which LiteLLM serves at its root. Two variables do it: ANTHROPIC_BASE_URL set to the proxy, and ANTHROPIC_AUTH_TOKEN set to the virtual key, which Claude Code sends as Authorization: Bearer. Test them first with the request Claude Code’s gateway page gives (export the two variables in this shell for the test):
curl -sS -w '\n%{http_code}\n' -X POST "$ANTHROPIC_BASE_URL/v1/messages" \
-H "Authorization: Bearer $ANTHROPIC_AUTH_TOKEN" \
-H "anthropic-version: 2023-06-01" \
-H "content-type: application/json" \
-d '{"model": "claude-sonnet-5-5", "max_tokens": 1, "messages": [{"role": "user", "content": "."}]}'CopiedThen make it stick in the env block of ~/.claude/settings.json, which applies to all your projects, beats a shell export, and reaches background sessions a shell export may not. LiteLLM’s Claude Code tutorial says the model names Claude Code asks for must match model_name in your config exactly, so map its three tiers onto your names:
{
"env": {
"ANTHROPIC_BASE_URL": "http://127.0.0.1:4000",
"ANTHROPIC_AUTH_TOKEN": "sk-...the virtual key...",
"ANTHROPIC_DEFAULT_OPUS_MODEL": "claude-opus-5-5",
"ANTHROPIC_DEFAULT_SONNET_MODEL": "claude-sonnet-5-5",
"ANTHROPIC_DEFAULT_HAIKU_MODEL": "claude-haiku-5-5"
}
}CopiedThe file now holds a key, so chmod 600 ~/.claude/settings.json, and never put the key in a project’s .claude/settings.json, which is committed. Start claude and run /status: an Anthropic base URL line shows the proxy is in use. The Claude Code environment variables guide covers the rest of the variables and which source wins.
What Anthropic’s gateway page says about this setup:
Anthropic doesn't endorse, maintain, or audit third-party gateway products, and doesn't support routing Claude Code to non-Claude models through any gateway.CopiedLiteLLM does translate /v1/messages to GPT or Gemini models if you list them; using them in Claude Code that way is outside what Anthropic supports. The same page says that while a gateway credential is set, requests do not use your claude.ai plan: they are billed per token to whoever owns the provider key the gateway forwards. Its connect page adds that Remote Control and voice dictation are unavailable while a gateway credential is active.
One setting the two makers’ pages treat differently. Claude Code’s troubleshooting table suggests CLAUDE_CODE_DISABLE_EXPERIMENTAL_BETAS=1 when a gateway returns 400 errors such as Extra inputs are not permitted. LiteLLM’s tutorial says not to set it, because it turns off auto mode’s server-side checks (/status then shows Auto mode server: Disabled), and that LiteLLM 1.109.0 fixed the last invalid beta flag error on Bedrock Invoke, the case it had been recommended for. On a current proxy, leave it unset unless you see that error. If you use MCP tools, the tutorial also notes that tool search is off behind a non-Anthropic base URL until you set ENABLE_TOOL_SEARCH=true.
Point Codex at LiteLLM
Codex reaches anything other than OpenAI through a custom model provider in ~/.codex/config.toml. Current Codex speaks only the Responses API (the config reference: “responses is the only supported value” for wire_api), and LiteLLM serves it under /v1. Following LiteLLM’s Codex page, put the two top-level keys at the top of the file, before any [table]:
# ~/.codex/config.toml
model = "gpt-6.2-sol"
model_provider = "litellm"
[model_providers.litellm]
name = "LiteLLM"
base_url = "http://127.0.0.1:4000/v1"
env_key = "LITELLM_API_KEY"
wire_api = "responses"Copiedenv_key names the environment variable Codex reads the key from; the key itself does not go in the file. Set it from a hidden prompt, using a virtual key minted for Codex with gpt-6.2-sol in its models:
printf 'LiteLLM key for Codex: '; read -rs LITELLM_API_KEY; echo; export LITELLM_API_KEY
codexCopiedTo keep it across shells, put the export line in a file only you can read that your shell profile sources. Three things trip people up:
- The block is ignored in a project’s
.codex/config.toml.model_providerandmodel_providersare among the keys Codex skips in project-local config, with a warning at start-up. The Codex config.toml guide covers how the files stack. - The provider id cannot be a built-in one. Codex reserves
openai,ollama,lmstudio,amazon-bedrock,amazon-bedrock-runtimeandgcp;litellmis free. - Other makers’ models work, with less metadata. LiteLLM’s page says
model“can be anymodel_namefrom your LiteLLM config”, socodex -m claude-sonnet-5-5works through the proxy. Codex does not know that name, warns as below, and offers no reasoning-effort choices for it.
Unknown model <name> is used. This will use fallback model metadata.CopiedLong agent turns can sit silent on the stream; LiteLLM’s page raises stream_idle_timeout_ms in the provider block (Codex’s default is 300000, five minutes) if runs are cut off.
OpenCode
OpenCode takes LiteLLM as an OpenAI-compatible provider. The shape from OpenCode’s provider docs, in ~/.config/opencode/opencode.jsonc:
{
"$schema": "https://opencode.ai/config.json",
"providers": {
"litellm": {
"name": "LiteLLM",
"env": ["LITELLM_API_KEY"],
"package": "@opencode/ai/providers/openai-compatible",
"settings": { "baseURL": "http://127.0.0.1:4000/v1" },
"models": {
"claude-sonnet-5-5": { "name": "Claude Sonnet 5.5 via LiteLLM" },
"gpt-6.2-sol": { "name": "GPT-6.2 Sol via LiteLLM" }
}
}
}
}Copiedenv lists the variables OpenCode reads the key from, and models are picked as litellm/<name>. If you would rather have one hosted account than a proxy to run, the OpenRouter settings for the same three agents are side by side in their own guide.
Reaching the proxy from other machines
Everything above keeps the proxy on 127.0.0.1, so only programs on the same machine can use it. When your agents run on a different machine, bind it to that machine’s address on a private network such as a VPN (--host 10.0.0.5, or LITELLM_BIND=10.0.0.5: in the Compose .env) rather than 0.0.0.0, or keep it on loopback and reach it through an SSH tunnel (ssh -N -L 4000:127.0.0.1:4000 you@server). If you do put it on the internet, put TLS in front of it, and treat the master key as the key to every provider account in the config. LiteLLM publishes security advisories on GitHub, several in November 2026 alone, which is one more reason to follow its releases. For sizing the machine the agents and the proxy run on, choosing a VPS for Claude Code and Codex applies here too.