cost

package
v0.0.1 Latest Latest
Warning

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

Go to latest
Published: Sep 13, 2026 License: MIT Imports: 9 Imported by: 0

Documentation

Overview

Package cost provides token-cost estimation for common AI model providers. Prices are per 1M tokens and are approximate — they may lag behind official pricing changes. Always verify with your provider's pricing page.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AppendLedger

func AppendLedger(workDir string, entry LedgerEntry) error

AppendLedger appends a cost entry to the project's SQLite database (primary) and to .cloop/costs.jsonl (legacy fallback for backward compatibility). It also mirrors to the global ledger at ~/.config/cloop/costs.jsonl.

func Estimate

func Estimate(model string, inputTokens, outputTokens int) (usd float64, ok bool)

Estimate returns the estimated cost in USD for the given token counts. model is matched case-insensitively; unrecognised models return (0, false).

func EstimateSessionCost

func EstimateSessionCost(provider, model string, inputTokens, outputTokens int) float64

EstimateSessionCost returns the estimated cost in USD for the session. provider is used as a hint: "ollama" always returns 0 (local). For unknown models the function returns 0. Returns 0 when cost cannot be determined.

func FormatCost

func FormatCost(usd float64) string

FormatCost returns a human-readable cost string, e.g. "$0.0042" or "$1.23".

func FormatCostWithLimit

func FormatCostWithLimit(usd, limit float64) string

FormatCostWithLimit returns a human-readable cost string with optional limit info. e.g. "$0.0042 / $1.00" when limit > 0.

func MonthlyTotal

func MonthlyTotal(workDir string) (float64, error)

MonthlyTotal returns the total estimated USD spent in the current calendar month.

Types

type LedgerEntry

type LedgerEntry struct {
	Timestamp      time.Time `json:"timestamp"`
	TaskID         int       `json:"task_id"`
	TaskTitle      string    `json:"task_title"`
	Provider       string    `json:"provider"`
	Model          string    `json:"model"`
	InputTokens    int       `json:"input_tokens"`
	OutputTokens   int       `json:"output_tokens"`
	ThinkingTokens int       `json:"thinking_tokens,omitempty"`
	EstimatedUSD   float64   `json:"estimated_usd"`
}

LedgerEntry records the cost of one task execution.

func ReadLedger

func ReadLedger(workDir string) ([]LedgerEntry, error)

ReadLedger reads all cost entries. When a SQLite state.db exists it reads from the costs table (primary). It then merges any JSONL-only entries that are not yet in the database (legacy migration path). Returns an empty slice (not an error) when neither source has data.

type ModelPricing

type ModelPricing struct {
	InputPerM  float64
	OutputPerM float64
}

ModelPricing holds the input and output cost in USD per 1M tokens.

Jump to

Keyboard shortcuts

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