Documentation
¶
Overview ¶
Package usage pushes each officer instance's Claude usage — API calls, tokens and turns per session and UTC hour — to the officer's HQ (docs/officers.md, "Claude usage to HQ").
THE SOURCE is the instance's own Claude Code transcripts, counted by the instance pod's ccd on GET /v1/usage (cmd/ccd/usage_route.go) and read through the agent gateway with the instance's turn bearer. muster-server counts nothing itself: ccd owns the counting rule, next to the files.
THE SINK is the officer's backend (officers.UsageBackend): HQ's PUT /officers/{id}/usage (officers.Client.PutUsage), sent with the officer's own token; for a builtin officer, officer_llm_usage (builtin.Backend.PutUsage, which merges a day with GREATEST for the same whole-window reason). HQ upserts each bucket and REPLACES its counts, so every push is the whole of the window (Window) and a push is idempotent: a later one only ever corrects an earlier one.
WHEN: every Interval, for every declared officer, for each instance the wake loop's own view (officers.Fleet.Instances) says exists and is up — and once more just before the wake loop scales an instance to zero (Flush), so the hour it stopped in is not left partial at HQ until it next runs. Its pod volume is kept on scale-to-zero, so the transcripts survive; but hours older than Window when the instance comes back are never pushed.
🔴 IT NEVER RETRIES WITHIN A TICK AND NEVER BLOCKS THE OFFICER: a failed read or push is counted (muster_officer_usage_push_total{result}) and logged once per change of state, and the next tick's push covers it. Flush is bounded by FlushTimeout and its failure never fails the stop. No transcript text passes through here, and an HQ refusal is logged by its status, never its body.
Index ¶
Constants ¶
const ( // DefaultInterval is how often every up instance's usage is pushed. DefaultInterval = 10 * time.Minute // Window is how far back each push reaches. ccd truncates its start down // to the hour, so the oldest hour pushed is whole. Longer than a day, so // a muster restart or an HQ outage of up to a day loses nothing. Window = 25 * time.Hour // DefaultRequestTimeout bounds one read or one push. DefaultRequestTimeout = 30 * time.Second // FlushTimeout bounds the push before a scale-to-zero, read and push // together. FlushTimeout = 10 * time.Second )
Variables ¶
var ErrUnsupported = errors.New("usage: the instance's ccd does not serve GET /v1/usage (its image predates it)")
ErrUnsupported is an instance whose ccd does not serve GET /v1/usage (an image that predates it). The instance is skipped quietly.
Functions ¶
This section is empty.
Types ¶
type Collector ¶
type Collector struct {
// contains filtered or unexported fields
}
Collector is the usage collector.
func (*Collector) Flush ¶
Flush pushes instance's usage once, now: the wake loop is about to scale it to zero (officers.Fleet.Stop). Bounded by FlushTimeout, read and push together; best-effort — whatever happens is counted and logged, and nothing is returned for the stop to fail on.
type Config ¶
type Config struct {
// Officers are the declared officers; each one's pools are walked.
Officers []officers.Officer
Instances Instances
Source Source
Poster Poster
Logger *log.Logger
Now func() time.Time
// Interval is the cadence (DefaultInterval).
Interval time.Duration
// RequestTimeout bounds one read or push (DefaultRequestTimeout).
RequestTimeout time.Duration
}
Config is New's input. Zero values take the defaults.
type Instances ¶
Instances is the wake loop's view of pool o's instances (officers.Fleet.Instances): THE rule for which instance exists and is up.
type Poster ¶
type Poster interface {
PutUsage(ctx context.Context, o officers.Officer, r officers.UsageReport) error
}
Poster is the HQ side.
func NewBackendPoster ¶ added in v0.209.0
NewBackendPoster is a Poster over each officer's own backend. A backend that does not serve usage answers officers.ErrNotServed.
type Reading ¶
type Reading struct {
// Account is the pooled Claude account the instance is on NOW, "" for a
// pool on the LLM proxy (officers.UsageReport.Account).
Account string
Buckets []cctranscript.UsageBucket
}
Reading is one instance's usage as its Source read it.
type Source ¶
type Source interface {
Read(ctx context.Context, o officers.Officer, instance string, since time.Time) (Reading, error)
}
Source reads an officer instance's usage since since (production: its ccd's GET /v1/usage, through the agent gateway). An image without the route is ErrUnsupported.