cost

package
v0.4.0 Latest Latest
Warning

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

Go to latest
Published: Mar 12, 2026 License: MIT Imports: 4 Imported by: 0

Documentation

Overview

Package cost tracks token usage and estimated costs from Claude CLI output.

Claude's --output-format stream-json emits usage events with token counts. This package parses those events, stores per-bead and per-day aggregates in state.db, and optionally enforces a daily cost limit.

Token pricing (Claude Sonnet 4, approximate):

Input:  $3.00 per 1M tokens
Output: $15.00 per 1M tokens
Cache read: $0.30 per 1M tokens
Cache write: $3.75 per 1M tokens

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CopilotPremiumMultiplier added in v0.2.0

func CopilotPremiumMultiplier(model string) float64

CopilotPremiumMultiplier returns the premium request multiplier for a given Copilot model name. Models not in the lookup table default to 1.0x.

Reference (2026 pricing):

claude-opus-4.6:        3x
claude-opus-4.6-fast:  30x
claude-opus-4.5:        3x
claude-sonnet-4.6/4.5/4: 1x
claude-haiku-4.5:       0.33x
gpt-5.4/5.3-codex/5.2-codex/5.2/5.1-codex-max/5.1-codex/5.1: 1x
gpt-5.1-codex-mini:     0.33x
gpt-5-mini/gpt-4.1:     0x (free)
gemini-3-pro-preview/gemini-2.5-pro: 1x

func Today

func Today() string

Today returns today's date string in YYYY-MM-DD format.

Types

type BeadCost

type BeadCost struct {
	BeadID    string    `json:"bead_id"`
	Anvil     string    `json:"anvil"`
	Usage     Usage     `json:"usage"`
	UpdatedAt time.Time `json:"updated_at"`
}

BeadCost stores cumulative cost data for a specific bead.

type DailyCost

type DailyCost struct {
	Date  string  `json:"date"` // YYYY-MM-DD
	Usage Usage   `json:"usage"`
	Limit float64 `json:"limit,omitempty"` // 0 = no limit
}

DailyCost stores aggregated cost data for a specific day.

type Pricing

type Pricing 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"`
}

Pricing defines per-token costs in USD per million tokens.

func CopilotPricing added in v0.2.0

func CopilotPricing() Pricing

CopilotPricing returns approximate GitHub Copilot pricing. Copilot runs Claude models under the hood; we use Claude's pricing as a reasonable cost estimate for token-level tracking.

func DefaultPricing

func DefaultPricing() Pricing

DefaultPricing returns approximate Claude Sonnet 3.5 pricing.

func GeminiPricing

func GeminiPricing() Pricing

GeminiPricing returns approximate Gemini 1.5 Pro pricing.

type Usage

type Usage struct {
	InputTokens      int     `json:"input_tokens"`
	OutputTokens     int     `json:"output_tokens"`
	CacheReadTokens  int     `json:"cache_read_tokens"`
	CacheWriteTokens int     `json:"cache_write_tokens"`
	EstimatedCostUSD float64 `json:"estimated_cost_usd"`
}

Usage tracks token usage for a single Claude invocation.

func ParseResultJSON

func ParseResultJSON(data []byte) Usage

ParseResultJSON parses a single Claude result JSON (non-streaming).

func ParseStreamJSON

func ParseStreamJSON(r io.Reader) Usage

ParseStreamJSON reads Claude stream-json output and extracts total usage. It scans for events with "type": "result" or usage fields.

func (*Usage) Add

func (u *Usage) Add(other Usage)

Add merges another Usage into this one.

func (*Usage) Calculate

func (u *Usage) Calculate(p Pricing)

Calculate computes the estimated cost based on pricing.

Jump to

Keyboard shortcuts

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