Skip to content

Record compaction token savings in ledger step events (#143) - #144

Merged
tishachawla-jg merged 1 commit into
theagentplane:mainfrom
RjyavardhanSingh:fix/token-usage-before-after
Sep 17, 2026
Merged

tishachawla-jg merged 1 commit into
theagentplane:mainfrom
RjyavardhanSingh:fix/token-usage-before-after

Conversation

@RjyavardhanSingh

Copy link
Copy Markdown
Contributor

Problem

fixes: #143

When context_compaction rewrites a prompt via _compact_messages, the
compaction 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_messages through to the ledger step
event, following the existing data path:

The compaction field is only present when compaction actually
occurred
— 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

@tishachawla-jg
tishachawla-jg merged commit bb34459 into theagentplane:main Sep 17, 2026
3 checks 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.

Record tokens saved by each compaction in the ledger event

2 participants