cost

package
v1.3.35 Latest Latest
Warning

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

Go to latest
Published: May 25, 2026 License: MIT Imports: 7 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func FormatCost

func FormatCost(usd float64) string

FormatCost returns a human-readable cost string.

func FormatSessionCost

func FormatSessionCost(sc SessionCost, now time.Time) string

FormatSessionCost returns a formatted summary for a session.

func FormatTokens

func FormatTokens(n int64) string

FormatTokens returns a human-readable token count.

Types

type Manager

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

Manager tracks cost across all sessions.

func NewManager

func NewManager(pricing PricingTable, dataDir string) *Manager

NewManager creates a cost manager with the given pricing table.

func (*Manager) AllCosts

func (m *Manager) AllCosts() []SessionCost

AllCosts returns costs for all sessions, sorted by total cost descending.

func (*Manager) GetOrCreateTracker

func (m *Manager) GetOrCreateTracker(sessionID, providerName, model string) *Tracker

GetOrCreateTracker returns the tracker for a session, creating one if needed.

func (*Manager) Load

func (m *Manager) Load(sessionID, providerName, model string)

Load restores session cost data from disk.

func (*Manager) Save

func (m *Manager) Save(sessionID string) error

Save persists session cost data to disk.

func (*Manager) SessionCost

func (m *Manager) SessionCost(sessionID string) (SessionCost, bool)

SessionCost returns cost for a specific session.

func (*Manager) TotalCost

func (m *Manager) TotalCost() float64

TotalCost returns the sum of all session costs.

type ModelRate

type ModelRate struct {
	InputPerM      float64 `json:"input_per_m"`
	OutputPerM     float64 `json:"output_per_m"`
	CacheReadPerM  float64 `json:"cache_read_per_m"`
	CacheWritePerM float64 `json:"cache_write_per_m"`
}

ModelRate holds per-million-token pricing for a single model.

type PricingTable

type PricingTable map[string]map[string]ModelRate // provider → model → rate

PricingTable maps provider+model to pricing rates.

func DefaultPricingTable

func DefaultPricingTable() PricingTable

DefaultPricingTable returns built-in pricing for known models.

func (PricingTable) Get

func (t PricingTable) Get(provider, model string) (ModelRate, bool)

Get returns the rate for a provider+model, or false if not found.

func (PricingTable) Merge

func (t PricingTable) Merge(other PricingTable) PricingTable

Merge overlays another pricing table on top of this one.

type SessionCost

type SessionCost struct {
	Provider         string  `json:"provider"`
	Model            string  `json:"model"`
	InputTokens      int64   `json:"input_tokens"`
	OutputTokens     int64   `json:"output_tokens"`
	CacheReadTokens  int64   `json:"cache_read_tokens"`
	CacheWriteTokens int64   `json:"cache_write_tokens"`
	TotalCostUSD     float64 `json:"total_cost_usd"`
}

SessionCost tracks cumulative token usage and estimated cost for a session.

type TokenUsage

type TokenUsage struct {
	InputTokens  int `json:"input_tokens"`
	OutputTokens int `json:"output_tokens"`
	CacheRead    int `json:"cache_read_tokens"`
	CacheWrite   int `json:"cache_write_tokens"`
}

TokenUsage records token consumption for a single API call. Defined in the cost package to avoid circular imports.

type Tracker

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

Tracker accumulates token usage and computes cost.

func NewTracker

func NewTracker(provider, model string, pricing PricingTable) *Tracker

NewTracker creates a cost tracker for the given provider/model.

func (*Tracker) Record

func (t *Tracker) Record(usage TokenUsage)

Record adds a usage update from an API call.

func (*Tracker) SessionCost

func (t *Tracker) SessionCost() SessionCost

SessionCost returns a snapshot of the current cost.

Jump to

Keyboard shortcuts

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