feat(models): a caller can keep the cache breakpoint off per-request rows - #50
Merged
Conversation
…rows A prompt-cache entry is only worth writing if the NEXT request can read it, which requires its hashed prefix to be bytes that request still contains. When a caller appends per-request content after the durable conversation — a clock, a budget figure, anything derived from "now" — the unconditional last-message breakpoint lands on exactly the row the next request cannot reproduce, so every request writes a fresh entry and reads none. `CACHE_EXEMPT_KEY` is a neutral per-item key on the caller's own message dict (the `defer_loading` shape) marking a row as per-request. The marker then lands on the last durable row, which the next request contains by construction since durable rows only ever grow by append. Counted from the end and stopping at the first unmarked row: the exemption is about position, so a marked row with durable rows after it is not a tail. An all-exempt list writes no marker at all. Measured on Bedrock/Opus 5, single-step turns with tools present: marker on the volatile row: turn 1 write=8211 -> turn 2 write=8226 read=0 marker on the last durable row: turn 1 write=8425 -> turn 2 write=15 read=8425 With no exempt row the payload is byte-identical to before, so no existing caller changes behaviour. Twinned on the OAuth leg, which is not a subclass and has its own payload builder. The key never reaches the wire — both builders rebuild rows with only the wire-legal fields.
|
Caution The consumer version of Gemini Code Assist on GitHub has been sunset. All code review activity has officially ceased. |
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.
Why
A prompt-cache entry is only worth writing if the next request can read it, which requires its hashed prefix to consist of bytes that request still contains.
mark_conversation_tail_for_cachemarks the last content block of the last message, unconditionally. When a caller appends per-request content after the durable conversation — a clock line, a budget figure, anything derived from "now" — the marker lands on exactly the row the next request cannot reproduce. Every request then writes a fresh entry and reads none, silently, forever.Measured on Bedrock /
anthropic.claude-opus-5, single-step turns,toolspresent:write=8211write=8226read=0write=8425write=15read=8425Note the
toolsarray is load-bearing in any probe of this: without one, a messages-tier marker writes entries that never become readable, which produces false negatives.What
CACHE_EXEMPT_KEY— a neutral per-item key riding the caller's own message dict, following thedefer_loadingprecedent (the framework's nearest analogous feature also took no new request parameter). A caller marks its per-request rows; the marker moves back to the last durable row, which the next request contains by construction since durable rows only ever grow by append.Semantics:
Compatibility
volatile_taildefaults to 0, so with no exempt row the payload is byte-identical to before. A test pins that explicitly. No existing caller changes behaviour.Twinned on
AnthropicOAuthAdapter, which is not a subclass ofAnthropicAdapterand has its ownformat_request_payload. Bedrock inherits unchanged.Tests
10 new tests in
tests/models/test_prompt_cache_breakpoint.pycovering placement, the position semantics, the all-exempt case, wire-cleanliness, byte-identity of the no-key path, both legs, and idempotence. Full file: 39 passed. Suite: 4 failures, all pre-existing onmainand none in adapters (3 reproduce at the merge base; the 4th is a live smoke test that skips without a key).