usage

package
v0.8.4 Latest Latest
Warning

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

Go to latest
Published: Mar 12, 2026 License: Apache-2.0 Imports: 9 Imported by: 0

Documentation

Overview

Package usage provides persistent token usage and cost tracking for LLM interactions. Records are append-only and indexed by timestamp, session, and conversation for efficient aggregation queries.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ComputeCost

func ComputeCost(model string, inputTokens, outputTokens int, pricing map[string]config.PricingEntry) float64

ComputeCost calculates the USD cost for a model's token usage based on the pricing table. Models not in the table are treated as free (local/Ollama models).

func ResolveProvider

func ResolveProvider(model string) string

ResolveProvider infers the LLM provider from the model name. Models starting with "claude-" are Anthropic; everything else is assumed to be Ollama (local).

Types

type GroupedSummary

type GroupedSummary struct {
	Key     string
	Summary Summary
}

GroupedSummary pairs a grouping key (model name, role, task name) with its aggregated usage totals. Slices of GroupedSummary preserve the SQL ordering (highest cost first).

type Record

type Record struct {
	ID             string
	Timestamp      time.Time
	RequestID      string
	SessionID      string
	ConversationID string
	Model          string
	Provider       string // "anthropic", "ollama"
	InputTokens    int
	OutputTokens   int
	CostUSD        float64
	Role           string // "interactive", "delegate", "scheduled", "auxiliary"
	TaskName       string // "email_poll", "periodic_reflection", etc. (empty for interactive)
}

Record represents a single LLM interaction's token usage and cost.

type Store

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

Store is an append-only SQLite store for token usage records. All public methods are safe for concurrent use (SQLite serializes writes).

func NewStore

func NewStore(dbPath string) (*Store, error)

NewStore creates a usage store at the given database path. The schema is created automatically on first use.

func (*Store) Close

func (s *Store) Close() error

Close closes the database connection.

func (*Store) Record

func (s *Store) Record(ctx context.Context, rec Record) error

Record persists a usage record. If rec.ID is empty, a UUIDv7 is generated. The context is used for cancellation only.

func (*Store) Summary

func (s *Store) Summary(start, end time.Time) (*Summary, error)

Summary returns aggregated totals for records within [start, end).

func (*Store) SummaryByModel

func (s *Store) SummaryByModel(start, end time.Time) ([]GroupedSummary, error)

SummaryByModel returns per-model aggregated totals for records within [start, end), ordered by cost descending.

func (*Store) SummaryByRole

func (s *Store) SummaryByRole(start, end time.Time) ([]GroupedSummary, error)

SummaryByRole returns per-role aggregated totals for records within [start, end), ordered by cost descending.

func (*Store) SummaryByTask

func (s *Store) SummaryByTask(start, end time.Time) ([]GroupedSummary, error)

SummaryByTask returns per-task aggregated totals for records within [start, end), ordered by cost descending. Records with empty task_name are grouped under the key "".

type Summary

type Summary struct {
	TotalRecords      int
	TotalInputTokens  int64
	TotalOutputTokens int64
	TotalCostUSD      float64
}

Summary holds aggregated token usage and cost totals.

Jump to

Keyboard shortcuts

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