cost

package
v0.11.0-beta.2 Latest Latest
Warning

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

Go to latest
Published: Oct 23, 2026 License: Apache-2.0, MIT Imports: 12 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrBudgetExceeded     = errors.New("cost: budget exceeded")
	ErrDailyLimitExceeded = errors.New("cost: daily limit exceeded")
	ErrTaskLimitExceeded  = errors.New("cost: task budget exceeded")
)

Functions

func CalculateCost

func CalculateCost(usage providers.Usage, model providers.Model) float64

func FormatTokens

func FormatTokens(n int64) string

FormatTokens formats a token count into a human-readable abbreviated form:

<1000      → raw number (e.g. "234")
1k-999k    → k suffix with 1 decimal (e.g. "45.2k")
1M-999M    → M suffix with 1 decimal (e.g. "1.5M")
1B-999B    → B suffix with 1 decimal (e.g. "2.1B")
1T+        → T suffix with 1 decimal

func FormatUSD

func FormatUSD(n float64) string

FormatUSD formats a dollar amount:

<1000      → "$1.23"
1k+        → "$1.2k"

Types

type BudgetStatus

type BudgetStatus struct {
	DailyUsed    float64       `json:"daily_used"`
	DailyLimit   float64       `json:"daily_limit"`
	DailyPercent float64       `json:"daily_percent"`
	TaskUsed     float64       `json:"task_used"`
	TaskLimit    float64       `json:"task_limit"`
	TaskPercent  float64       `json:"task_percent"`
	RollingUsed  float64       `json:"rolling_used"`
	Window       time.Duration `json:"window_seconds"`
	ShouldWarn   bool          `json:"should_warn"`
	ShouldAbort  bool          `json:"should_abort"`
}

type CostBreakdown

type CostBreakdown struct {
	InputCost  float64
	OutputCost float64
	CacheCost  float64
	Total      float64
}

func CalculateCostDetailed

func CalculateCostDetailed(usage providers.Usage, model providers.Model) CostBreakdown

type CostSummary

type CostSummary struct {
	TotalUSD     float64 `json:"total_usd"`
	InputTokens  int64   `json:"input_tokens"`
	OutputTokens int64   `json:"output_tokens"`
	CachedTokens int64   `json:"cached_tokens"`
	RequestCount int64   `json:"request_count"`
}

type Entry

type Entry struct {
	ID           string    `json:"id,omitempty"`
	SessionID    string    `json:"session_id,omitempty"`
	Model        string    `json:"model,omitempty"`
	Provider     string    `json:"provider,omitempty"`
	Timestamp    time.Time `json:"timestamp,omitempty"`
	InputTokens  int       `json:"input_tokens"`
	OutputTokens int       `json:"output_tokens"`
	CachedTokens int       `json:"cached_tokens"`
	CostUSD      float64   `json:"cost_usd"`
	TaskID       string    `json:"task_id,omitempty"`
}

type PostgresTracker

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

func NewPostgresTracker

func NewPostgresTracker(db *sql.DB, cfg config.CostConfig) (*PostgresTracker, error)

func (*PostgresTracker) CheckBudget

func (t *PostgresTracker) CheckBudget(ctx context.Context, sessionID string) (BudgetStatus, error)

func (*PostgresTracker) Close

func (t *PostgresTracker) Close() error

func (*PostgresTracker) DailyCost

func (t *PostgresTracker) DailyCost(ctx context.Context) (CostSummary, error)

func (*PostgresTracker) Record

func (t *PostgresTracker) Record(ctx context.Context, entry Entry) error

func (*PostgresTracker) RegisterModel

func (t *PostgresTracker) RegisterModel(m providers.Model)

func (*PostgresTracker) RollingCost

func (t *PostgresTracker) RollingCost(ctx context.Context, window time.Duration) (CostSummary, error)

func (*PostgresTracker) SessionCost

func (t *PostgresTracker) SessionCost(ctx context.Context, sessionID string) (CostSummary, error)

func (*PostgresTracker) Usage

func (t *PostgresTracker) Usage(ctx context.Context, opts UsageOptions) (*UsageReport, error)

type Tracker

type Tracker interface {
	Record(ctx context.Context, entry Entry) error
	SessionCost(ctx context.Context, sessionID string) (CostSummary, error)
	DailyCost(ctx context.Context) (CostSummary, error)
	RollingCost(ctx context.Context, window time.Duration) (CostSummary, error)
	CheckBudget(ctx context.Context, sessionID string) (BudgetStatus, error)
	Usage(ctx context.Context, opts UsageOptions) (*UsageReport, error)
	Close() error
}

type UsageOptions

type UsageOptions struct {
	SessionID string
	Provider  string
	Model     string
	StartTime time.Time
	EndTime   time.Time
	Limit     int
}

type UsageReport

type UsageReport struct {
	Summary    CostSummary            `json:"summary"`
	ByModel    map[string]CostSummary `json:"by_model"`
	ByProvider map[string]CostSummary `json:"by_provider"`
	Entries    []Entry                `json:"entries,omitempty"`
}

Jump to

Keyboard shortcuts

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