Skip to content

fix(gateway): forward session-id to the Codex subscription endpoint - #2782

Merged
hidb4ai merged 8 commits into
oceanbase:masterfrom
jasper-yl:fix/gateway-codex-session-routing
Nov 23, 2026
Merged

hidb4ai merged 8 commits into
oceanbase:masterfrom
jasper-yl:fix/gateway-codex-session-routing

Conversation

@jasper-yl

@jasper-yl jasper-yl commented Nov 23, 2026 •

Copy link
Copy Markdown
Contributor

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: authorization and chatgpt-account-id. The Codex endpoint routes its prompt cache by the session-id request 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 fresh prompt_cache_key per trial; a hit means B's cached tokens cover A):

Headers sent B hit A's cache
no session header (what the gateway sends today) 2 of 12
thread-id only 0 of 4
session-id, alone or with other headers 14 of 15

prompt_cache_key was the same in A and B in every trial. A delay of 0, 3, 10 or 30 seconds between A and B and tool_choice made 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 sets session-id from prompt_cache_key. No code is taken from it.

What changes are included in this PR?

  • Model entry. On a Codex subscription, the request's session-id header is forwarded upstream. A request without it uses the body's prompt_cache_key as session-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.
  • Prefix extraction. The kept prefix records the session ID the request carried, and the extraction request sends the same value.
  • Other upstreams do not receive the header: API-key upstreams and the other subscriptions are unchanged.
  • Docs. 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-id value now reaches chatgpt.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 --check and cargo 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:

Run Input Cached
Before (#2776) 9 runs 13,985 to 26,339 23,808 once; 7,936 four times; 0 four times
After 1 25,485 23,808
After 2 25,916 24,320
After 3 25,580 23,936

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 a session-id header: with prompt_cache_key in 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-id 0/4,带 session-id 14/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

jasper-yl and others added 7 commits November 22, 2026 08:35
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
jasper-yl marked this pull request as ready for review 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
hidb4ai merged commit 7b1e78a into oceanbase:master Nov 23, 2026
43 checks passed
@hidb4ai hidb4ai added the 1.6.0 v1.6.0 release tracking label Nov 23, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

1.6.0 v1.6.0 release tracking

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants