feat(routing): provider-aware session affinity hints for prompt caching - #5252
Closed
davidgovea wants to merge 1 commit into
Closed
feat(routing): provider-aware session affinity hints for prompt caching#5252davidgovea wants to merge 1 commit into
davidgovea wants to merge 1 commit into
Conversation
davidgovea
marked this pull request as ready for review
April 6, 2026 18:37
This was referenced Apr 6, 2026
19 tasks
Collaborator
|
Thanks for this proposal — the idea was sound and main has since realized provider-aware session cache affinity in the transport layer: |
1 task
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What does this PR do?
Most inference providers now support some form of prompt prefix caching, but the cache-hint mechanism varies per provider: Fireworks uses an
x-session-affinityheader, OpenAI uses aprompt_cache_keybody field, xAI hasx-grok-conv-id, etc. There's no standard, and this is a real pain point across the ecosystem: several PRs and issues in other agentic clients are stalled or incomplete because each one hits the same "which provider am I actually talking to?" problem before it can send the right hint.This PR introduces a data-driven routing table that maps
(provider, api_mode)→ request metadata, so each provider gets exactly the cache hint it understands and unknown routes get nothing. It also generalizes the existing hardcodedprompt_cache_keyfor OpenAI Codex into this same table, and threadssession_idthrough all LLM call sites (primary, auxiliary, context compression, title generation, memory flush, iteration-limit summaries).Related Issue
N/A — standalone improvement. See "Related ecosystem work" below for cross-project context.
Type of Change
Performance & robustness
Changes Made
Core routing logic:
agent/model_metadata.py— newbuild_openai_request_session_routing()with a provider/api_mode lookup table, provider inference from base_url, and template renderingCurrent routing entries:
fireworkschat_completionsx-session-affinity: {session_id}headeropenai/openai-codexcodex_responsesprompt_cache_key: {session_id}Future / WIP — not yet wired, pending testing:
x-session-affinityheaderx-grok-conv-idheader orprompt_cache_keyx-session-affinityor similarIntegration points:
run_agent.py—_apply_request_session_routing()method applied to primary_build_api_kwargs, memory flush, and iteration-limit summary callsagent/auxiliary_client.py—session_idparam oncall_llm/async_call_llm, routing applied in_build_call_kwargs,api_modeattribute on all auxiliary client wrappers,_CodexCompletionsAdapterforwardsprompt_cache_key/extra_headers/extra_bodyagent/context_compressor.py— forwardssession_idto auxiliarycall_llmagent/title_generator.py— forwardssession_idto auxiliarycall_llmHow to Test
pytest tests/ -q— new tests pass, but there are prexisting failures in my current env (WIP)x-session-affinityheader should be present with the session IDprompt_cache_keyshould still be set tosession_id(unchanged behavior, now via the routing table)prompt_cache_keyshould be sent (unchanged behavior, copilot provider is not in the table)Provider docs
user/x-session-affinityprompt_cache_keyprompt_cache_keyx-grok-conv-id/prompt_cache_keyx-session-affinityfor prefix-cache localityRelated ecosystem work
These all circle around the same problem — agentic clients need provider-aware gating to send the right cache hint on the right route:
prompt_cache_keythrough Responses → Chat translationChecklist
Code
fix(scope):,feat(scope):, etc.)pytest tests/ -qand all tests pass (I'm seeing preexisting test failures on main: will continue to iterate)Documentation & Housekeeping
docs/, docstrings) — or N/Acli-config.yaml.exampleif I added/changed config keys — or N/ACONTRIBUTING.mdorAGENTS.mdif I changed architecture or workflows — or N/Aguide — or N/A