Repository navigation
Description
Client or integration
Codex App
Area
Provider adapter
Summary
Long Codex tasks routed through the Anthropic Messages adapter can repeatedly rewrite a large prompt cache even when most historical input is unchanged:
- Routed compaction removes the tool catalog before requesting a summary. That changes the cached prefix independently of the later, necessary replacement of history with a summary.
- Image normalization can demote historical images under aggregate pressure; the 413 recovery path rebuilds with tighter image tiers. Both change already-sent image bytes.
- A separate, expected source of rewrites is five-minute TTL expiry while a main or child turn is still running tools. There is currently no opt-in running-turn renewal. This is not evidence of a prefix-rewrite bug by itself.
Expected: retain unchanged historical image encodings and the Anthropic system/tool prefix, prevent tools from executing during summaries, and compact old complete history units only when the actual request limit requires it. Optional renewal should protect verified running main/child turns and stop on completion or unknown state, without modifying Codex or its transcript.
Reproduction
- Route Codex Responses requests to Anthropic with short cache retention. Include client tools and a growing image history.
- Trigger routed compaction. Compare the ordinary request's tool catalog with the summary request: the current
createResponsesEffectsremovescontext.tools. - Append enough valid images to exceed the aggregate image budget, or return an upstream 413. Compare historical image payloads before/after normalization or the biased retry.
- Separately, leave a turn running a long tool for more than five minutes. Its next natural request may legitimately miss expired cache. Do not conflate this with steps 2–3.
An offline regression against the current dev implementation reproduces step 2: the expected unchanged tool catalog becomes undefined. Public regression fixtures use generated PNGs, synthetic large rollout records, fake clocks and fake upstreams. No paid provider call is needed for these wire/lifecycle checks.
Version
Observed on installed 2.86.0. The compaction-prefix regression was also reproduced against dev at 8640c2f30edbd2fbb6947ed5c25e2793f5ae72a6 (2.89.0).
Operating system
Windows 11, build 26200
Provider and model
Anthropic Messages adapter / claude-opus-5-5
Logs or error output
Sanitized offline result from the unchanged dev compaction implementation:
compaction preserves client tool and system cache prefixes without changing tool choice
Expected: [ { name: "read", ... } ]
Received: undefined
0 pass, 1 fail
Low hit rates alone do not establish the cause. Some historical low-hit requests remain unattributed and are not claimed as fixed by this report.
Screenshots and supporting files
Related prior work: #4532 and closed, unmerged #4549 address stable image encoding. This report additionally covers compaction-prefix preservation, continued image-task recovery at real limits, and optional running-turn renewal. Private conversations, raw logs and actual user images are intentionally not attached.
Redacted configuration
{
"cacheRetention": "short",
"providers": {
"anthropic": {
"adapter": "anthropic",
"baseUrl": "https://api.anthropic.com",
"authMode": "key",
"defaultModel": "claude-opus-5-5"
}
}
}Authentication material is omitted. OAuth and custom provider names also need isolated ownership; they must not borrow a parent's thread as a child's own identity.
Checks
- I searched existing issues and documentation.
- I removed secrets, tokens, account details, request credentials, and personal data.
Activity
Ingwannu commentedon Oct 18, 2026
Tracking the proposed implementation in Draft #7308. At dev
8640c2f30edb, the prefix-rewrite paths and the ordinary short-cache expiry need separate acceptance criteria: preserving an unchanged prefix must not depend on enabling renewal. Please keep regression receipts for tool/system preservation and historical image-byte stability separate from the optional running-turn renewal receipts (ended/unknown/expired turns produce no renewal). The draft PR is not a shipped fix; this issue stays open while its runtime-ownership, image recovery and validation gates are reviewed.OctopusEngineering commentedon Oct 24, 2026
Field data from a shared Mac mini running Codex Desktop through opencodex 2.84.0 (Anthropic Messages adapter,
claude-opus-5-5, default short cache retention). Aggregate numbers only; no transcripts or images attached.Over 30 days we looked at ~59k Anthropic requests. About 40% of relative input cost (uncached 1, cache read 0.1, cache write 1.25) came from requests that re-wrote most of the conversation into the cache. Classifying those full rewrites:
Image history correlates strongly with the prefix-change bucket. The large-rewrite rate per request was 3.8% when the conversation held 20 or fewer images. It rose to 20–38% once it held more than 100 images. That matches the age-tier demotion and textification described above. Page-by-page visual QA sessions (hundreds of screenshots) were the worst case.
We also simulated
cacheRetention: "long"on the same traffic: about -1% net, because the higher write price cancels most of the avoided TTL rewrites. So for us the image-byte stability and compaction-prefix parts of this issue matter more than renewal. For reference, we have since loweredproviders.anthropic.modelAutoCompactTokenLimitsto 400000 locally, so threads stop growing toward 900k between compactions.Happy to rerun the measurement against a build with #7308 if that helps validate the image-stability gate.
fix(anthropic): preserve cache prefixes during routed compaction (#7749)
lidge-jun commentedon Oct 26, 2026
Release triage for v2.93.0: the narrow prefix-preservation fix is merged into
devvia #7749 (squash162beb3d1509291e52f808bd4f11e389bf17c06f), carried from #7308 with credit to @S0RYUASUKA.What changed: when Codex triggers routed compaction on the Anthropic Messages adapter, the summary request now keeps the client tool catalog, system prompt and tool options byte-identical to the preceding ordinary request, so the cached tools/system prefix survives. Hosted server tools, or a mandatory tool choice, make the adapter send
tool_choice: none; the tools and system are still built from the caller's choice, so their bytes match. A summary guard drops tool events and fails a summary that attempts a tool call or is empty. Other adapters keep the old behaviour. Image normalization and the HTTP 413 one-shot tighter-image retry are unchanged, as the triage asked.Still open, deferred until after the release: historical image-byte stability under aggregate image pressure and 413 bias (step 3 of the report), and the optional running-turn cache renewal. Both remain with #7308, which stays open. A request that advertises a sidecar web-search tool (for example Exa) also still changes its tool prefix on compaction; this is documented. Keeping this issue open for those parts.
Brams75 commentedon Nov 8, 2026
Additional field data for the still-open image part (historical image-byte stability under aggregate image pressure), from Windows Codex Desktop through opencodex 2.90.0 (Anthropic Messages adapter,
claude-opus-5-5, default 1h retention for first-party Anthropic). Aggregate numbers only; no transcripts or images.Method: 7 days, measured per agent from Codex rollout
token_usage_recordlines (so subagents sharing a conversation key are not mixed), 666 threads and subagents, 31,701 Anthropic requests. A "rewrite" here is a request that writes more than half of its input to the cache less than 5 minutes after the previous request of the same agent, with input >= 80% of the previous one (so TTL expiry, compaction and thread start are excluded).applyPromptCaching(tools, system, penultimate and last user message): once an old image is textified or demoted, no breakpoint sits before it, so the whole tail is rewritten.anthropic-image-guard.ts,anthropic-image-normalize.ts,anthropic-image-codec.ts) are byte-identical between 2.90.0 and 2.99.0, so updating does not change this.Suggestion: hysteresis when dropping or demoting. When the count reaches 100 (or the base64 budget is exceeded), textify or demote the oldest images down to a low-water mark (for example 60 images, or about 70% of the budget) in one step, and keep that state stable until the high-water mark is hit again. The prefix is then rewritten once per ~40 screenshots instead of on every new screenshot. With the numbers above that would remove most of the >100 bucket rewrites (161 down to about 5).
Happy to rerun the same measurement against a build with such a change.
bla3416 commentedon Nov 12, 2026
Field data and a likely remaining cause on v2.97.0, which includes #7749. The summary request still does not share the cached prefix with the ordinary request before it.
Setup: opencodex 2.97.0 (npm), codex-cli 0.166.0 on Linux, Anthropic Messages adapter (OAuth). Models:
claude-opus-5-5(Codex Desktop, effort max) andclaude-sonnet-5-5(codex exec, effort low). The cache writes are 1h. There are no images. Requests are seconds apart, so TTL expiry is not involved.Minimal repro (codex exec). Make two generated text files of about 115 KB each, then run:
Usage from
ocx logs(input / cache read / cache write):I ran the same repro again within the hour. Both summary requests read exactly 9,844 tokens. They hit the prefix of the earlier summary request, never the ordinary prefix.
Codex Desktop session (Opus 5.5, max):
What Codex sends. I captured the Codex → opencodex bodies with a local pass-through logger. The summary request matches the ordinary request in
tools(same hash),instructions,prompt_cache_key,reasoning,include,parallel_tool_callsand the leadinginputitems. The history is unchanged, and the tail is{"type":"compaction_trigger"}. So the divergence comes from the opencodex side.Likely cause. Codex's ordinary tool list includes
{"type":"web_search"}, next toexec,wait,request_user_input,request_user_input_asyncandclock. Insrc/server/responses/response-effects.ts, the routed-compaction branch runsdelete parsed._webSearchunconditionally, before thepreserveCompactionPrefixcheck. Without_webSearch,planWebSearch()does not activate the web-search sidecar. The summary request is then built without the web-search tool that ordinary turns carry. Tools are the first part of the Anthropic cache prefix, so the whole prefix misses. This would also explain why later summary requests hit only each other's ~10K prefix. I have not inspected the upstream request bytes directly, so please treat this as the leading hypothesis.Impact. In one Desktop session (42 minutes, 135 requests), the two compactions and the two requests right after them were about 23% of the session's relative cost (cache read 0.1, 1h write 2.0, output 5.0). If the summary requests had reused the ordinary prefix, about 17% of the session's cost would have been saved.
Suggestion. When
preserveCompactionPrefixis true, keep the web-search tool definition in the summary request and skip only the sidecar execution. The summary guard already rejects tool calls.lidge-jun commentedon Nov 19, 2026
Status from the L5 maintainer lane: the owner-accepted image-413 recovery from #7308 landed in #8927 (
e4ea3c6433). It is off by default and enabled per provider. It allows one tool-disabled summary and one smaller resend, pinned to the credential and endpoint that refused the request, with separate spend accounting for each send. The prefix-preservation half landed earlier in #7749.This issue stays open for the remaining evidence. Two reports are still unexplained: the summary request on v2.97.0 does not share the cached prefix with the ordinary request before it (@bla3416), and historical image bytes are unstable under aggregate image pressure (@Brams75). A fresh trace on a build that includes #8927 would help. Please include the cache read and write tokens for the turn before the 413 and for the recovery sends, so we can tell whether a separate prefix fix is still needed.