Repository navigation
fix(gateway): forward session-id to the Codex subscription endpoint - #2782
Merged
hidb4ai merged 8 commits intoNov 23, 2026
Merged
Conversation
The extraction entry sent with the static upstream key and the plain Responses renderer, so prefix extraction was turned off for every OAuth subscription. It now uses the same upstream address, subscription rendering and credentials as the model entry. A Codex subscription request carries no max_output_tokens, which that endpoint rejects. Other subscriptions keep normal extraction. Co-Authored-By: Claude <noreply@anthropic.com>
- A failed or cancelled terminal response arrives as HTTP 200 once the subscription stream is collapsed; the extraction entry now returns the provider error instead of an empty reply. - A requested output limit is refused with output_limit_unsupported instead of being dropped, since the Codex endpoint cannot enforce it. - Prefix extraction on Codex is enabled only when Core is confirmed as non-enforcing, because an enforcing Core withholds every request that replays encrypted reasoning; the gateway logs when the setting is not in effect. - Core maps the two new gateway refusals to output_limit_unsupported and credentials_unavailable. - The channel returns a named type and the auth error; tests cover the credentials and form of the extraction channel. - gateway/README.md and SUBSCRIPTIONS.md describe the Codex case. Co-Authored-By: Claude <noreply@anthropic.com>
The check for a failed terminal response changed how the extraction entry treats every 2xx reply, including the API-key path that predates this branch. The reply handling is the same as on master again. Co-Authored-By: Claude <noreply@anthropic.com>
…ix-extraction-oauth Both sides add a check, tests and fallback reasons to the extraction entry (oceanbase#2775 the continuation, this branch the Codex subscription); all of them are kept. Co-Authored-By: Claude <noreply@anthropic.com>
The Codex subscription endpoint routes its prompt cache by the session-id request header, not by prompt_cache_key. The gateway sent only the authorization and account headers, so requests of one session mostly missed each other's cache. The model entry now forwards the client's session-id on a Codex subscription. The kept prefix records it, and the extraction request sends the same value. Other upstreams do not receive the header. Co-Authored-By: Claude <noreply@anthropic.com>
…n-id An agent other than the Codex CLI may not send the session-id header but still sends prompt_cache_key, which Codex fills with the same value. On a Codex subscription the gateway now sends that key as session-id when the header is absent. The body is read for it only on that subscription. Co-Authored-By: Claude <noreply@anthropic.com>
…-session-routing oceanbase#2776 was squash-merged; master's tree equals the head this branch was built on, so the two files that conflict keep this branch's version. Co-Authored-By: Claude <noreply@anthropic.com>
jasper-yl
marked this pull request as ready for review
November 23, 2026 05:23
jasper-yl
requested review from
AlexStocks,
PsiACE and
frostming
as code owners
November 23, 2026 05:23
jasper-yl
added a commit
to jasper-yl/powercontext
that referenced
this pull request
Nov 23, 2026
List subscription reuse with the other capabilities on the first screen, note that Codex-subscription extraction reuses the prefix cache, and add a quick-start subsection on Server generation and gateway traffic with existing subscriptions, including their current limits. Written for the gateway behaviour once session-id forwarding (oceanbase#2782) lands. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
hidb4ai
approved these changes
Nov 23, 2026
hireview001
approved these changes
Nov 23, 2026
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.
Which issue or RFC does this PR close?
No issue. Follow-up to #2562 (OAuth subscriptions) and #2776 (prefix extraction on a Codex subscription).
Rationale for this change
On a Codex subscription the gateway sends two headers upstream:
authorizationandchatgpt-account-id. The Codex endpoint routes its prompt cache by thesession-idrequest header, which the Codex CLI sends on every request. Without it, requests of one session mostly do not reach the cache the earlier requests wrote, both for the agent's own requests and for prefix extraction (#2776).Measurement, directly against the endpoint, without the gateway (
gpt-5.6-sol; request A of about 16,500 input tokens, then B = A plus the reply and one user message; fresh content and a freshprompt_cache_keyper trial; a hit means B's cached tokens cover A):thread-idonlysession-id, alone or with other headersprompt_cache_keywas the same in A and B in every trial. A delay of 0, 3, 10 or 30 seconds between A and B andtool_choicemade no difference.Magpie records the same behaviour of this endpoint independently (its
internal/provider/codex_request.go,sessionHeaders: with the body key alone a request was cached 0 tokens six times in a row, with the headers 7,552 of 7,659 from the second request on) and setssession-idfromprompt_cache_key. No code is taken from it.What changes are included in this PR?
session-idheader is forwarded upstream. A request without it uses the body'sprompt_cache_keyassession-id: Codex sends the same value in both, and an agent that does not send the header often still sends the key. The value must be 1 to 256 visible ASCII characters, the rule the gateway already applies to conversation headers; otherwise no session header is sent.gateway/SUBSCRIPTIONS.md.Are there any user-facing changes?
Codex subscription traffic through the gateway gets prompt cache hits where it mostly did not. The
session-idvalue now reacheschatgpt.com; the Codex CLI sends the same header there when it is used without the gateway. It is not sent to Core.How was this change tested?
Automated
cargo test --locked -p pc-gateway-app -p pc-gateway-core: all pass. New or extended tests: the Codex channel carries the session header; the value comes from the header or else from the body's cache key; it is absent without either and on an API-key upstream; a kept prefix records the session ID of its request.cargo fmt --checkandcargo clippy --locked -p pc-gateway-app -p pc-gateway-core --all-targets -- -D warnings: clean.Live, through the gateway with a real Codex subscription (Codex CLI 0.169.0,
gpt-5.6-sol, three turns that each read a file of about 14,000 tokens, memory flushed after each turn)Prefix extraction request, input tokens and cached tokens:
The agent's own follow-up requests in those turns were cached up to the previous request in 5 of 6 cases (for example 24,192 of 24,390); one got only the shared 7,936-token base.
Fallback to
prompt_cache_key, through the gateway, same A/B requests sent without asession-idheader: withprompt_cache_keyin the body B hit A's cache in 4 of 4 trials (for example 16,640 of 16,859); with neither in 0 of 2.The samples are small. They show the effect of the header, not a hit rate to rely on.
@PsiACE This changes what the Codex channel from #2562 sends upstream. Is there a reason it should not forward
session-id?中文摘要
Codex 订阅端点按请求头
session-id路由提示缓存,请求体里的prompt_cache_key在这里不起这个作用。网关的 Codex 订阅通道只发送令牌和账号两个头,所以同一会话的请求大多落不到之前写入的缓存上。直接对端点的测量:不带会话头 2/12 命中,只带thread-id0/4,带session-id14/15。本 PR 在 Codex 订阅下把请求的session-id转发给上游(请求没有这个头时用请求体的prompt_cache_key),保留的前缀记录这个值,抽取请求带同一个值;其他上游不转发。经网关的真实验证中,前缀抽取请求三次都命中了约 94% 的输入。AI usage statement
Built with Claude Code (Claude Fable 5): the measurement, code, tests, docs and this description. I reviewed the change and the results.
🤖 Generated with Claude Code