Repository navigation
Description
Summary
When the launched OpenHands agent profile references a managed openhands/* LLM profile (for example Default) and the member's key slot holds a custom key for another provider (has_custom_llm_api_key=True, e.g. an OpenRouter key stored when the member activated an OpenRouter profile), the launch view attaches that custom key to the managed-proxy LLM. The proxy rejects it with token_not_found_in_db, and every self-heal path returns early because the member is marked custom. The user sees, on every new conversation:
litellm.AuthenticationError: Litellm_proxyException - Authentication Error, Invalid proxy server token passed.
Received API Key = <redacted> ... Unable to find token in cache or `LiteLLM_VerificationTokenTable`
even though the LLM profile they activated is a different provider and saved fine.
Where (main @ 7486d84)
storage/saas_settings_store.py:235-247_get_effective_llm_api_key: the member's custom key wins whenhas_custom_llm_api_keyis set.storage/saas_settings_store.py:355-364: the resolved agent-profile LLM goes throughresolve_profile_llm(resolved.llm, managed_proxy_url=LITE_LLM_API_URL, fallback_api_key=effective_llm_api_key).openhands/app_server/settings/llm_profiles.py:74-80: when the resolved profile routes to the managed proxy, its key is replaced byfallback_api_keyunconditionally. The same helper is applied toAppConversationStartRequest.llm_profilewithfallback_api_key=agent_settings.llm.api_key(openhands/app_server/app_conversation/live_status_app_conversation_service.py:596-603).- Heal is off for this state:
get_current_managed_llm_keyreturnsNoneforhas_custom_llm_api_key(saas_settings_store.py:1207-1212), so_maybe_refresh_managed_llm_keylogsskip_no_current_managed_keyand returns the LLM unchanged (live_status_app_conversation_service.py:1793-1803);rotate_managed_llm_keyreturnsBYOK(saas_settings_store.py:1403-1404);POST /api/keys/llm/managed/refreshreturns 400 for custom members (server/routes/api_keys.py:516-520). - fix: replace stale custom LLM key when switching back to the managed default profile #425 and fix: heal stale org-level BYOR LLM key on conversation start (#421) #437 fix the org-level slot (
org._llm_api_key) and the org-route switch back toDefault; they deliberately never touch a member withhas_custom_llm_api_key=True. UX: user can get stuck with a dead org-default LLM key and no way to fix it in the UI #296 describes the resulting dead end from the user's side.
Demonstration (pure function, main @ 7486d84)
from pydantic import SecretStr
from openhands.app_server.settings.llm_profiles import resolve_profile_llm
from openhands.sdk.llm import LLM
from openhands.sdk.llm.utils.openhands_provider import litellm_call_kwargs
PROXY = "https://llm-proxy.app.all-hands.dev"
custom = SecretStr("CUSTOM-OPENROUTER-KEY") # the member's effective key when has_custom_llm_api_key=True
managed = LLM(model="openhands/deepseek-v4-flash", base_url=None, api_key=None, usage_id="t")
r = resolve_profile_llm(managed, managed_proxy_url=PROXY, fallback_api_key=custom)
print(r.model, r.api_key.get_secret_value())
print(litellm_call_kwargs(r.model, r.base_url))openhands/deepseek-v4-flash CUSTOM-OPENROUTER-KEY
{'model': 'litellm_proxy/deepseek-v4-flash', 'api_base': 'https://llm-proxy.app.all-hands.dev'}
The OpenRouter key is sent to the OpenHands proxy as a bearer token. A profile that carries its own key for its own provider is left alone by the same helper, so the leak only shows when the launched LLM is managed while the member slot is custom.
One way a user gets there (community report, not reproduced on the account)
- Cloud account. Set up an ACP agent, then switch the active agent profile back to OpenHands in Agent Canvas. The OpenHands agent profile's
llm_profile_refmay point at a managed profile such asDefault (openhands/…). - Activate an OpenRouter LLM profile. The org route stores the OpenRouter key in
org_member.llm_api_keywithhas_custom_llm_api_key=True(server/routes/org_profiles.py:480-494). - Start a conversation.
_start_app_conversationloads withresolve_agent_profile=True, the agent profile resolves the managed profile,resolve_profile_llmoverrides its key with the member's OpenRouter key, the SDK sendslitellm_proxy/<model>tollm-proxy.app.all-hands.dev, 401token_not_found_in_db. The heal skips the member.
Report: https://openhands-ai.slack.com/archives/C06QT0AGY4W/p1792761903199139 (desktop Agent Canvas with the cloud backend, OpenRouter profile, error text in the thread). Account state that would confirm this path: org_member.has_custom_llm_api_key, org_member.active_agent_profile_id and that profile's llm_profile_ref, org.llm_profiles.active, and the model in the failing request.
Expected behavior
A custom key for a non-OpenHands provider must never be attached to a managed-proxy LLM. Either a managed profile referenced by an agent profile (or by llm_profile) uses the member's managed key, minting one if the member has none, or the fallback passed to resolve_profile_llm is restricted to the member's managed key. A member in this state should also recover on the next launch rather than being treated as BYOK forever.
Acceptance criteria
- With
has_custom_llm_api_key=True(custom key for provider X) and an active OpenHands agent profile whosellm_profile_refis a managedopenhands/*profile, the launch view's LLM carries a managed key, not the custom key. - The same holds for
AppConversationStartRequest.llm_profilenaming a managed profile. - A regression test in
tests/unit/test_saas_settings_store.pycovers the agent-profile launch with a custom member key and a managedllm_profile_ref. - A member already in this state recovers without manual DB edits (self-heal or a reset action).
Filed by a Claude Code job on Vasco's behalf from code analysis of main @ 7486d84; the demonstration above was run against the installed package, the account state was not inspected.
Activity