Repository navigation
fix(cli): let the apiKeyHelper supply Claude Code's key under lite claude - #40489
mateo-berri merged 5 commits into
Conversation
…aude `lite login --config-claude` writes an apiKeyHelper into ~/.claude/settings.json, and `lite claude` then also exported ANTHROPIC_AUTH_TOKEN, so Claude Code opened with its "Both ANTHROPIC_AUTH_TOKEN and apiKeyHelper set" banner and, because the env token wins, never ran the helper that was meant to refresh the key. When the launch key is the stored login key and settings.json carries exactly the helper lite wrote for this base URL, `lite claude` now leaves the token out of the env (dropping an inherited one) so Claude Code asks the helper. An explicit --api-key or LITELLM_PROXY_API_KEY, a helper for another proxy, a hand-written helper, or no settings file keep the previous behavior.
🤖 Devin AI EngineerI'll be helping with this pull request! Here's what you should know: ✅ I will automatically:
Note: I can only respond to comments from users who have write access to this repository. ⚙️ Control Options:
|
Greptile SummaryThis PR lets Claude Code use LiteLLM's configured apiKeyHelper instead of exporting a competing token, while respecting CLAUDE_CONFIG_DIR and preserving explicit-key behavior
Confidence Score: 5/5The PR appears safe to merge, with no outstanding correctness, security, or repository-rule failures The current code resolves Claude Code's active settings file, recognizes aliases before ownership decisions, preserves explicit-key precedence, and safely records and restores configuration changes. All previous findings were manually resolved and are addressed by the current implementation
|
| Filename | Overview |
|---|---|
| litellm/proxy/client/cli/commands/agents.py | Detects the matching LiteLLM helper and suppresses inherited or generated Anthropic tokens only for stored-login Claude launches |
| litellm/proxy/client/cli/commands/auth.py | Configures the active Claude settings file and records an undo receipt after checking file ownership |
| litellm/proxy/client/cli/commands/claude_settings.py | Centralizes path resolution, canonical ownership checks, helper detection, transactional configuration, and conservative restoration |
| litellm/proxy/client/cli/commands/configure.py | Adds persistent Claude configuration and unconfiguration commands with explicit versus stored credential handling |
| tests/test_litellm/proxy/client/cli/test_agents.py | Covers helper-based launch environments, explicit-key precedence, alternate settings paths, and unaffected agent profiles |
| tests/test_litellm/proxy/client/cli/test_claude_settings.py | Exercises canonical paths, separate receipts, transactional failures, user edits, credential withholding, and restoration |
Reviews (5): Last reviewed commit: "merge: origin/litellm_internal_staging i..." | Re-trigger Greptile
Codecov Report✅ All modified and coverable lines are covered by tests. 📢 Thoughts on this report? Let us know! |
Devin e2e QATested at: 7e13795 Shared setup: one proxy built from this branch (7e13795) on port 45052, confirmed free before boot with Proxy launch: Before (2c836f4)Worktree
After (7e13795)Worktree
Verdict: the fix works as described. Login mode drops the env token and Claude Code takes its key from the helper with no banner; the explicit key path is unchanged |
Claude Code reads settings.json from CLAUDE_CONFIG_DIR when it is set, while lite login --config-claude wrote to ~/.claude/settings.json and lite claude checked that same file for its apiKeyHelper. With the override set, lite could drop ANTHROPIC_AUTH_TOKEN because the helper lives in a file Claude Code never reads, leaving it with no key at all. claude_settings_path(environ) now picks the file the way Claude Code does, and both commands go through it. The CLI test directory gets an autouse conftest that isolates HOME, USERPROFILE, and CLAUDE_CONFIG_DIR per test and fails any test that writes the developer's real settings.json, restoring it first.
…utoroute backups on login --config-claude
…e lite up and autoroute backups guard it
…yhelper_conflict Resolves the conflicts with the lite configure claude work from #40319: every persistent writer and reader of Claude Code's settings file now resolves it through CLAUDE_CONFIG_DIR, the lite up backup check only guards the default file, and each settings file keeps its own undo receipt (the default file keeps ~/.litellm/claude_configure_state.json, any other file gets ~/.litellm/claude_configure_state/<sha256 of its resolved path>.json).
|
bugbot run |
There was a problem hiding this comment.
✅ Bugbot reviewed your changes and found no new issues!
Comment @cursor review or bugbot run to trigger another review on this PR
Reviewed by Cursor Bugbot for commit ab9dc75. Configure here.
c005431
into
litellm_internal_staging
TLDR
Problem this solves:
lite login --config-claudewrites anapiKeyHelperinto~/.claude/settings.jsonlite claudethen also exportsANTHROPIC_AUTH_TOKENinto Claude Code's envHow it solves it:
lite claudechecks whether settings.json carries the helper lite wrote for this proxylite login, the token stays out of the env--config-claudeintended$CLAUDE_CONFIG_DIR/settings.jsonwhen set, else~/.claude/settings.json--api-keyorLITELLM_PROXY_API_KEYstill exports the token as beforeDecisions made here, with the alternatives considered:
lite login --config-claudewrites for this base URL, rather than parsing any helper that looks lite-shaped. Exact match means only a lite-installed helper changes behavior; the cost is that a helper written by a differentlitebinary keeps the old behavior--api-keyorLITELLM_PROXY_API_KEYkeeps exporting the env token even when a helper matches, because that key is what the user asked for and the helper would hand Claude Code the stored login key instead. That case still shows Claude Code's banner; the alternative (drop the token there too) would silently ignore the explicit keyANTHROPIC_AUTH_TOKENfrom the parent shell is dropped, the same wayANTHROPIC_API_KEYalready is, because Claude Code would prefer it over the helper and the launch key would no longer be the oneliteverifiedlite login --config-claudeandlite clauderesolve the settings file throughCLAUDE_CONFIG_DIRthe way Claude Code itself does, so a user who keeps Claude Code's config outside~/.claudegets the helper written to, and read from, the file Claude Code actually loads. The alternative (keep the fixed~/.claude/settings.json) would makelite claudeexport the token whenever the helper lives in the alternate file, bringing the banner back.lite upandlite autoroutestay on the default file: they take temporary ownership of it with a backup and restore, which is separate behavior and out of scope herelite up/lite autoroute upbackup check on--config-claudeonly applies when the target is the default~/.claude/settings.json, since those commands only ever manage that file. Checking it for an alternateCLAUDE_CONFIG_DIRwould refuse a write nothing is going to undo--config-clauderuns that backup check before the browser login starts, so a refused configure never leaves aLogin successful!behind it. The alternative (log in first, then fail the configure) is what this branch did before merging thelite configure claudework from feat(cli): add lite configure claude and lite unconfigure claude #40319, and it left users logged in with a half-applied setuplite configure claudeandlite unconfigure claude(from feat(cli): add lite configure claude and lite unconfigure claude #40319) followCLAUDE_CONFIG_DIRthe same way, and each settings file keeps its own undo receipt: the default file keeps~/.litellm/claude_configure_state.json, and any other file gets~/.litellm/claude_configure_state/<sha256 of its resolved path>.json. One shared receipt was rejected because unconfiguring underCLAUDE_CONFIG_DIRwould restore the default file's previous values (and its file-existed flag) into the wrong file; a receipt beside the settings file was rejected because that puts a lite artifact inside Claude Code's config dir, which people keep in dotfile reposUser Flow
Before: a developer who ran
lite login --config-claudestartslite claudeand Claude Code opens with an auth warning banner on every launch, with the helper never usedlite login --config-claude, finish the browser login, and seeConfigured Claude Code: ~/.claude/settings.json now routes through https://litellm-domainlite claudeand seelitellm: routing Claude Code through proxy at https://litellm-domain⚠ Both ANTHROPIC_AUTH_TOKEN and apiKeyHelper set · auth may not work as expected, telling them to unset one or the otherlite loginprintenv | grep ANTHROPIC_AUTH_TOKENprints nothing, which reads like the token was never setCLAUDE_CONFIG_DIRset sees--config-claudewrite~/.claude/settings.json, a file their Claude Code never reads, so Claude Code opens without the proxy wiring at allAfter: the same developer starts
lite claudeand Claude Code opens clean, reading its key from the helperlite login --config-claudeinstalledlite login --config-claude, finish the browser login, and seeConfigured Claude Code: ~/.claude/settings.json now routes through https://litellm-domain(withCLAUDE_CONFIG_DIRset, the path shown is$CLAUDE_CONFIG_DIR/settings.json)lite claudeand seelitellm: routing Claude Code through proxy at https://litellm-domainfollowed bylitellm: Claude Code reads its key from the apiKeyHelper in ~/.claude/settings.json, naming the same file the login step wrotelite auth print-tokeneach time Claude Code asks for it! env | grep -c '^ANTHROPIC_AUTH_TOKEN='prints0andANTHROPIC_BASE_URLstill points at https://litellm-domainRelevant issues
Linear ticket
Resolves LIT-7395
Pre-Submission checklist
Please complete all items before asking a LiteLLM maintainer to review your PR
uv run pytest tests/test_litellm/<your_test_file>.py -v. Leave the suites (make test-unit-*,make test-unit) to CI: it finishes in ~15 minutes where a laptop takes an hour or more@greptileaito re-request a review after pushing changes)Screenshots / Proof of Fix
Shared setup: one proxy from this branch on a random free port (25588, confirmed free with
lsof -nP -iTCP:25588 -sTCP:LISTENbefore boot),anthropic/claude-haiku-4-5in the model list, real Anthropic spend. The change is entirely in theliteCLI, so the same proxy serves both legs and only thelitecheckout differs. Claude Code v2.1.267 is driven interactively under tmux (tmux new-session -d -s lit7395 -x 160 -y 45, keystrokes viatmux send-keys, output viatmux capture-pane) with an isolatedHOMEso the run's~/.claude/settings.jsonand~/.litellm/token.jsonare its own. Login islite login --pkce --config-claudewith the proxy's own login form, which ends in the same settings write as the SSO device flow the ticket used.--model claude-haiku-4-5is passed because the rig's proxy does not serve Claude Code's default modelBefore (db7ca65, the merge base)
Log in and configure Claude Code
Start Claude Code through lite
Inside the session, the env token is what Claude Code has
Precedence check: with a bogus token in the env and the valid helper still in settings.json, Claude Code uses the env token and gets
401 Authentication Error, Invalid proxy server token passed. Received API Key = sk-...7395, so the helper never runs while the token is exported (this step exercises Claude Code's own precedence rather than lite, so it was run once, at 2c836f4, and not repeated at the merge base)After (ab9dc75)
Log in and configure Claude Code (same command, same settings.json written)
Start Claude Code through lite
No banner
Inside the session, no env token, and a real prompt goes through the proxy with the key coming from the helper
Proxy log for that turn:
POST /v1/messages?beta=true HTTP/1.1" 200 OKSame flow with Claude Code's config kept outside
~/.claude: both commands followCLAUDE_CONFIG_DIRNo banner; inside the session the token count is
0andpongcomes back the same wayCLAUDE_CONFIG_DIRpointing at a directory with no settings.json:lite claudekeeps exporting the token, since there is no helper for Claude Code to calllite upbackup present (~/.litellm/claude_settings_backup.json):--config-claudestill writes the alternate file, and refuses the default one before the login starts (no browser tab opens, noLogin successful!), also when the default dir is reached through a symlinkedCLAUDE_CONFIG_DIRlite unconfigure claudeunderCLAUDE_CONFIG_DIRundoes only that file: the alternate file (created by step 4's login) is removed, its receipt under~/.litellm/claude_configure_state/goes with it, and the default file plus its own receipt are untouchedObservations from the run:
printenvin the parent shell is expected: token lives in the child env only--api-keyplus a matching helper still shows the banner (by design, see decisions)Type
🐛 Bug Fix
Caveats (if any)
Low
lite upandlite autoroute upstill manage only~/.claude/settings.json, ignoringCLAUDE_CONFIG_DIRFinal Attestation
The tests check the right things, including the edge cases, and regressions in the respective real-world customer use-cases are not possible after this PR
ab9dc75 passes /live-pr-risk