fix: truncate billing replay at compaction boundary, clamp exact overshoot#22
Merged
Merged
Conversation
…shoot After /compact the request contains only the summary entry and what follows, but the per-call replay still laid out the entire transcript. The pre-compact assistant pieces are usage-derived (exact, never rescaled), so reconcileToUsage zeroed every estimate (static overhead, memory, prompts all showed 0) and the leftover exact mass overflowed the positional cut into the fresh-input tier: lane input summed to millions of phantom tokens while totals.input stayed correct (observed $523 vs $130 on the dashboard). - replay each call from the last compact boundary; the summary entry is in the request and is attributed to prior_assistant/compact_summary - skip the summary in user-prompt counts - safety net: if usage-derived pieces alone still exceed the call's measured prompt (undetected truncation, context editing), scale them down too — per-call exactness outranks per-piece exactness Validated on a real compacted session: lane sums now equal usage token-for-token (input 115,068 / cache_read 317,081,437 / cache_creation 2,665,523).
This was referenced Jun 12, 2026
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.
Details
The per-call billing replay was compaction-blind: it laid out the entire transcript for every LLM call, but after
/compactthe request only contains the summary entry and what follows. The pre-compact assistant pieces are usage-derived (exact, never rescaled), soreconcileToUsagezeroed every estimated piece (static overhead, memory, and user prompts all reported 0) and the leftover exact mass overflowed the positional cut into the fresh-input tier. Result: laneinputsummed to millions of phantom tokens whiletotals.inputstayed correct - the dashboard priced lanes at $523 against a true ~$130 on a real compacted session.Changes:
type:"system", subtype:"compact_boundary"or theisCompactSummaryuser entry). The summary entry IS in the request and is attributed to theprior_assistantlane as acompact_summaryitem - it stands in for the compacted conversation, not something the user typed.TranscriptEntrygainssubtype/isCompactSummaryfields.Usage-derived pieces stay un-rescaled on purpose: if they alone ever exceed a call's measured prompt again, the request dropped content we failed to detect, and the resulting Σ lanes vs usage discrepancy is the signal that finds that bug. The
unattributedlane remains the only reconciliation mechanism.Validated by dry-running the fixed binary on the real compacted session that exposed the bug: lane sums now equal API usage token-for-token (input 115,068 / cache_read 317,081,437 / cache_creation 2,665,523), with
prior_assistantitemized as thinking + text + compact_summary.Testing
TestBillingCompactBoundaryTruncatesReplay: pre-compact content must not leak into the replay; summary lands inprior_assistant; Σ lanes == usagegofmt,go vetclean; binaries rebuilt🤖 Generated with Claude Code