cost

package
v1.3.98 Latest Latest
Warning

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

Go to latest
Published: Jul 1, 2026 License: MIT Imports: 8 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.

func IsCodingPlanEndpoint added in v1.3.98

func IsCodingPlanEndpoint(endpoint string) bool

IsCodingPlanEndpoint returns true if the endpoint name indicates a coding plan. Coding plan endpoints use subscription billing (monthly quota), not per-token. Examples: "cn-coding-openai", "global-coding-anthropic", "kimi-coding", "ark-coding"

func IsSubscriptionVendor added in v1.3.98

func IsSubscriptionVendor(vendor string) string

IsSubscriptionVendor returns the plan name if the vendor is entirely subscription-based (all built-in endpoints are coding/token plans). Returns empty string if the vendor is not a subscription vendor.

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,omitempty"`
	OutputPerM     float64     `json:"output_per_m,omitempty"`
	CacheReadPerM  float64     `json:"cache_read_per_m,omitempty"`
	CacheWritePerM float64     `json:"cache_write_per_m,omitempty"`
	Type           PricingType `json:"type,omitempty"`
	Plan           string      `json:"plan,omitempty"` // e.g., "GitHub Copilot", "GLM Coding Plan"
}

ModelRate holds pricing info for a single model. For subscription/bundled/free models, the per-token fields are zero and PricingType indicates the billing model.

func (ModelRate) IsKnown added in v1.3.98

func (r ModelRate) IsKnown() bool

IsKnown returns true if any pricing information is available.

func (ModelRate) IsMetered added in v1.3.98

func (r ModelRate) IsMetered() bool

IsMetered returns true if the model is billed per-token.

type PricingTable

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

PricingTable maps provider+model to pricing info.

func DefaultPricingTable

func DefaultPricingTable() PricingTable

DefaultPricingTable returns built-in billing-type info for known providers.

IMPORTANT: We deliberately do NOT hardcode per-token prices. Model pricing changes frequently and stale data is misleading. This table only contains billing TYPE information (subscription vs per-token vs free) that is verifiable from each provider's official pricing page.

For per-token pricing, users can configure custom rates via Merge().

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. Uses fuzzy matching for versioned model names.

func (PricingTable) Merge

func (t PricingTable) Merge(other PricingTable) PricingTable

Merge overlays another pricing table on top of this one.

type PricingType added in v1.3.98

type PricingType string

PricingType indicates how a model is billed.

const (
	// PricingUnknown: no pricing data available. Display "(no pricing data)".
	// This is the default — we do NOT hardcode per-token prices because
	// model pricing changes frequently and stale data is worse than no data.
	PricingUnknown PricingType = ""
	// PricingPerToken: standard per-million-token billing.
	// Only set when the user has explicitly configured pricing via Merge().
	PricingPerToken PricingType = "per_token"
	// PricingSubscription: flat-rate subscription with monthly quota.
	// Display "included in <Plan>" instead of dollar amount.
	PricingSubscription PricingType = "subscription"
	// PricingBundled: included in a bundle (e.g., Google One AI Premium).
	PricingBundled PricingType = "bundled"
	// PricingFree: free tier, no cost.
	PricingFree PricingType = "free"
)

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