Skip to content

fix(provider): route each session to its OpenAI and xAI prompt cache - #461

Merged
lg2m merged 1 commit into
fix/prompt-cache-budget-noticefrom
fix/prompt-cache-key
Oct 25, 2026
Merged

lg2m merged 1 commit into
fix/prompt-cache-budget-noticefrom
fix/prompt-cache-key

Conversation

@lg2m

@lg2m lg2m commented Oct 24, 2026

Copy link
Copy Markdown
Collaborator

Stacked on #460.

OpenAI and xAI cache prompt prefixes implicitly, but a hit needs the request to reach the machine that holds the prefix. QQ sent no routing signal. Over the last 21 days of local sessions, openai-codex/gpt-6.1-sol read only 39.5% of its input from cache (356M uncached input tokens; Claude routes read 95%). 1,475 of its turns sent more than 50k input tokens with zero cache read, all after a run's first turn. Codex and Pi both send the session id for this; OpenCode sends xAI's conversation header.

What changed

  • ModelRequest::with_cache_key: a session run (every turn, follow-up runs, and the in-run summarizer, which shares the run's prefix) sends its session id as the key. Direct runs send none.
  • CacheRouting on the HTTP recipe decides how the key reaches the wire, and only for built-in endpoints, which configuration cannot redirect:
    • openai, openai-codex: Responses prompt_cache_key. The Codex body also sends the session-id header that the ChatGPT backend derives cache affinity from (as Codex and Pi do).
    • xai: x-grok-conv-id.
    • Custom and LiteLLM endpoints: nothing, since a strict OpenAI-compatible proxy may reject an unknown field. Anthropic, Bedrock and Google: nothing; they use explicit breakpoints or have no key.
    • Construction rejects a routing on the wrong protocol.
  • The session id is a random identifier, not a credential. Request-shape digests are unchanged.

Verification

  • every_session_request_carries_the_session_as_its_cache_key (two runs in one session) and the in-run summarizer assertion in an_in_run_summarizer_keeps_the_session_context_before_its_prompt both failed with the session-layer lines removed and pass with them.
  • Loopback provider tests check the exact body field and headers: present when enabled and keyed, absent otherwise. Codex-only session-id; xAI x-grok-conv-id. cache_routing_follows_the_builtin_route pins the route table.
  • Real binary against the verify-qq Custom endpoint: run completed, and prompt_cache_key appears 0 times in the captured request, as intended for custom endpoints.
  • cargo xtask check passes. Six xtask/src/legacy-sleeps.txt line numbers moved; each was checked to still name the same sleep.

Not verified

There are no live provider calls in tests, so the cache-hit improvement itself is unmeasured. Measure it with the same model_turns.usage_json query on new openai-codex turns after this merges.

@lg2m

lg2m commented Oct 24, 2026

Copy link
Copy Markdown
Collaborator Author

@codex review

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 24, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-10-24T20:42:11.360714Z b66670c Manual request
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Already looking forward to the next diff.

Reviewed commit: ee6612cff6

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@lg2m
lg2m added this pull request to stack #462 October 24, 2026 13:10
@lg2m
lg2m force-pushed the fix/prompt-cache-key branch from ee6612c to b66670c Compare October 24, 2026 20:40
@lg2m

lg2m commented Oct 24, 2026

Copy link
Copy Markdown
Collaborator Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Breezy!

Reviewed commit: b66670c936

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@lg2m
lg2m merged commit a40ae13 into main Oct 25, 2026
7 checks passed
@lg2m
lg2m deleted the fix/prompt-cache-key branch October 25, 2026 00:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant