usage

package
v0.13.1 Latest Latest
Warning

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

Go to latest
Published: Nov 26, 2026 License: BSD-3-Clause Imports: 17 Imported by: 1 module, 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 usage turns the transcripts' token counts into the Quota tab's data.

A transcript is read once: the scanner remembers, per file, how far it read, and keeps only per-bucket sums (10 minutes, per model family), cached under the user cache directory, so a restart reads what was appended and not gigabytes. Only numbers are decoded: the struct has no field for message content.

Index

Constants

View Source
const BucketSec = 600

BucketSec is the resolution of the kept sums.

View Source
const Keep = 35 * 24 * time.Hour

Keep is how far back sums are kept: four 7 day windows and a week of margin for the alignment on the reset.

View Source
const KeepRefusals = 13 * 7 * 24 * time.Hour

KeepRefusals is how far back gate refusals are kept: the Guardrails tab's 12 weeks and a week of margin for the alignment on Monday.

View Source
const ScanEvery = time.Minute

ScanEvery is how often transcripts are scanned for new tokens.

Variables

View Source
var Families = model.Families

Families are the stack order of the charts.

Functions

func ActiveKey added in v0.8.0

func ActiveKey(path string) string

ActiveKey is the key of the account Claude Code is signed in to, from the oauthAccount of its .claude.json: "" when that cannot be read. Only the account id is decoded, and only its hash is kept.

func Build

func Build(in Input, sc *Scanner) *model.Usage

Build assembles the Quota tab's data: one view per account, the sum, and the capacity left.

Types

type Counts

type Counts struct {
	Out       int64 `json:"o,omitempty"`
	CacheRead int64 `json:"c,omitempty"`
}

Counts is the tokens of one family in one bucket.

type Engine

type Engine struct {
	// contains filtered or unexported fields
}

Engine keeps the Quota tab's data current: it scans transcripts every ScanEvery, reads aiquota at most every quota.MinInterval, and records each reading with the account it belongs to.

func NewEngine

func NewEngine(claudeDir, cacheDir, configDir string, run quota.Runner) *Engine

NewEngine reads transcripts under claudeDir/projects, caches sums in cacheDir and records readings in configDir. The signed-in account is read from the .claude.json next to claudeDir.

func (*Engine) Guardrails added in v0.10.0

func (e *Engine) Guardrails() *model.Guardrails

Guardrails is the Guardrails tab's latest data, nil before the first pass ended.

func (*Engine) Rename added in v0.9.0

func (e *Engine) Rename(key, name string) error

Rename gives the account with the key a display name, or removes it when name is empty, and rebuilds the view so the next snapshot carries it. It is what the UI's Rename action calls; the file is written here, by the serve process.

func (*Engine) Run

func (e *Engine) Run(ctx context.Context)

Run steps now and then every ScanEvery until ctx ends.

func (*Engine) SetColor added in v0.12.0

func (e *Engine) SetColor(key, color string) error

