Documentation
¶
Index ¶
- Constants
- func CountProviders(entries []UsageEntry) int
- func DefaultDateRange() (since, until time.Time)
- func GetTrackedExternallyBy(providerName string) string
- func ParseDateYYYYMMDD(s string) (time.Time, error)
- func RefreshDirectoryInBackground(ctx context.Context)
- type DailyUsage
- type FilterOptions
- type LoadResult
- type LogEntry
- type Logger
- type ModelBreakdown
- type ModelPricing
- type PricingFetcher
- func (p *PricingFetcher) CalculateCost(entry UsageEntry) (float64, error)
- func (p *PricingFetcher) CalculateCostLocal(entry UsageEntry) (float64, error)
- func (p *PricingFetcher) GetPricing(modelName string) (ModelPricing, error)
- func (p *PricingFetcher) GetPricingLocal(modelName string) (ModelPricing, error)
- func (p *PricingFetcher) RefreshModelsDevPricing(ctx context.Context) error
- type ProviderBreakdown
- type UsageEntry
Constants ¶
const ( ProviderClaudeCode = "claude-code" ProviderTermLLM = "term-llm" )
Provider constants for identifying data sources
Variables ¶
This section is empty.
Functions ¶
func CountProviders ¶
func CountProviders(entries []UsageEntry) int
CountProviders returns the number of unique providers in the entries
func DefaultDateRange ¶
DefaultDateRange returns the default date range (last 7 days)
func GetTrackedExternallyBy ¶
GetTrackedExternallyBy returns the external tracking provider for a given term-llm provider name
func ParseDateYYYYMMDD ¶
ParseDateYYYYMMDD parses a date in YYYYMMDD format
func RefreshDirectoryInBackground ¶ added in v0.9.49
RefreshDirectoryInBackground refreshes the cached model directory without blocking the caller. Prices are only ever read from the cache, so a program that starts, runs and exits before the fetch lands simply uses the previous catalog or the bundled table.
Types ¶
type DailyUsage ¶
type DailyUsage struct {
Date string // YYYY-MM-DD format
InputTokens int
OutputTokens int
CacheWriteTokens int
CacheReadTokens int
ReasoningTokens int
TotalCost float64
ModelsUsed []string
Entries []UsageEntry // Raw entries for this day (used for breakdown)
}
DailyUsage represents aggregated usage for a single day
func AggregateDaily ¶
func AggregateDaily(entries []UsageEntry) []DailyUsage
AggregateDaily aggregates usage entries by day
func CalculateTotals ¶
func CalculateTotals(daily []DailyUsage) DailyUsage
CalculateTotals calculates total usage across all daily entries
func (DailyUsage) TotalTokens ¶
func (d DailyUsage) TotalTokens() int
TotalTokens returns the sum of all token types for the day
type FilterOptions ¶
type FilterOptions struct {
Since time.Time // Include entries on or after this time
Until time.Time // Include entries on or before this time
Provider string // Filter to specific provider, or empty for all
IncludeExternal bool // Include term-llm entries with TrackedExternallyBy set (opt-in)
}
FilterOptions contains options for filtering usage data
type LoadResult ¶
type LoadResult struct {
Entries []UsageEntry
MissingDirectories []string
Errors []error
}
LoadResult contains the result of loading usage data from a provider
func LoadClaudeUsage ¶
func LoadClaudeUsage() LoadResult
LoadClaudeUsage loads usage data from Claude Code JSONL files
func LoadTermLLMUsage ¶
func LoadTermLLMUsage() LoadResult
LoadTermLLMUsage loads usage data from term-llm's own log files
func LoadTermLLMUsageForDateRange ¶
func LoadTermLLMUsageForDateRange(since, until time.Time) LoadResult
LoadTermLLMUsageForDateRange loads term-llm usage data for a specific date range This is more efficient than loading all data and filtering, as it only reads files within the date range.
func (LoadResult) Filter ¶
func (r LoadResult) Filter(opts FilterOptions) []UsageEntry
Filter returns entries matching the filter options
func (*LoadResult) Merge ¶
func (r *LoadResult) Merge(other LoadResult)
Merge combines another LoadResult into this one
type LogEntry ¶
type LogEntry struct {
Timestamp time.Time `json:"timestamp"`
SessionID string `json:"session_id,omitempty"`
Model string `json:"model"`
Provider string `json:"provider"`
InputTokens int `json:"input_tokens"`
OutputTokens int `json:"output_tokens"`
CacheWriteTokens int `json:"cache_write_tokens,omitempty"`
CacheReadTokens int `json:"cache_read_tokens,omitempty"`
CostUSD float64 `json:"cost_usd,omitempty"`
TrackedExternallyBy string `json:"tracked_externally_by,omitempty"`
}
LogEntry represents a single usage log entry written by term-llm
type Logger ¶
type Logger struct {
// contains filtered or unexported fields
}
Logger writes usage entries to daily JSONL files
func DefaultLogger ¶
func DefaultLogger() *Logger
DefaultLogger returns the singleton logger instance
type ModelBreakdown ¶
type ModelBreakdown struct {
Model string
InputTokens int
OutputTokens int
CacheWriteTokens int
CacheReadTokens int
ReasoningTokens int
Cost float64
}
ModelBreakdown represents usage breakdown by model
func GetModelBreakdown ¶
func GetModelBreakdown(entries []UsageEntry) []ModelBreakdown
GetModelBreakdown returns token usage broken down by model for a set of entries
type ModelPricing ¶
type ModelPricing struct {
InputCostPerToken float64 `json:"input_cost_per_token"`
OutputCostPerToken float64 `json:"output_cost_per_token"`
CacheCreationInputTokenCost float64 `json:"cache_creation_input_token_cost"`
CacheReadInputTokenCost float64 `json:"cache_read_input_token_cost"`
InputCostPerTokenAbove200k float64 `json:"input_cost_per_token_above_200k_tokens"`
OutputCostPerTokenAbove200k float64 `json:"output_cost_per_token_above_200k_tokens"`
CacheCreationCostAbove200k float64 `json:"cache_creation_input_token_cost_above_200k_tokens"`
CacheReadCostAbove200k float64 `json:"cache_read_input_token_cost_above_200k_tokens"`
// TieredThreshold overrides the legacy 200K threshold. WholeRequestTier
// means crossing the threshold reprices every token in the request rather
// than only tokens above the threshold (the GPT-5.6 pricing contract).
TieredThreshold int `json:"-"`
WholeRequestTier bool `json:"-"`
}
ModelPricing contains pricing information for a model
type PricingFetcher ¶
type PricingFetcher struct {
// contains filtered or unexported fields
}
PricingFetcher fetches and caches model pricing from LiteLLM
func NewPricingFetcher ¶
func NewPricingFetcher() *PricingFetcher
NewPricingFetcher creates a new pricing fetcher
func (*PricingFetcher) CalculateCost ¶
func (p *PricingFetcher) CalculateCost(entry UsageEntry) (float64, error)
CalculateCost calculates the cost for a usage entry
func (*PricingFetcher) CalculateCostLocal ¶ added in v0.0.328
func (p *PricingFetcher) CalculateCostLocal(entry UsageEntry) (float64, error)
CalculateCostLocal calculates cost without fetching pricing from the network.
func (*PricingFetcher) GetPricing ¶
func (p *PricingFetcher) GetPricing(modelName string) (ModelPricing, error)
GetPricing returns pricing for a model, fetching if necessary
func (*PricingFetcher) GetPricingLocal ¶ added in v0.0.328
func (p *PricingFetcher) GetPricingLocal(modelName string) (ModelPricing, error)
GetPricingLocal returns bundled pricing or pricing from the existing on-disk cache. It never performs a network request and accepts stale cache data: exit summaries must remain immediate even when the network is unavailable.
func (*PricingFetcher) RefreshModelsDevPricing ¶ added in v0.9.49
func (p *PricingFetcher) RefreshModelsDevPricing(ctx context.Context) error
RefreshModelsDevPricing updates the cached model directory when the copy on disk is older than the refresh interval. It is safe to call on a background goroutine at startup; callers that want the bundled table only can skip it.
type ProviderBreakdown ¶
type ProviderBreakdown struct {
Provider string
InputTokens int
OutputTokens int
CacheWriteTokens int
CacheReadTokens int
ReasoningTokens int
Cost float64
}
ProviderBreakdown represents usage breakdown by provider
func GetProviderBreakdown ¶
func GetProviderBreakdown(entries []UsageEntry) []ProviderBreakdown
GetProviderBreakdown returns token usage broken down by provider for a set of entries
type UsageEntry ¶
type UsageEntry struct {
Timestamp time.Time
SessionID string
Model string
InputTokens int
OutputTokens int
CacheWriteTokens int // cache_creation for Claude
CacheReadTokens int // cache_read tokens
ReasoningTokens int // provider-reported reasoning tokens
CostUSD float64 // Pre-calculated cost if available
Provider string // Usage data source, such as ProviderClaudeCode or ProviderTermLLM
TrackedExternallyBy string // External tracker name, or empty for direct API usage
// PreAggregated marks counters summed over many requests. Long-context
// pricing applies per request, so a summed bucket must never be repriced at
// the tiered rate: a session of small requests would be billed as one
// enormous one, at up to double the real rate.
PreAggregated bool
}
UsageEntry represents a single usage event from any provider
func CalculateCosts ¶
func CalculateCosts(entries []UsageEntry, fetcher *PricingFetcher) []UsageEntry
CalculateCosts uses the pricing fetcher to calculate costs for entries without pre-calculated costs
func (UsageEntry) TotalTokens ¶
func (e UsageEntry) TotalTokens() int
TotalTokens returns the sum of all token types