Skip to main content

Context Ledger

The Context Ledger tracks token usage across different context segments, enabling precise budget monitoring and optimization decisions. The user tracks token spend; the ledger records per-segment usage for budget decisions.

How It Works

Quick Start

1

Create a ledger

2

View stats in chat

Tracked Segments

API Reference

Track Segments

Get Totals

Reset

ContextLedger Data Class

Multi-Agent Ledger

For multi-agent scenarios, use MultiAgentLedger for per-agent isolation:

CLI Usage

Output:

Integration with Budgeter

Best Practices

Split system, history, tools, and retrieval so you know which segment grows fastest.
Compare ledger.get_total() to the budget before calling the LLM on long sessions.
Avoid carrying stale counts when users start a fresh conversation.
Dump ledger state when investigating overflow or unexpected compaction.

Context Budgeter

Set up token budgets

Context Optimizer

Reduce context when over budget