SetColor gives the account with the key a colour (#rrggbb), or restores its automatic colour when color is empty, and rebuilds the view so the next snapshot carries it. It is what the UI's Colour action calls; the file is written here, by the serve process, and a value that is not #rrggbb is refused.

func (*Engine) SetPrefs added in v0.13.0

func (e *Engine) SetPrefs(theme, lang string) error

SetPrefs stores the viewer's theme ("light", "dark", "system" or "") and language (a tag or "" for automatic) and rebuilds the view so the next snapshot carries them. It is what the UI's theme and language selectors call; the file is written here, by the serve process, and a value that is neither is refused.

func (*Engine) SetRecur added in v0.10.0

func (e *Engine) SetRecur(s *guard.Source)

SetRecur gives the engine the source of `recur --json` that the Guardrails tab reads (at most every guard.MinInterval).

func (*Engine) ShowOrg added in v0.9.0

func (e *Engine) ShowOrg(claudeDir string)

ShowOrg makes the engine offer the organisation's name (from the .claude.json next to claudeDir) as a suggested display name of the active account: it is off by default, and while it is off no organisation name is read at all.

func (*Engine) Step

func (e *Engine) Step(ctx context.Context, now time.Time)

Step does one pass at now.

func (*Engine) View

func (e *Engine) View() *model.Usage

View is the latest data, nil before the first pass ended.

type Input added in v0.8.0

type Input struct {
	Now time.Time
	// Live is the last successful aiquota reading, one per account, tagged; empty
	// when aiquota has not been read (or could not be).
	Live []quota.Reading
	// Hist is every recorded reading (live ones included), oldest first.
	Hist []quota.Reading
	// QuotaErr is why aiquota could not be read.
	QuotaErr string
	// Active is the key of the account Claude Code is signed in to, "" unknown.
	Active string
	// Alias is the display name of an account by key ("" for none); nil is none.
	Alias func(key string) string
	// Color is the colour the owner gave an account by key ("" for none); nil is
	// none.
	Color func(key string) string
	// Prefs is the viewer's theme and language; the zero value is none chosen.
	Prefs model.Prefs
	// Stamps is the `stamp` hook's timeline: an account known only by its tokens
	// takes its label and plan from its latest stamp.
	Stamps accounts.Timeline
	// Suggest is the organisation's name of the active account, "" unless the
	// server runs with --show-org.
	Suggest string
}

Input is everything Build needs besides the transcripts.

type Scanner

type Scanner struct {
	// contains filtered or unexported fields
}

Scanner holds the per-file offsets and the per-bucket sums.

func NewScanner

func NewScanner(root, cacheFile string) *Scanner

NewScanner reads transcripts under root and keeps its cache in cacheFile ("" keeps none). A cache that cannot be read is ignored: it is rebuilt.

func (*Scanner) Accounts added in v0.8.0

func (s *Scanner) Accounts() (keys []string, unattributed bool)

Accounts is the keys of the accounts that have tokens, and whether any tokens are unattributed.

func (*Scanner) Attribute added in v0.9.0

func (s *Scanner) Attribute(owner string, at time.Time) string

Attribute is the account of a call made at `at` in a transcript whose latest session-owner entry named owner: that one when there is one, else the latest stamp at or before the call, else "" (unattributed).

func (*Scanner) Buckets

func (s *Scanner) Buckets(start, step int64, n int, w Which, acct string) (out, cacheRead [][]float64)

Buckets returns n buckets of step seconds from start: out[layer][i] and cacheRead[layer][i], one layer per family, and for Every one more for the unattributed tokens. step is a multiple of BucketSec.

func (*Scanner) Refresh

func (s *Scanner) Refresh(now time.Time) Stats

Refresh scans what changed since the last pass and drops sums older than Keep. It returns what it did.

func (*Scanner) Refusals added in v0.10.0

func (s *Scanner) Refusals() []guard.Count

Refusals is the gate refusals kept, per hour and gate, oldest first.

func (*Scanner) Scanned added in v0.10.0

func (s *Scanner) Scanned() bool

Scanned is whether a pass over the transcripts has ended: before it, the refusals are not measured, which is not the same as none.

func (*Scanner) SetStamps added in v0.9.0

func (s *Scanner) SetStamps(tl accounts.Timeline)

SetStamps gives the scanner the timeline of the `stamp` hook, used for the calls of a transcript that has no session-owner entry.

type Source

type Source struct {
	Inner interface{ Snapshot() model.Snapshot }
	Eng   *Engine
}

Source adds the engine's view to the snapshots of an inner source.

func (Source) Snapshot

func (s Source) Snapshot() model.Snapshot

Snapshot is the inner snapshot with the Usage view attached.

type Stats

type Stats struct {
	Files, Scanned int
	Bytes          int64
	Lines, Counted int
	Skipped        int // lines over maxLine
	Bad            int // usage lines that did not decode
}

Stats says what a Refresh did, for tests and for the cold-scan measurement.

type Which added in v0.8.0

type Which int

Which says whose tokens a series counts.

const (
	// Only counts one account (a key).
	Only Which = iota
	// Every counts every account and the unattributed tokens, the latter as an
	// extra last layer.
	Every
)

Jump to

Keyboard shortcuts

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