usage

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Jul 6, 2026 License: MIT Imports: 7 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func EventUsagePayload

func EventUsagePayload(u zeroruntime.Usage) map[string]any

EventUsagePayload builds the persisted EventUsage payload for a usage record. It is the single writer paired with usageEventPayload (the reader), so the JSON keys can never drift. Cache and reasoning counts are written only when non-zero, keeping payloads compact and older readers unaffected; BuildReport reads them back to price a turn exactly (cache discount + cache-write premium + reasoning) rather than estimating from prompt/completion alone. Callers add "model" afterward on escalation runs.

func FormatCacheEfficiency added in v0.2.0

func FormatCacheEfficiency(summary Summary) string

FormatCacheEfficiency renders the prompt-cache hit rate for display, e.g. "63% (8,200 cached / 13,100 input)", so a user can see whether cache reads are actually saving work. Returns "n/a" until some input has been recorded.

func FormatSummary

func FormatSummary(summary Summary) string

Types

type DayBucket

type DayBucket struct {
	Date         string  `json:"date"`
	Requests     int     `json:"requests"`
	InputTokens  int     `json:"inputTokens"`
	OutputTokens int     `json:"outputTokens"`
	TotalTokens  int     `json:"totalTokens"`
	TotalCost    float64 `json:"totalCost"`
}

DayBucket aggregates usage events sharing the same UTC calendar date.

type ModelSummary

type ModelSummary struct {
	ModelID            string
	Provider           modelregistry.ProviderKind
	RecordCount        int
	InputTokens        int
	CachedInputTokens  int
	CacheWriteTokens   int
	OutputTokens       int
	ReasoningTokens    int
	TotalTokens        int
	InputCost          float64
	CachedInputCost    float64
	CacheWriteCost     float64
	OutputCost         float64
	TotalCost          float64
	FormattedTotalCost string
}

type Normalized

type Normalized struct {
	InputTokens       int
	CachedInputTokens int
	CacheWriteTokens  int
	OutputTokens      int
	ReasoningTokens   int
	TotalTokens       int
}

func Normalize

func Normalize(usage zeroruntime.Usage) (Normalized, zeroruntime.Usage, error)

type Record

type Record struct {
	ID        string
	Sequence  int
	ModelID   string
	Provider  modelregistry.ProviderKind
	Source    string
	CreatedAt string
	Usage     Normalized
	Cost      modelregistry.CostBreakdown
}

type RecordInput

type RecordInput struct {
	ModelID string
	Usage   zeroruntime.Usage
	Source  string
}

type Report

type Report struct {
	Buckets         []DayBucket `json:"buckets"`
	Total           Totals      `json:"total"`
	NetLOC          int         `json:"netLOC"`
	NetLOCPositive  bool        `json:"netLOCPositive"`
	TokensPerNetLOC float64     `json:"tokensPerNetLOC"`
	CostPerNetLOC   float64     `json:"costPerNetLOC"`
	LOCEstimated    bool        `json:"locEstimated"`
	CostEstimated   bool        `json:"costEstimated"`
}

Report is the aggregated usage view rendered by `zero usage report`. Cost is a reconstructed estimate (see usageEventPayload) and NetLOC is a working-tree estimate; both are surfaced as estimates in the rendered output.

func BuildReport

func BuildReport(events []sessions.Event, meta []sessions.Metadata, registry *modelregistry.Registry, netLOC int) (Report, error)

BuildReport aggregates persisted EventUsage events into per-day buckets and a report-wide total, reconstructing cost from the owning session's Metadata.ModelID via modelregistry.CalculateCost. Sessions whose model id is empty or unknown contribute token counts but no cost. The per-net-LOC ratios are guarded against a non-positive netLOC.

type Summary

type Summary struct {
	RecordCount        int
	Currency           string
	InputTokens        int
	CachedInputTokens  int
	CacheWriteTokens   int
	OutputTokens       int
	ReasoningTokens    int
	TotalTokens        int
	InputCost          float64
	CachedInputCost    float64
	CacheWriteCost     float64
	OutputCost         float64
	TotalCost          float64
	FormattedTotalCost string
	ByModel            []ModelSummary
	LastRecord         *Record
}

func (Summary) CacheHitRate added in v0.2.0

func (summary Summary) CacheHitRate() float64

CacheHitRate is the fraction of input tokens served from the provider's prompt cache. CachedInputTokens is clamped to InputTokens in Normalize, so the result is always in [0,1]; it is 0 when no input has been recorded.

type Totals

type Totals struct {
	Requests     int     `json:"requests"`
	InputTokens  int     `json:"inputTokens"`
	OutputTokens int     `json:"outputTokens"`
	TotalTokens  int     `json:"totalTokens"`
	TotalCost    float64 `json:"totalCost"`
}

Totals carries report-wide sums across every bucket.

type Tracker

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

func NewTracker

func NewTracker(options TrackerOptions) *Tracker

func (*Tracker) Record

func (tracker *Tracker) Record(input RecordInput) (Record, error)

func (*Tracker) Records

func (tracker *Tracker) Records() []Record

func (*Tracker) Reset

func (tracker *Tracker) Reset()

func (*Tracker) Summary

func (tracker *Tracker) Summary() Summary

type TrackerOptions

type TrackerOptions struct {
	Now      func() time.Time
	Registry *modelregistry.Registry
}

Jump to

Keyboard shortcuts

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