cost

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Mar 9, 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 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 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