Skip to content

fix(codex): enable 24h prompt cache retention on Bedrock Mantle - #70083

Closed
israellot wants to merge 5 commits into
NousResearch:mainfrom
israellot:fix/gpt55-prompt-cache-retention
Closed

fix(codex): enable 24h prompt cache retention on Bedrock Mantle#70083
israellot wants to merge 5 commits into
NousResearch:mainfrom
israellot:fix/gpt55-prompt-cache-retention

Conversation

@israellot

@israellot israellot commented Jul 23, 2026

Copy link
Copy Markdown
Contributor

What & why

Amazon Bedrock Mantle supports extended prompt-cache retention for documented pre-GPT-5.6 models, but Hermes' Responses transport did not select it. At the same time, prompt_cache_retention is not part of every OpenAI-compatible Responses implementation; automatically sending it to endpoints such as GitHub Models can cause compatibility failures.

This PR deliberately applies the automatic prompt_cache_retention: "24h" default only when both conditions hold:

  1. The parsed hostname exactly matches the public Mantle form bedrock-mantle.<region>.api.aws.
  2. The model is documented to support extended retention.

Other endpoints—including direct OpenAI, Azure OpenAI, xAI, GitHub Models, Copilot, ChatGPT Codex, and arbitrary compatible relays—do not receive the automatic field.

Implementation

  • Adds a shared request-capability helper used by both main and auxiliary Responses paths.
  • Passes the resolved base URL into the main Responses transport.
  • Parses and validates the hostname rather than using substring matching; suffix and path spoof cases remain excluded.
  • Preserves explicit caller overrides and existing prompt-cache-key behavior.

Models

The model gate covers the documented extended-retention set: GPT-5.5/Pro, GPT-5.4, GPT-5.2, GPT-5.1 and its listed variants, GPT-5/Codex, and GPT-4.1, including provider-prefixed and dated IDs. GPT-5.6+ and unknown model families remain excluded.

Tests

bash scripts/run_tests.sh tests/agent/test_auxiliary_client.py tests/agent/transports/test_codex_transport.py tests/run_agent/test_run_agent_codex_responses.py -q

Result: 544 passed, 0 failed.

Also verified:

python scripts/check-windows-footguns.py agent/transports/codex.py agent/auxiliary_client.py agent/codex_responses_adapter.py agent/chat_completion_helpers.py
git diff --check

OpenAI documents GPT-5.5 / GPT-5.5 Pro as extended-cache-only: in-memory
prompt cache retention is not available for them, and only
prompt_cache_retention: "24h" is supported. Responses requests that omit
the field see near-zero cached_tokens even with a stable prompt_cache_key
and identical prefixes (observed on an OpenAI-compatible Responses relay:
0 cached across repeated identical calls before; 97% cache reads after).

Send the field for the gpt-5.5 model family (bare and namespaced ids like
openai.gpt-5.5) on OpenAI-compatible Responses routes, mirrored in the
auxiliary Codex adapter, and pass it through preflight normalization.
Skipped for xAI, GitHub/Copilot, and the chatgpt.com Codex backend, which
reject or ignore body-level cache fields.
@alt-glitch alt-glitch added type/bug Something isn't working comp/agent Core agent runtime: loop, agent_init, prompt builder, context-compression, responses endpoint provider/openai OpenAI / Codex Responses API area/usage-cost Token accounting, usage reporting, billing, cost tracking P0 Critical — data loss, security, crash loop sweeper:risk-caching Sweeper risk: may break/degrade prompt caching or cache-key stability (invariant) labels Jul 23, 2026
@israellot

israellot commented Jul 23, 2026

Copy link
Copy Markdown
Contributor Author

Scope update: the automatic prompt_cache_retention: "24h" default is now intentionally limited to requests whose parsed hostname matches bedrock-mantle.<region>.api.aws and whose model supports extended retention.

This positive endpoint gate supersedes the earlier broad compatible-endpoint behavior. Direct OpenAI, Azure OpenAI, GitHub Models, Copilot, xAI, ChatGPT Codex, and arbitrary compatible relays do not receive the automatic field.

@egilewski

Copy link
Copy Markdown
Contributor

suggesting changes

agent/auxiliary_client.py does not implement the advertised
chatgpt.com/Codex-backend exclusion. _CodexCompletionsAdapter checks only xAI
and GitHub/Copilot hosts before adding prompt_cache_retention, so its default
https://chatgpt.com/backend-api/codex path now sends
prompt_cache_retention="24h" for gpt-5.5. That bypasses the equivalent
not is_codex_backend guard in the main transport.

An invocation-level adapter probe emitted no retention field on current main,
but emitted "24h" for the chatgpt.com Codex backend on this head; the intended
OpenAI-compatible endpoint still received "24h" and the xAI/GitHub negative
controls stayed unset. Since the PR states that this backend rejects or ignores
body-level cache fields and uses header-based affinity, please add the equivalent
auxiliary-path exclusion and a regression case using the existing test helper's
default chatgpt.com URL.

