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 ¶
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 ¶
ParseResultJSON parses a single Claude result JSON (non-streaming).
func ParseStreamJSON ¶
ParseStreamJSON reads Claude stream-json output and extracts total usage. It scans for events with "type": "result" or usage fields.