Skip to content

fix(accountant): Stop three fallbacks that lose the saving (#906, #907, #908) - #925

Closed
boazdavid wants to merge 5 commits into
feat/reland-accountantfrom
fix/accountant-fallbacks
Closed

boazdavid wants to merge 5 commits into
feat/reland-accountantfrom
fix/accountant-fallbacks

Conversation

@boazdavid

Copy link
Copy Markdown
Collaborator

Summary

Three accountant fallbacks lost the whole saving of a request. Each now has the operation or bound it was missing.
This PR is a draft stacked on #923. Figures change on purpose.

What changed

Issue Problem Cause Fix Before / after
#906 Default tail breakpoint: every request fell back with check_sum. The tail mark makes the last message longer on the wire. No operation reported it (ops=0 bodies=-63 bytes). apply appends a tailbreakpoint rewrite on the last message when the mark is placed. dash gives it an envelope row. Fallbacks 7 to 0. net_saved_usd 0 to 0.0817. Truth 0.0736.
#907 Removed tool declarations tripped implausible_size and dropped the whole result. The bound compared tool tokens with TokensBefore, which counts messages only. countedSavedTokens skips tool-section operators (toolfilter, toolschema, expand, adjudicate). The bound stays for messages. tf2: fallbacks 8 to 0. net_saved_usd 0 to 0.0554. Truth 0.0564.
#908 Tool defer failed check_sum and conservation on every request. Deferral adds a names list at messages[1] after the pipeline reported its operations. Nobody reported it. proxy diffs the body around applyToolDefer and reports a tooldefer operation (splice or rewrite). Fallbacks 8 to 0. Failed checks to 0. net_saved_usd now -0.0006.

No accountant check or plausibility rule is weakened. Reasons are in each commit message.

Not fixed

How it was tested

  • New Go tests, each failing before its fix: TestTailBreakpointReportsItsBytes, TestTailOpCoversTheMarkBytes, TestRemovedToolDeclarationsDoNotTripThePlausibilityBound, TestPriceMode_ToolSectionTokensAreNotBoundedByMessageTokens, TestToolDeferReportsItsNamesList, TestMessageOpsExplainTheDeferralList.
  • go test -count=1 ./dash/... ./proxy/... ./apply/... ./keepalive/... ./components/... ./tooldefer/... passes. gofmt -l, go vet ./..., go build ./... and go mod tidy -diff are clean.
  • Harness repro against the fake upstream, before and after, as the issues describe.

Assisted-By: Claude (Anthropic AI) [email protected]

The default tail breakpoint (#795) adds a cache_control mark to the last
message after the pipeline ran. A string content also becomes one text
block. The message gets longer on the wire, and no operation reported
the extra bytes. The sum check failed on every request with a trailing
system message (section=messages ops=0 bodies=-63 bytes), so every
request fell back with check_sum and booked no saving.

apply now appends one rewrite operation for the last message, in wire
bytes, when markTail places a mark. Its ChangeID comes from the message
before the mark, so it is the same on every request. The operator
tailbreakpoint is an envelope operator in dash, so it gets its own row.

No accountant check or plausibility rule is weakened. The check now has
the operation it was missing.

Harness (FAKE_TOKDIV=8, tailx, 7 requests): net_saved_usd 0 -> 0.0817.
Truth (control cost - tail on cost): 0.0736. Fallbacks 7 -> 0.

Assisted-By: Claude (Anthropic AI) <[email protected]>
Signed-off-by: DAVID BOAZ <[email protected]>
…y bound (#907)

PriceMode compared the tokens the library saved with e.TokensBefore.
TokensBefore counts message text only. Removed tool declarations are not
in it. A 121-token conversation that drops 2,188 tokens of declarations
looked like a request that removed more than it had, so the event fell
back with implausible_size and lost every other component's saving too.

countedSavedTokens now skips operations of the operators that act on the
tools section (toolfilter, toolschema, expand, adjudicate). The bound is
unchanged for every operation on messages. The accountant checks are not
touched. The bound cannot be made against the full request size here,
because the event has no count of tool and system tokens.

Harness (FAKE_TOKDIV=7, MSGMULT=30, tf2, 8 requests): fallbacks
8 implausible_size -> 0. net_saved_usd 0 -> 0.0554. Truth (control cost -
treatment cost): 0.0564.

Assisted-By: Claude (Anthropic AI) <[email protected]>
Signed-off-by: DAVID BOAZ <[email protected]>
…908)

Tool deferral runs after the pipeline reported its operations. It marks
declarations defer_loading, adds the search tool, and puts a names list
at messages[1]: appended to the system message there, or as a new
system message. The tools half is found by the envelope reporter. The
names list was reported by nobody, so the sum check failed on the
messages section (ops=0 bodies=-308 bytes) and conservation failed
with it, on every request of a deferred thread.

proxy now diffs the body before and after applyToolDefer and reports
the change as a tooldefer operation: a one-message splice for a new
message, a rewrite in place for an appended list. tooldefer is an
envelope operator in dash, so it gets its own row. No accountant check
is weakened.

Harness (FAKE_TOKDIV=4, MSGMULT=1500, td, 8 requests): check_sum
fallbacks 8 -> 0, failed checks 8+7 -> 0. The Overview still does not
credit the deferred declarations: the library sees bytes, and a
declaration marked defer_loading keeps its bytes. net_saved_usd is
-0.0006 (the names list and search tool as cost), truth is +0.0185.
/api/tool-defer still reads 0.0412. Making the two agree needs a
product decision, so it is not part of this change.

Assisted-By: Claude (Anthropic AI) <[email protected]>
Signed-off-by: DAVID BOAZ <[email protected]>
@boazdavid

Copy link
Copy Markdown
Collaborator Author

Closed in favour of #930, the combined PR for the accountant integration. The commits of this PR are in #930. Nothing is lost: the branch stays. Assisted-By: Claude (Anthropic AI) [email protected]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

1 participant