Skip to content

feat(server): skip cache writes on cold Claude compaction - #6

Closed
Alb11747 wants to merge 0 commit into
mainfrom
feat/cold-compaction-cache-policy
Closed

Alb11747 wants to merge 0 commit into
mainfrom
feat/cold-compaction-cache-policy

Conversation

@Alb11747

@Alb11747 Alb11747 commented Oct 11, 2026 •

Copy link
Copy Markdown
Owner

Fork-only feature. Proposed upstream in pingdotgg#18069; no upstream PR.

Problem

When a large Claude thread has been idle past its prompt cache, /compact (the Compact chip, Compact context, or a typed /compact) writes the whole old history back to the cache, even though the summary replaces it. Claude Code's skipCacheWrite only moves the tail breakpoint, so the earlier history keeps its cache markers. In one measured Opus cold compaction, that write was about half the cost.

Change

  • New Claude instance setting, Skip cache writes on cold compaction, off by default.
  • When it is on, ClaudeAdapterV2.compactThread treats a compaction as cold only if both of these hold:
    • the provider thread's last turn activity was at least 70 minutes ago (the same threshold as the web resume offer, now shared from @t3tools/shared/claudeCompaction);
    • the thread has no live CLI process. Idle release normally closes it after 30 minutes, so the compaction starts a process anyway.
  • A cold compaction opens its process with DISABLE_PROMPT_CACHING=1 in that query's env only, plus a distinct query-policy key.
  • The next non-continuation turn computes the normal key, so the existing replacement path reopens Claude (resume) with normal env and caching. That costs one extra process start.
  • The same process also appends X-CPA-Prompt-Cache-Mode: explicit to ANTHROPIC_CUSTOM_HEADERS, keeping any headers the instance already sets. CLIProxyAPI treats Agent SDK clients as non-native and adds its own cache markers to markerless requests. A proxy build that maps this header onto its existing explicit cache mode (prompt_cache_options.mode) adds none. Other gateways don't use the header.
  • A live process is never replaced for this, since it may run background work. Those compactions, and every warm or default compaction, behave as before.
  • ProviderTurnStartService passes lastProviderTurnActivityAt (the latest provider turn completion, or start for a turn that never completed) through ProviderAdapterV2TurnInput. The adapter loses in-memory state on idle release, so it can't track this itself.
  • Transcripts are untouched, so branching with the standalone SDK forkSession still restores the full pre-compaction history.

Surfaces

  • Web and desktop: the switch appears in the Claude instance settings, which are schema-driven.
  • Mobile, MCP agents and scheduled tasks: no settings form, but the decision is server-side, so compactions they start follow the setting.
  • Other providers: unaffected.
  • Not covered: Claude Code's own resume_return dialog compacts inside the productive process, so this setting doesn't apply to it.

Verification

  • Adapter tests:

    • A cold compaction opens with the flag, and other env is kept.
    • After both a successful and a failed compaction, the next turn reopens without the flag.
    • A live process with background work is kept, along with its caching.
    • Setting off: no flag.
    • 69 minutes idle: no flag.
    • Mutation check: without the key split, the uncached process was reused for the next turn and the tests failed.
  • Integration test: the timestamp is forwarded from the projection.

  • Other tests: the settings default, and the settings form field list.

  • Typechecks: shared, contracts, provider-core, server and web. Lint and format pass on the changed files.

  • Mock end to end (loopback, dummy keys; done by GPT-6.1-Sol), using Claude Code 2.1.296 /compact with this env:

    • The summarizer request carried the header and 0 cache markers, and the next normal turn had markers again.
    • Through a CLIProxyAPI build with the header patch (cloaking on), the upstream request had 0 markers, and neither the header nor prompt_cache_options reached upstream.
    • The unpatched deployed proxy build added 3 markers.

Not verified / limitations

  • Marker removal is confirmed only at the CLI boundary, from earlier synthetic tests with Claude Code 2.1.296 / SDK 0.3.276. There is no paired live cold test.
  • No real-API savings measured. Through CLIProxyAPI this needs the separate proxy patch (local branch feat/claude-prompt-caching-optout, not deployed). Other gateways that add default cache_control would still cache the compaction.
  • The summary itself is still cached. It becomes cached input on the next request, so caching it is not lost.
  • 70 minutes is a heuristic. If another thread kept the same prefix warm, that history is billed as ordinary input instead of a cheap cache read.
  • Pro/Max quota weighting of cache writes is unknown.

Landing

The second commit is fixup! for the docs(fork) change list. Land with GIT_SEQUENCE_EDITOR=true git rebase -i --autosquash "$(git merge-base HEAD fork/upstream-main)", then push main per fork/README.md.

🤖 Generated with Claude Code (Claude Opus 5.5 in T3 Code)

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 11, 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-11T03:30:54.366663Z 0e4a85d PR opened
ℹ️ 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 chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 0e4a85d549

ℹ️ 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".

Comment thread fork/CHANGES.md Outdated
@@ -17,4 +17,5 @@ an entry.
- `fix(server): detect Windows npm provider binaries for updates`: Codex instances configured with npm's native executable can update through T3, and manual-only installs no longer suggest an unavailable update action. Issue: none linked. PR: none yet
- `feat(web): download files from chat file links`: the context menu on chat file links offers "Download file" for any host file the connection can read, including files outside the workspace, so remote clients can fetch agent output. Issue: none linked. PR: none yet
- `fix(fork): show release deltas and link fork notes`: release notes list upstream changes and only new or updated fork patches since the previous published build, even after rebases; desktop release links follow the configured updater repository. Issue: none linked. PR: none yet
- `feat(server): skip cache writes on cold Claude compaction`: opt-in Claude setting **Skip cache writes on cold compaction** runs a compaction of a thread idle 70+ minutes, whose Claude process was already released, without prompt caching, so the history the summary replaces is not cached again; the next message reopens Claude with caching (one extra process start). Saves usage only if every gateway or proxy forwards requests without cache markers unchanged. Issue: https://github.com/pingdotgg/t3code/discussions/18069 (was https://github.com/pingdotgg/t3code/issues/18067). PR: none yet

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Fold this entry into the docs commit

This feature commit directly modifies fork/CHANGES.md, but fork/README.md:98-104 requires all entries to be folded into the single docs(fork) commit. Leaving the entry here gives feature commits separate ownership of this file, making an upstream sync conflict-prone when a patch is dropped or reordered; move it with the documented fixup/autosquash workflow.

AGENTS.md reference: AGENTS.md:L1-L3

Useful? React with 👍 / 👎.

@Alb11747

Copy link
Copy Markdown
Owner Author

Landed on fork main as 0d299fcd8 (change-list entry folded into the docs(fork) commit). Release run: https://github.com/Alb11747/t3code/actions/runs/38121216140

@Alb11747 Alb11747 closed this Oct 11, 2026
@Alb11747
Alb11747 deleted the feat/cold-compaction-cache-policy branch October 11, 2026 07:24
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