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
- Variables
- func ActiveKey(path string) string
- func Build(in Input, sc *Scanner) *model.Usage
- type Counts
- type Engine
- func (e *Engine) Guardrails() *model.Guardrails
- func (e *Engine) Rename(key, name string) error
- func (e *Engine) Run(ctx context.Context)
- func (e *Engine) SetColor(key, color string) error
- func (e *Engine) SetRecur(s *guard.Source)
- func (e *Engine) ShowOrg(claudeDir string)
- func (e *Engine) Step(ctx context.Context, now time.Time)
- func (e *Engine) View() *model.Usage
- type Input
- type Scanner
- func (s *Scanner) Accounts() (keys []string, unattributed bool)
- func (s *Scanner) Attribute(owner string, at time.Time) string
- func (s *Scanner) Buckets(start, step int64, n int, w Which, acct string) (out, cacheRead [][]float64)
- func (s *Scanner) Refresh(now time.Time) Stats
- func (s *Scanner) Refusals() []guard.Count
- func (s *Scanner) Scanned() bool
- func (s *Scanner) SetStamps(tl accounts.Timeline)
- type Source
- type Stats
- type Which
Constants ¶
const BucketSec = 600
BucketSec is the resolution of the kept sums.
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.
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.
const ScanEvery = time.Minute
ScanEvery is how often transcripts are scanned for new tokens.
Variables ¶
var Families = model.Families
Families are the stack order of the charts.
Functions ¶
Types ¶
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 ¶
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
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) SetColor ¶ added in v0.12.0
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) SetRecur ¶ added in v0.10.0
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
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.
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
// 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 ¶
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
Accounts is the keys of the accounts that have tokens, and whether any tokens are unattributed.
func (*Scanner) Attribute ¶ added in v0.9.0
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 ¶
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
Refusals is the gate refusals kept, per hour and gate, oldest first.