Documentation
¶
Overview ¶
Package burn attributes the tokens a Claude Code installation spends: per session and per sub-agent, by category and model, and the share caused by the harness itself (gate refusals that force a retry, notifications that wake a big context for nothing, the fixed cost of starting a sub-agent, text injected by hooks, turns run on a huge context).
The transcript format is undocumented; the fields relied on are the ones the code below decodes (see the line type): assistant entries' message.id, message.model, message.usage.{input_tokens, cache_creation_input_tokens, cache_creation.ephemeral_5m/1h_input_tokens, cache_read_input_tokens, output_tokens}, timestamp, isApiErrorMessage; user entries' tool_result items (is_error + text) and <task-notification> texts; attachment entries of type hook_additional_context and hook_blocking_error. Sub-agent transcripts live in <session>/subagents/agent-<id>.jsonl with an agent-<id>.meta.json beside them.
Index ¶
Constants ¶
const ( GapShort = 5 * 60 GapLong = 60 * 60 )
Gap buckets, in seconds between a turn and the previous turn of the same transcript: the 5 minute cache lifetime and the 1 hour one.
const ( MissMin = 5_000 )
MissShare is the part of a turn's prompt that must be cache writes for the turn to count as a cache miss, and MissMin the smallest write that counts: a first turn of a conversation writes all of a small prompt and says nothing about a TTL.
const ( WIn = 1.0 WCW5 = 1.25 WCW1 = 2.0 WCR = 0.1 WOut = 5.0 )
Weights turn the four token categories into input-equivalent tokens, in the ratios the API prices them at (cache write 5 minutes 1.25x, 1 hour 2x, cache read 0.1x, output 5x of an input token). The percentages of a report are shares of this weighted total: raw token counts would call a cache read as heavy as an output token.
const ( Big = 150_000 Huge = 250_000 )
Context sizes above which a turn is counted as run on a huge context.
const ( ClassRetry = "retry after a gate refusal" ClassIdle = "idle wake (notification, no tool call)" ClassFixed = "agent fixed cost (first turn)" ClassInject = "hook injection (estimate)" ClassBig = "turns on context > 150k" ClassHuge = "turns on context > 250k" )
Class names, in report order.
const BytesPerToken = 4
BytesPerToken is the ratio used to turn injected bytes into an estimated token count; the transcripts record bytes, not tokens, for hook output.
const PermissionDeny = "permission-deny"
PermissionDeny is the gate name of a refusal by a permission rule, not a hook.
Variables ¶
This section is empty.
Functions ¶
func Load ¶
func Load(root, cacheFile string, cutoff time.Time) ([]*FileSummary, Stats)
Load summarizes every transcript under root written at or after cutoff. A file whose size and modification time are those the cache holds is not read again; cacheFile "" keeps no cache. A cache that cannot be read is rebuilt. A transcript that cannot be opened is counted in Stats.Unreadable, never taken as empty.
Types ¶
type CacheMiss ¶ added in v0.27.0
type CacheMiss struct {
Buckets []MissBucket `json:"buckets"`
// CW1 is every 1 hour write of the window and CW1Weighted its cost; Premium is
// the part above what the same tokens cost as 5 minute writes.
CW1 int64 `json:"cache_write_1h"`
CW1Weighted float64 `json:"cache_write_1h_weighted"`
Premium float64 `json:"premium_over_5m"`
// MidTurns and MidRead are the turns after a gap of 5 to 60 minutes, in a
// transcript that writes 1 hour entries, and the cache tokens they read. Under
// the 5 minute lifetime those reads would have been writes: Rewrite is their cost
// so, Actual the cost as read, Saved the difference.
MidTurns int64 `json:"turns_gap_5_60min"`
MidRead int64 `json:"cache_read_gap_5_60min"`
Rewrite float64 `json:"rewrite_cost_5m_ttl"`
Actual float64 `json:"actual_read_cost"`
Saved float64 `json:"saved"`
// Net is Saved minus Premium: positive when the 1 hour lifetime paid.
Net float64 `json:"net"`
}
CacheMiss says whether the 1 hour cache lifetime pays for itself. A miss is a turn whose cache writes are at least MissShare of its prompt (and MissMin tokens) after a gap; the buckets split them by gap and by lifetime written. The verdict compares what 1 hour writes cost over 5 minute ones with what the gaps of 5 to 60 minutes would have cost as full rewrites under the 5 minute lifetime.
type Class ¶
type Class struct {
Name string `json:"name"`
Turns int64 `json:"turns"`
Tokens int64 `json:"tokens"`
Weighted float64 `json:"weighted"`
Pct float64 `json:"pct_of_total"`
Top string `json:"top_offender"`
Parts []Part `json:"breakdown,omitempty"`
}
Class is one cause of waste: the turns it made, the tokens they cost, and who is the largest part. Classes overlap (a retry may also run on a huge context).
type Consumer ¶
type Consumer struct {
Kind string `json:"kind"` // "session" or "agent"
Session string `json:"session"`
Project string `json:"project"`
AgentID string `json:"agent_id,omitempty"`
Type string `json:"agent_type,omitempty"`
Desc string `json:"description,omitempty"`
Tally
Models []string `json:"models"`
// Over150 and Over250 are the turns run on a context above Big and Huge.
Over150 int64 `json:"turns_over_150k"`
Over250 int64 `json:"turns_over_250k"`
}
Consumer is one transcript's tokens: a session's own, or one sub-agent's.
type FileSummary ¶
type FileSummary struct {
Session string `json:"s"`
Project string `json:"p,omitempty"`
AgentID string `json:"a,omitempty"` // "" for a session's own transcript
Kind string `json:"k,omitempty"` // a sub-agent's type
Desc string `json:"d,omitempty"`
Turns []Turn `json:"u,omitempty"`
Injects []Inject `json:"j,omitempty"`
// Wakes are the times of the task notifications read.
Wakes []int64 `json:"n,omitempty"`
// Skipped counts lines over maxLine.
Skipped int `json:"x,omitempty"`
}
FileSummary is what one transcript contributes; it is what the cache keeps.
func Summarize ¶
func Summarize(path string) (*FileSummary, error)
Summarize reads one transcript. The session, project and sub-agent fields come from where the file is.
type MissBucket ¶ added in v0.27.0
type MissBucket struct {
Gap string `json:"gap"`
Turns int64 `json:"turns"`
CW5 int64 `json:"cache_write_5m"`
CW1 int64 `json:"cache_write_1h"`
Weighted float64 `json:"weighted"`
}
MissBucket is the cache misses whose gap since the previous turn fell in one range.
type Part ¶
type Part struct {
Name string `json:"name"`
Turns int64 `json:"turns"`
Tokens int64 `json:"tokens"`
Weighted float64 `json:"weighted"`
}
Part is one line of a class's breakdown.
type Report ¶
type Report struct {
From, To time.Time
Total Tally
ByModel map[string]Tally
Consumers []Consumer // heaviest first
Classes []Class
Agents int // sub-agents started in the window
Wakes int
Stats Stats
Miss CacheMiss
}
Report is the attribution of a window of transcripts.
type Tally ¶
type Tally struct {
Turns int64 `json:"turns"`
In int64 `json:"input"`
CW5 int64 `json:"cache_write_5m"`
CW1 int64 `json:"cache_write_1h"`
CR int64 `json:"cache_read"`
Out int64 `json:"output"`
}
Tally is tokens by category.
type Turn ¶
type Turn struct {
T int64 `json:"t"`
Model string `json:"m"`
In int64 `json:"i"`
CW5 int64 `json:"w5,omitempty"`
CW1 int64 `json:"w1,omitempty"`
CR int64 `json:"r,omitempty"`
Out int64 `json:"o"`
// Tool is whether the message called a tool.
Tool bool `json:"tl,omitempty"`
// Cause is the gate whose refusal this turn followed, "" for none.
Cause string `json:"c,omitempty"`
// Idle marks a turn run for a task notification that led to no tool call.
Idle bool `json:"id,omitempty"`
// Carried is the estimated tokens of hook-injected text sitting in the context
// this turn read from the cache.
Carried int64 `json:"k,omitempty"`
}
Turn is one assistant message (one API call), counted once however many transcript lines it was streamed over.