burn

package
v0.26.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 3, 2026 License: BSD-3-Clause Imports: 12 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

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

View Source
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.

View Source
const (
	Big  = 150_000
	Huge = 250_000
)

Context sizes above which a turn is counted as run on a huge context.

View Source
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.

View Source
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.

View Source
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.

func Num

func Num(n int64) string

Num writes a token count as 1.2k, 3.4M or 5.6G.

func Render

func Render(w io.Writer, r *Report, top int)

Render writes the report as text: the totals by model, the top consumers, the classes of waste with one summary line each.

Types

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.

func (Consumer) Label

func (c Consumer) Label() string

Label names the consumer for the listing.

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 Inject

type Inject struct {
	T     int64  `json:"t"`
	Hook  string `json:"h"`
	Bytes int64  `json:"b"`
}

Inject is one hook output added to the context.

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
}

Report is the attribution of a window of transcripts.

func Build

func Build(sums []*FileSummary, from, to time.Time, st Stats) *Report

Build attributes the turns of sums made at or after from and up to to.

type Stats

type Stats struct {
	Files, Read, Cached, Unreadable int
	Bytes                           int64
	Skipped                         int
}

Stats says what a Load did.

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.

func (Tally) Total

func (t Tally) Total() int64

Total is the raw tokens of every category.

func (Tally) Weighted

func (t Tally) Weighted() float64

Weighted is the tokens in input-equivalents.

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.

func (Turn) Ctx

func (t Turn) Ctx() int64

Ctx is the context the call ran on: everything it sent or read.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL