llm: anchor the cache breakpoint on every request of a turn #143

Open
JakeHillion wants to merge 1 commits from jj/pzuonmvspsol into main
pull from: jj/pzuonmvspsol
Owner

The Responses API breakpoint was positioned by inferring it from the wire
shape: the item before the last, and only when the request ended in a
message. Every request after the first in a tool loop ends in a
function_call_output instead, so those carried no breakpoint of ours at
all, and the ~70k-token prefix was re-read — and its reuse window
refreshed — only by each turn's opening request. At a tick interval near
the edge of that window, that single refresh per turn is what lets a run
of misses sustain itself.

Thread the boundary through instead of guessing it. The tool loop knows
the caller's last message opens the turn and only ever appends after it,
so chat_completion_with_tools_impl captures that count once and passes it
down to build_responses_input, which translates it into an item index as
it builds (messages are not one-to-one with items — system messages fold
into instructions, an assistant message with tool calls expands into
several). anchor_cache_breakpoint marks the last committed item at or
before it, with the end-of-request guard removed.

The anchor now lands on the same item for every request a turn makes, so
each pass of a tool loop re-reads the committed prefix and refreshes it
for free, rather than leaving it untouched between turns. Anchoring
behind the turn rather than at the tip is what makes that possible: a
tick that stays silent discards its own prompt and tool traffic, so the
last committed message is the furthest point guaranteed to repeat.

The API's implicit breakpoint is deliberately left in place. It only ever
writes what is new beyond the longest cached prefix, and our anchor is
read on the same request, so its write is the turn's own tail — cheap
when the turn is discarded, and enough for each pass of the loop to read
the pass before it.

Test plan:

  • CI
  • cache_breakpoint_holds_still_across_a_tool_loop replaces
    cache_breakpoint_skips_mid_tool_loop_requests, asserting the same item
    is marked before and after tool traffic is appended
The Responses API breakpoint was positioned by inferring it from the wire shape: the item before the last, and only when the request ended in a message. Every request after the first in a tool loop ends in a function_call_output instead, so those carried no breakpoint of ours at all, and the ~70k-token prefix was re-read — and its reuse window refreshed — only by each turn's opening request. At a tick interval near the edge of that window, that single refresh per turn is what lets a run of misses sustain itself. Thread the boundary through instead of guessing it. The tool loop knows the caller's last message opens the turn and only ever appends after it, so chat_completion_with_tools_impl captures that count once and passes it down to build_responses_input, which translates it into an item index as it builds (messages are not one-to-one with items — system messages fold into instructions, an assistant message with tool calls expands into several). anchor_cache_breakpoint marks the last committed item at or before it, with the end-of-request guard removed. The anchor now lands on the same item for every request a turn makes, so each pass of a tool loop re-reads the committed prefix and refreshes it for free, rather than leaving it untouched between turns. Anchoring behind the turn rather than at the tip is what makes that possible: a tick that stays silent discards its own prompt and tool traffic, so the last committed message is the furthest point guaranteed to repeat. The API's implicit breakpoint is deliberately left in place. It only ever writes what is new beyond the longest cached prefix, and our anchor is read on the same request, so its write is the turn's own tail — cheap when the turn is discarded, and enough for each pass of the loop to read the pass before it. Test plan: - CI - cache_breakpoint_holds_still_across_a_tool_loop replaces cache_breakpoint_skips_mid_tool_loop_requests, asserting the same item is marked before and after tool traffic is appended
JakeHillion added 1 commit 2026-08-18 23:43:24 +01:00
The Responses API breakpoint was positioned by inferring it from the wire
shape: the item before the last, and only when the request ended in a
message. Every request after the first in a tool loop ends in a
function_call_output instead, so those carried no breakpoint of ours at
all, and the ~70k-token prefix was re-read — and its reuse window
refreshed — only by each turn's opening request. At a tick interval near
the edge of that window, that single refresh per turn is what lets a run
of misses sustain itself.

Thread the boundary through instead of guessing it. The tool loop knows
the caller's last message opens the turn and only ever appends after it,
so chat_completion_with_tools_impl captures that count once and passes it
down to build_responses_input, which translates it into an item index as
it builds (messages are not one-to-one with items — system messages fold
into instructions, an assistant message with tool calls expands into
several). anchor_cache_breakpoint marks the last committed item at or
before it, with the end-of-request guard removed.

The anchor now lands on the same item for every request a turn makes, so
each pass of a tool loop re-reads the committed prefix and refreshes it
for free, rather than leaving it untouched between turns. Anchoring
behind the turn rather than at the tip is what makes that possible: a
tick that stays silent discards its own prompt and tool traffic, so the
last committed message is the furthest point guaranteed to repeat.

The API's implicit breakpoint is deliberately left in place. It only ever
writes what is new beyond the longest cached prefix, and our anchor is
read on the same request, so its write is the turn's own tail — cheap
when the turn is discarded, and enough for each pass of the loop to read
the pass before it.

Test plan:
- CI
- cache_breakpoint_holds_still_across_a_tool_loop replaces
  cache_breakpoint_skips_mid_tool_loop_requests, asserting the same item
  is marked before and after tool traffic is appended
All checks were successful
CI / nix (push) Successful in 6m11s
Required
Details
This pull request has changes conflicting with the target branch.
  • src/llm/mod.rs
  • src/llm/responses.rs
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin jj/pzuonmvspsol:jj/pzuonmvspsol
git checkout jj/pzuonmvspsol
Sign in to join this conversation.
No Reviewers
No labels
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: JakeHillion/personal-agent#143