Record compaction token savings in ledger step events (#143) - #144
Merged
tishachawla-jg merged 1 commit intoSep 17, 2026
Merged
tishachawla-jg merged 1 commit into
tishachawla-jg merged 1 commit into
Conversation
tishachawla-jg
approved these changes
Sep 17, 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.
Problem
fixes: #143
When
context_compactionrewrites a prompt via_compact_messages, thecompaction happens silently. There is no record of how many tokens it
removed, so you cannot tell whether compaction helped or how much context
was saved.
Solution
Plumb compaction metadata (
tokens_before,tokens_after,tokens_saved) from_compact_messagesthrough to the ledger stepevent, following the existing data path:
The
compactionfield is only present when compaction actuallyoccurred — no change to the common (non-compacted) path.
Step event with compaction
{ "kind": "step", "node_type": "llm", "usage": {"input": 80, "output": 20}, "compaction": {"tokens_before": 195, "tokens_after": 119, "tokens_saved": 76} }COMPACTION RESULTS
Input messages sent: 8
Messages after compact: 5 (3 duplicate assistant msgs dropped)
Tokens BEFORE compaction: 195
Tokens AFTER compaction: 119
Tokens SAVED: 76
Saving percentage: 39.0%
LLM usage reported: input=120, output=15
Cost (micro-USD): 1650