Security evidence:

  • trust boundary: auxiliary Responses kwargs cross into provider-specific
    endpoints with different accepted request bodies.
  • source/sink/invariant: gpt-5.5 plus the auxiliary client's base URL reaches
    responses.create; chatgpt.com Codex requests must not receive body-level
    cache-retention fields.
  • current-main reproduction: a direct adapter invocation against the
    chatgpt.com Codex URL left prompt_cache_retention unset.
  • PR-head or patch-replay validation: the same invocation on this head sent
    prompt_cache_retention="24h".
  • positive/negative cases: the intended OpenAI-compatible route received
    "24h" while xAI and GitHub/Copilot stayed unset; chatgpt.com failed its
    negative case.
  • residual bypass search: the main transport has not is_codex_backend; the
    auxiliary emission site has no equivalent guard.
  • reviewer validation: all four new focused tests pass, but none checks
    retention against the test helper's default chatgpt.com Codex URL.

Signed: GPT-5.6-sol-xhigh in Codex

@israellot

israellot commented Jul 23, 2026

Copy link
Copy Markdown
Contributor Author

Addressed in ecd2e92.

  • Added the auxiliary-path chatgpt.com/backend-api/codex exclusion before setting prompt_cache_retention.
  • Added a regression test using _build_adapter()'s default Codex backend URL and asserting that the retention field is absent.
  • Preserved the positive OpenAI-compatible relay case and the existing xAI/GitHub negative controls.

Verification:

scripts/run_tests.sh tests/agent/test_auxiliary_client.py tests/agent/transports/test_codex_transport.py -q

Result: 407 passed, 0 failed across both focused test files.

@egilewski

Copy link
Copy Markdown
Contributor

suggesting changes

The auxiliary Responses path still sends prompt_cache_retention: "24h" to GitHub Models at https://models.github.ai/inference. _CodexCompletionsAdapter.create() classifies only githubcopilot.com as GitHub, although the main transport explicitly classifies both models.github.ai and githubcopilot.com; a GPT-5.5 capture probe against models.github.ai therefore emitted the field. Please extend the auxiliary GitHub-host check to cover models.github.ai and add the corresponding negative regression.

The exact PR patch replayed cleanly on current main, the supported relay and namespaced GPT-5.5 Pro cases emitted "24h", the other tested incompatible backends stayed unset, and the two focused files passed 423 tests.

Security evidence:

  • trust boundary: Auxiliary Responses kwargs cross into provider-specific endpoints with different accepted cache controls.
  • source/sink/invariant: _CodexCompletionsAdapter.create() forwards kwargs to responses.create; retention must remain absent on every GitHub Responses host recognized by the main transport.
  • current-main reproduction: the current-main auxiliary probe emitted no retention field because the feature was absent.
  • PR-head or patch-replay validation: the current-main patch replay emitted "24h" for an auxiliary GPT-5.5 request whose base URL was https://models.github.ai/inference.
  • positive/negative cases: the relay and namespaced GPT-5.5 Pro cases passed, and chatgpt.com Codex, xAI, githubcopilot.com, and main-transport GitHub cases stayed unset, but auxiliary models.github.ai did not.
  • residual bypass search: the main route checks both GitHub host families, while the changed auxiliary emission site checks only githubcopilot.com.
  • reviewer validation: the focused suite passed 423 tests, the direct auxiliary capture probe reproduced the uncovered host, and CodeRabbit completed with no findings.

Signed: GPT-5.6-sol-xhigh in Codex

@israellot

Copy link
Copy Markdown
Contributor Author

Addressed in f89b2f3.

  • Extended the auxiliary GitHub-host check in _CodexCompletionsAdapter to cover models.github.ai in addition to githubcopilot.com, matching the main transport's classification in agent/chat_completion_helpers.py.
  • Added test_prompt_cache_retention_skipped_for_github_models_host, a negative regression asserting the retention field is absent for an auxiliary GPT-5.5 request with base URL https://models.github.ai/inference.

Verification:

scripts/run_tests.sh tests/agent/test_auxiliary_client.py tests/agent/transports/test_codex_transport.py -q

Result: 408 passed, 0 failed across both focused test files (includes the new negative case).

@egilewski

Copy link
Copy Markdown
Contributor

looks mergeable

The latest commit addresses the remaining auxiliary GitHub Models gap: models.github.ai now follows the same cache-retention exclusion as githubcopilot.com, while the supported OpenAI-compatible route still receives prompt_cache_retention: "24h".

