Skip to content

fix: truncate billing replay at compaction boundary, clamp exact overshoot#22

Merged
jverre merged 2 commits into
mainfrom
jacques/OPIK-6873-compaction-aware-billing
Jun 12, 2026
Merged

fix: truncate billing replay at compaction boundary, clamp exact overshoot#22
jverre merged 2 commits into
mainfrom
jacques/OPIK-6873-compaction-aware-billing

Conversation

@jverre

@jverre jverre commented Jun 12, 2026

Copy link
Copy Markdown
Collaborator

Details

The per-call billing replay was compaction-blind: it laid out the entire transcript for every LLM call, but after /compact the request only contains the summary entry and what follows. The pre-compact assistant pieces are usage-derived (exact, never rescaled), so reconcileToUsage zeroed 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: lane input summed to millions of phantom tokens while totals.input stayed correct - the dashboard priced lanes at $523 against a true ~$130 on a real compacted session.

Changes:

  • Each call's replay starts at the last compact boundary (type:"system", subtype:"compact_boundary" or the isCompactSummary user entry). The summary entry IS in the request and is attributed to the prior_assistant lane as a compact_summary item - it stands in for the compacted conversation, not something the user typed.
  • The summary is skipped in user-prompt event counts.
  • TranscriptEntry gains subtype / isCompactSummary fields.

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 unattributed lane 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_assistant itemized as thinking + text + compact_summary.

Testing

  • TestBillingCompactBoundaryTruncatesReplay: pre-compact content must not leak into the replay; summary lands in prior_assistant; Σ lanes == usage
  • Full suite, gofmt, go vet clean; binaries rebuilt

🤖 Generated with Claude Code

jverre added 2 commits June 12, 2026 09:07
…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).
@jverre jverre merged commit 1d2c1c0 into main Jun 12, 2026
1 check passed
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