fix: persist API keys when switching LLM providers#691
Conversation
When users switch between providers (e.g., OpenAI → Anthropic) in Settings, the previous provider's API key was lost because the config stores only a single flat `api_key` field. This fix: - Saves the current API key into `api_keys[old_provider]` before switching to a new provider - Restores the saved key from `api_keys[new_provider]` when switching back, if no new key is explicitly provided - Falls back to per-provider keys in the GET endpoint when the top-level key is empty Closes srbhr#674
Code Review SummaryStatus: 3 Issues Found | Recommendation: Address before merge Overview
Issue Details (click to expand)CRITICAL
WARNING
Other Observations (not in diff)Issues found in unchanged code that cannot receive inline comments:
Files Reviewed (2 files)
LiteLLM Configuration ReviewThe PR adds per-provider API key storage and retrieval, which is a useful feature for users who switch between providers. The implementation:
Prompts Flow: The LLM configuration is used by |
There was a problem hiding this comment.
1 issue found across 1 file
Prompt for AI agents (unresolved issues)
Check if these issues are valid — if so, understand the root cause of each and fix them. If appropriate, use sub-agents to investigate and fix each issue separately.
<file name="apps/backend/app/routers/config.py">
<violation number="1" location="apps/backend/app/routers/config.py:135">
P2: Switching providers can overwrite a newer per-provider key with a stale top-level api_key because the new preservation block always writes stored["api_key"] into api_keys[old_provider], while /config/api-keys only updates api_keys. This creates a real regression when both endpoints are used.</violation>
</file>
Reply with feedback, questions, or to request a fix. Tag @cubic-dev-ai to re-run a review.
- CRITICAL: Add per-provider api_keys fallback to get_llm_config() in llm.py so actual LLM calls (not just the UI) use the correct key after provider switches - P2: Don't overwrite api_keys[old_provider] if it already has a value set via /config/api-keys endpoint, preventing stale key regression
There was a problem hiding this comment.
3 issues found across 2 files (changes from recent commits).
Prompt for AI agents (unresolved issues)
Check if these issues are valid — if so, understand the root cause of each and fix them. If appropriate, use sub-agents to investigate and fix each issue separately.
<file name="apps/backend/app/routers/config.py">
<violation number="1" location="apps/backend/app/routers/config.py:137">
P2: Guarding the per-provider key update can leave stale keys in api_keys and discard recent /llm-api-key updates when switching providers.</violation>
</file>
<file name="apps/backend/app/llm.py">
<violation number="1" location="apps/backend/app/llm.py:252">
P2: Explicit empty api_key is no longer respected; falsy check now falls back to per-provider/env defaults, masking missing-key state after provider switches and potentially using unrelated env credentials.</violation>
<violation number="2" location="apps/backend/app/llm.py:253">
P2: `stored.get("api_keys", {})` can return a non-dict persisted value (e.g., null/string), causing `.get(...)` to raise AttributeError and break config loading.</violation>
</file>
Reply with feedback, questions, or to request a fix. Tag @cubic-dev-ai to re-run a review.
- Remove the api_keys[old_provider] guard that prevented updating keys after the first switch - Add isinstance check on api_keys to handle corrupted config.json
There was a problem hiding this comment.
1 issue found across 2 files (changes from recent commits).
Prompt for AI agents (unresolved issues)
Check if these issues are valid — if so, understand the root cause of each and fix them. If appropriate, use sub-agents to investigate and fix each issue separately.
<file name="apps/backend/app/routers/config.py">
<violation number="1" location="apps/backend/app/routers/config.py:135">
P1: Provider switching now unconditionally overwrites per-provider API keys, which can clobber newer keys saved via `/config/api-keys`.</violation>
</file>
Reply with feedback, questions, or to request a fix. Tag @cubic-dev-ai to re-run a review.
Summary
Fixes #674
When users switch between AI providers (e.g., OpenAI → Anthropic) in the Settings page, the previous provider's API key is lost. Switching back requires re-entering the key.
Root cause: The config stores a single flat
api_keyfield that gets overwritten on every provider switch. The per-providerapi_keysdictionary exists but was never used during the switch flow.Changes
apps/backend/app/routers/config.py:PUT /config/llm-api-key— Before switching providers, saves the currentapi_keyintoapi_keys[old_provider]. When switching to a new provider with no explicit key, restores fromapi_keys[new_provider]if available.GET /config/llm-api-key— Falls back toapi_keys[provider]when the top-levelapi_keyis empty, so the UI correctly shows a stored key exists.How it works
Test plan
data/config.json)Summary by cubic
Persist API keys when switching LLM providers so keys aren’t lost and are restored automatically. Fallback to per‑provider keys now powers both the UI and runtime LLM calls.
Written for commit cd48f8c. Summary will update on new commits.