Documentation
¶
Overview ¶
Package usagereport is a runtime's usage report: what its Claude Code and Codex transcripts say about prompts, sessions, tokens and their API-equivalent cost, per day, project and model. A transcript is reduced once to compact per-file facts, kept by size and modification time in memory and in a file shared with the previous runtime, and the report is a reduction of those facts.
Index ¶
Constants ¶
const DefaultWait = 20 * time.Second
DefaultWait is how long a request waits for a report being built before it is answered with the progress.
const (
// WindowDays is how far back the report looks.
WindowDays = 400
)
Variables ¶
var Volatile = []string{"generatedAt", "collecting", "runtimes.*.report.generatedAt", "runtimes.*.report.collecting"}
Volatile are the fields of an answer that differ between two runtimes reading the same transcripts at different moments: when each built its report, and how far a pass still running has got.
Functions ¶
func DefaultOwner ¶
DefaultOwner is the person a report is for when the session names none: the user at this host.
Types ¶
type Clock ¶
type Clock struct{ Offset float64 }
Clock is the report's day boundary: an offset from UTC in seconds.
type Collector ¶
type Collector struct {
// contains filtered or unexported fields
}
Collector keeps the facts of the transcripts and the latest report. Concurrent callers share one build.
func NewCollector ¶
NewCollector is a collector with nothing read yet.
func (*Collector) Facts ¶
Facts are the facts of every transcript known, in the order they became known.
type Config ¶
type Config struct {
// ClaudeRoot is Claude Code's projects folder, CodexRoot Codex's sessions folder.
ClaudeRoot, CodexRoot string
// CacheFile keeps the facts of every transcript between runs (the previous runtime reads and writes the same file).
CacheFile string
Projects *Projects
Clock Clock
// Timezone is the zone the report says its days are in.
Timezone string
// Machine is the host name the report carries.
Machine string
// Workers read transcripts in parallel on a first pass (GOMAXPROCS when 0).
Workers int
// Now is the clock (tests move it); nil is the real one.
Now func() time.Time
}
Config is where the transcripts and the facts file are, and how the report names its days and projects.
type Day ¶
type Day struct {
Prompts, Sessions, TurnMs, Tools, ToolErrors, Interrupts, Denials, Compactions, PRs float64
// Hours are the prompts per hour of the day.
Hours [24]float64
}
Day is one day's activity, by the report's zone.
type Facts ¶
type Facts struct {
Provider string
SessionID string
// Sidechain: a subagent's transcript, counted toward its parent session and never as prompts.
Sidechain bool
Title, FirstPrompt *string
// Models are strings, except where a transcript named a model with another JSON value (the report then fails, as
// the previous runtime's did).
Models []any
Projects []string
Tasks, PRs []string
FirstTs float64
LastTs float64
Messages []MsgRow
Prompts []PromptRow
Turns []TurnRow
Tools []ToolRow
Interrupts []EventRow
Denials []EventRow
Compactions []EventRow
}
Facts is one transcript reduced.
type MsgRow ¶
type MsgRow struct {
Key any
Ts float64
Model, Project int
Input, Output, CacheRead, W5m, W1h float64
}
MsgRow is a model call: key, time (epoch seconds), model and project (indexes), input, output, cache read and cache writes of the 5-minute and 1-hour tiers.
type Progress ¶
type Progress struct{ Done, Total int }
Progress is how far the first pass over the transcripts has got.
type Projects ¶
type Projects struct {
// Root is the name of the folder that holds the repositories ("" when there is none).
Root string
// TaskPrefixes are the tracker's task key prefixes (upper case): a task key in a prompt or branch is collected, and a
// worktree folder named after a task loses the suffix.
TaskPrefixes []string
// contains filtered or unexported fields
}
Projects names projects from working folders the same way on every machine: the repository git names, or the folder under the workspace root, a worktree without its task suffix.
func (*Projects) ParseClaude ¶
ParseClaude reduces a Claude Code transcript.
type Report ¶
type Report struct {
Person, Machine, GeneratedAt, Timezone string
Days []string
DayStats map[string]*Day
Usage []*UsageRow
Sessions []*Session
Collecting *Progress
}
Report is a runtime's usage report.
func Build ¶
func Build(files []*Facts, person, machine, timezone string, clock Clock, nowMs float64) (*Report, error)
Build reduces the facts of every transcript into the report, as of nowMs.
type Route ¶
type Route struct {
Collector *Collector
// Authorized is the old API's own check (a page session or the cli-token), SessionOK the page session the handler
// asks for besides; Claims are the session's claims.
Authorized, SessionOK func(*http.Request) bool
Claims func(*http.Request) map[string]any
// Owner is the person when the session names none (DefaultOwner when empty).
Owner string
// Machine is this host's name.
Machine string
// Wait replaces DefaultWait (tests).
Wait time.Duration
}
Route answers GET <platform API>/stats: this runtime's usage report, alone (?shape=report) or as a team of one runtime. ?wait=0 does not wait for a report being built.
type Session ¶
type Session struct {
ID, Provider string
Title, Project *string
Start, End, Prompts, TurnMs float64
Input, Output, CacheRead, CacheWrite, Cost, SubagentCost float64
Model *string
Tasks, PRs []string
}
Session is one conversation, its subagents included.