Repository navigation
Description
Summary
When the same LLM or Agent object is reused across multiple LocalConversation instances, per-conversation state mutated onto the LLM leaks between conversations. Two values are affected:
_prompt_cache_key— set in_pin_prompt_cache_key(), guarded byis Nonex-litellm-session-idinextra_headers— set in_pin_session_affinity_header()(feat: send x-litellm-session-id header for conversation-level routing affinity #3437), guarded bynot in existing
Both use the same pattern: mutate the LLM object, skip if already set. The second conversation sees the first's value and keeps it.
Repro
from openhands.sdk.llm import LLM
from openhands.sdk.agent import Agent
from openhands.sdk.conversation import LocalConversation
llm = LLM(model="openai/gpt-4o-mini", api_key="test")
agent = Agent(llm=llm, tools=[])
conv1 = LocalConversation(agent=agent, workspace="/tmp/ws1")
conv1._ensure_agent_ready()
conv2 = LocalConversation(agent=agent, workspace="/tmp/ws2")
conv2._ensure_agent_ready()
# Both fail — conv2 inherited conv1's values
assert agent.llm._prompt_cache_key == str(conv2.id) # still conv1.id
assert agent.llm.extra_headers["x-litellm-session-id"] == str(conv2.id) # still conv1.idExamples in this repo that reuse LLM objects across conversations:
examples/01_standalone_sdk/24_planning_agent_workflow.py— onellmused for planning + execution conversationsexamples/01_standalone_sdk/31_iterative_refinement.py— onellmreused across refactoring/critique conversations
Practical impact
Low in production — the Cloud/server path (EventService.start()) always deserializes a fresh agent from JSON, so shared references don't occur. The aliasing only affects standalone SDK usage where callers construct one LLM and pass it to multiple LocalConversation instances.
For _prompt_cache_key: the effect is two conversations sharing an OpenAI prefix-cache shard, which is benign (prefix matching is content-based, not key-based).
For x-litellm-session-id: the effect is two conversations routing to the same LiteLLM deployment, which defeats per-conversation probabilistic distribution. Only matters when the LiteLLM proxy has session_affinity enabled with multiple weighted deployments.
Prior art
fork() already handles this — it deep-copies the agent via JSON round-trip to avoid _prompt_cache_key aliasing (see comment at line ~389).
Possible fixes
-
Defensive copy in
__init__— same JSON round-trip thatfork()uses. Each conversation owns its own agent/LLM objects. Simple (~3 lines), fixes both values at once, but changes the contract (callers can no longer mutate the agent externally after creating the conversation). -
Always overwrite, remove guards —
_pin_prompt_cache_keydrops theis Nonecheck,_pin_session_affinity_headerdrops thenot in existingcheck. Each conversation stamps its own ID. Fixes the aliasing but means the last conversation to init "wins" on the shared object — still a race if conversations are concurrent. Also breaks_prompt_cache_key's intentional sub-agent inheritance (sub-agents inherit the parent's key viamodel_copy; overwriting would shard them separately). -
Private field + call-time injection — store the session ID on a private LLM field (
_conversation_session_id), inject intoextra_headersat call time inselect_chat_options(). Keepsextra_headersclean, private field is always overwritten. More plumbing (~15 lines across 2 files) and doesn't fix_prompt_cache_key.
Surfaced by @enyst's review on #3437.
This issue was created by an AI agent (OpenHands) on behalf of @calvinsmith.
Activity
csmith49 commentedon May 29, 2026
I know
.fork()resolves these aliasing issues by making a deep copy. I'm not sure that's what we'll want to use always: that same fix in the example given would result in the reference to the agent going stale.Honestly not sure that matters though, guess it depends on what we expect folks to be doing with the SDK.
enyst commentedon May 30, 2026
I wonder if this alternative may be better:
LLM.create_from(llm: LLM)which does the dirty detailsmodel_copy but oops actuallyBut I could be wrong?
csmith49 commentedon May 30, 2026
My mental model has these values living in the conversation itself, then being injected into the LLM right before completion time. The conversation is the thing that actually cares about the prompt cache keys (and the session ID, if you go to a different deployment there goes your prompt cache), and the SDK might benefit from the flexibility of being able to swap/modify agents and LLMs without having to think about these values.
But I think I'm implicitly optimizing for "conversation stability", and that may not be right. LLM registry would be a decent solution if these values really should be associated with an LLM instead. Do you think that's the case?
all-hands-bot commentedon Jun 1, 2026
[Automatic Post]: This issue has been waiting for triage. @xingyaoww @neubig @enyst, could you
please take a look and add the appropriate priority label when you have a chance?
This comment was created by an AI agent (OpenHands) on behalf of the user.
fix: thread call_context through fallback LLM calls (#3443)
fix(llm): replace mutable extra_headers with frozen LLMCallContext (#…
fix(llm): replace mutable extra_headers with frozen LLMCallContext (O…
fix(llm): thread call_context through async refresh-on-401 retry