The exact PR head passed the two focused test files (408 tests), and the patch replayed cleanly onto current main where the same files passed 440 tests. A direct transport probe on that replay emitted "24h" for a namespaced GPT-5.5 Pro relay request and omitted the field for xAI, GitHub Responses, and the chatgpt.com Codex backend.

Security evidence:

  • trust boundary: Responses kwargs cross into provider-specific endpoints with different accepted cache controls.
  • source/sink/invariant: GPT-5.5-family requests should receive 24h only on supported Responses endpoints; incompatible provider routes must omit it.
  • current-main reproduction: current main omitted the field because the feature is absent.
  • PR-head or patch-replay validation: the exact head and current-main patch replay both passed their focused suites.
  • positive/negative cases: the relay emitted 24h; xAI, GitHub/Copilot, GitHub Models, and chatgpt.com Codex stayed unset.
  • residual bypass search: the changed main, auxiliary, and preflight paths now preserve the same host exclusions for this field.
  • reviewer validation: CodeRabbit completed with no findings in the clean-pass flow.

Signed: GPT-5.6-sol-xhigh in Codex

@israellot israellot changed the title fix(codex): send prompt_cache_retention 24h for the GPT-5.5 family fix(codex): enable 24h prompt cache retention for supported models Jul 24, 2026
@israellot

israellot commented Jul 24, 2026

Copy link
Copy Markdown
Contributor Author

Superseded by 3c07966.

The implementation now uses a positive endpoint capability gate: automatic 24-hour retention is emitted only for supported models on bedrock-mantle.<region>.api.aws. It is omitted for direct OpenAI, Azure OpenAI, GitHub Models, Copilot, xAI, ChatGPT Codex, custom relays, and hostname/path spoof cases.

Focused result: 544 tests passed, 0 failed.

@israellot israellot changed the title fix(codex): enable 24h prompt cache retention for supported models fix(codex): enable 24h prompt cache retention on Bedrock Mantle Jul 24, 2026
@israellot

Copy link
Copy Markdown
Contributor Author

Narrowed the fix in 3c07966.

Instead of maintaining a denylist of incompatible Responses endpoints, the transport now automatically sends prompt_cache_retention: "24h" only when:

  • the parsed hostname exactly matches bedrock-mantle.<region>.api.aws; and
  • the model is in the documented extended-retention set.

This means GitHub Models, Copilot, xAI, ChatGPT Codex, direct OpenAI, Azure OpenAI, custom relays, and spoofed host/path forms are excluded by default. Main and auxiliary request paths share the same gate.

Verification: 544 tests passed, 0 failed, plus the Windows-footgun scan and git diff --check.

@egilewski

Copy link
Copy Markdown
Contributor

suggesting changes

agent/transports/codex.py now injects prompt_cache_retention: "24h" for GPT-5.5 and earlier on Bedrock Mantle, but AWS currently documents those models' caching as automatic, requiring no special parameter, and does not document either this field or a 24-hour TTL for them: https://docs.aws.amazon.com/bedrock/latest/userguide/prompt-caching.html

The current-main invocation omits the field; this head's replay emits and preserves it through preflight. The 576 focused tests pass, but they only prove request construction, not that Mantle accepts the field or provides 24-hour retention. Please remove the injected parameter, or add authoritative AWS contract evidence plus a Mantle-level regression/E2E showing that the field is accepted and produces the claimed retention behavior.

Security evidence:

  • trust boundary: Hermes sends this provider-specific field to AWS Bedrock Mantle.
  • source/sink/invariant: the model and Mantle hostname select a field that must be accepted by AWS and produce the claimed retention.
  • current-main reproduction: openai.gpt-5.5 on the exact Mantle hostname omits prompt_cache_retention.
  • PR-head or patch-replay validation: the same invocation on this patch emits and preflight-preserves "24h".
  • positive/negative cases: 576 focused tests passed; non-Mantle hosts, spoofed hosts, and GPT-5.6 omit the field.
  • residual bypass search: host/model scoping is narrow; the remaining issue is the undocumented target-service contract.
  • reviewer validation: no Bedrock credentials were available for a live request, so AWS documentation is the provider-contract evidence.

Signed: GPT-5.6-sol-xhigh in Codex

@teknium1

Copy link
Copy Markdown
Contributor

Merged via #70992 with your authorship preserved on all three commits — 24h retention scoped to Bedrock Mantle supported models with the models.github.ai auxiliary exclusion. Thanks for working through the review rounds.

@teknium1 teknium1 closed this Jul 24, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area/usage-cost Token accounting, usage reporting, billing, cost tracking comp/agent Core agent runtime: loop, agent_init, prompt builder, context-compression, responses endpoint P0 Critical — data loss, security, crash loop provider/openai OpenAI / Codex Responses API sweeper:risk-caching Sweeper risk: may break/degrade prompt caching or cache-key stability (invariant) type/bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants