Help us shape the future of Go! Take the 2026 Go Developer Survey

costs

package
v2.0.0 Latest Latest
Warning

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

Go to latest
Published: Oct 9, 2026 License: MIT Imports: 7 Imported by: 0

Documentation

Overview

Package costs provides token cost tracking and pricing for LLM models.

Index

Constants

This section is empty.

Variables

View Source
var ModelPricing = map[string]ModelCost{

	"gpt-4.1":       {Input: 2.50, Output: 15.00, CacheRead: 0.625},
	"gpt-4.1-mini":  {Input: 0.75, Output: 4.50, CacheRead: 0.1875},
	"gpt-4.1-nano":  {Input: 0.20, Output: 1.25, CacheRead: 0.05},
	"gpt-5.3-codex": {Input: 2.50, Output: 15.00, CacheRead: 0.25},
	"gpt-5.4":       {Input: 2.50, Output: 15.00, CacheRead: 0.25},
	"gpt-5.4-mini":  {Input: 0.75, Output: 4.50, CacheRead: 0.075},
	"gpt-5.4-nano":  {Input: 0.20, Output: 1.25, CacheRead: 0.02},
	"gpt-5.4-pro":   {Input: 15.00, Output: 60.00},
	"o3":            {Input: 2.50, Output: 15.00, CacheRead: 0.625},
	"o4-mini":       {Input: 0.75, Output: 4.50, CacheRead: 0.1875},

	"grok-build-0.1":              {Input: 1.00, Output: 2.00, CacheRead: 0.20},
	"grok-4-1-fast":               {Input: 0.20, Output: 0.50, CacheRead: 0.05},
	"grok-4-1-fast-reasoning":     {Input: 0.20, Output: 0.50, CacheRead: 0.05},
	"grok-4-1-fast-non-reasoning": {Input: 0.20, Output: 0.50, CacheRead: 0.05},

	"grok-4.3":                     {Input: 1.25, Output: 2.50},
	"grok-4.20-0309-reasoning":     {Input: 1.25, Output: 2.50},
	"grok-4.20-0309-non-reasoning": {Input: 1.25, Output: 2.50},
	"grok-4.20-multi-agent-0309":   {Input: 1.25, Output: 2.50},
	"grok-code-fast-1":             {Input: 0.20, Output: 0.50},

	"gemini-2.0-flash": {Input: 0.10, Output: 0.40, CacheRead: 0.025},

	"claude-fable-5-1":  {Input: 10.00, Output: 50.00, CacheRead: 0.25, CacheWrite: 12.50, CacheWrite1h: 20.00},
	"claude-fable-5":    {Input: 10.00, Output: 50.00, CacheRead: 0.25, CacheWrite: 12.50, CacheWrite1h: 20.00},
	"claude-opus-5-5":   {Input: 4.00, Output: 20.00, CacheRead: 0.20, CacheWrite: 5.00, CacheWrite1h: 8.00},
	"claude-opus-5":     {Input: 5.00, Output: 25.00, CacheRead: 0.50, CacheWrite: 6.25, CacheWrite1h: 10.00},
	"claude-opus-4-8":   {Input: 5.00, Output: 25.00, CacheRead: 0.50, CacheWrite: 6.25, CacheWrite1h: 10.00},
	"claude-opus-4-7":   {Input: 5.00, Output: 25.00, CacheRead: 0.50, CacheWrite: 6.25, CacheWrite1h: 10.00},
	"claude-opus-4-6":   {Input: 5.00, Output: 25.00, CacheRead: 0.50, CacheWrite: 6.25, CacheWrite1h: 10.00},
	"claude-sonnet-5-5": {Input: 2.00, Output: 10.00, CacheRead: 0.20, CacheWrite: 2.50, CacheWrite1h: 4.00},
	"claude-sonnet-5":   {Input: 2.00, Output: 10.00, CacheRead: 0.20, CacheWrite: 2.50, CacheWrite1h: 4.00},
	"claude-sonnet-4-6": {Input: 3.00, Output: 15.00, CacheRead: 0.30, CacheWrite: 3.75, CacheWrite1h: 6.00},
	"claude-sonnet-4-5": {Input: 3.00, Output: 15.00, CacheRead: 0.30, CacheWrite: 3.75, CacheWrite1h: 6.00},
	"claude-haiku-4-5":  {Input: 1.00, Output: 5.00, CacheRead: 0.10, CacheWrite: 1.25, CacheWrite1h: 2.00},

	"venice-uncensored":                    {Input: 0.20, Output: 0.90},
	"venice-uncensored-role-play":          {Input: 0.50, Output: 2.00},
	"deepseek-v3.2":                        {Input: 0.33, Output: 0.48},
	"qwen3-coder-480b-a35b-instruct":       {Input: 0.75, Output: 3.00},
	"qwen3-coder-480b-a35b-instruct-turbo": {Input: 0.35, Output: 1.50},
	"qwen3-235b-a22b-thinking-2507":        {Input: 0.45, Output: 3.50},
	"kimi-k2-5":                            {Input: 0.56, Output: 3.50},
	"zai-org-glm-4.7":                      {Input: 0.55, Output: 2.65},
	"mistral-small-3-2-24b-instruct":       {Input: 0.09, Output: 0.25},
	"llama-3.3-70b":                        {Input: 0.70, Output: 2.80},
	"minimax-m25":                          {Input: 0.34, Output: 1.19},
}

ModelPricing maps model identifiers to their costs.

Functions

func CostOf

func CostOf(model string, u Usage) float64

CostOf is the USD cost of one request's usage on model, cache reads and writes at their own rates (#312). Returns 0 if the model is not in the pricing table.

func GetCost

func GetCost(model string, inputTokens, outputTokens int) float64

GetCost is CostOf for usage with no cached tokens.

func Priced

func Priced(model string) bool

Priced reports whether model is in the pricing table.

Types

type CostSummary

type CostSummary struct {
	Model           string  `json:"model"`
	TotalInput      int     `json:"total_input_tokens"`
	TotalOutput     int     `json:"total_output_tokens"`
	TotalCacheRead  int     `json:"total_cache_read_tokens"`
	TotalCacheWrite int     `json:"total_cache_write_tokens"`
	TotalCostUSD    float64 `json:"total_cost_usd"`
	Turns           int     `json:"turns"`
	// Requests counts every priced or unpriced request, a dropped reply's too.
	Requests int `json:"requests"`
	// Unpriced counts requests on models missing from ModelPricing: their
	// tokens are totalled but cost nothing, so a zero cost is not read as free.
	Unpriced int `json:"unpriced_requests"`
}

CostSummary is a snapshot of cumulative session costs.

type ModelCost

type ModelCost struct {
	Input  float64 // USD per 1M input tokens
	Output float64 // USD per 1M output tokens
	// CacheRead, CacheWrite (5-minute entries) and CacheWrite1h price the
	// prompt tokens read from or written to the prompt cache (#312). 0: the
	// rate is not modeled, and those tokens are priced at Input.
	CacheRead    float64
	CacheWrite   float64
	CacheWrite1h float64
}

ModelCost holds the per-1M-token pricing for a model.

type SessionTracker

type SessionTracker struct {
	Model           string  `json:"model"`
	TotalInput      int     `json:"total_input"`
	TotalOutput     int     `json:"total_output"`
	TotalCacheRead  int     `json:"total_cache_read"`
	TotalCacheWrite int     `json:"total_cache_write"`
	TotalCostUSD    float64 `json:"total_cost_usd"`
	Turns           int     `json:"turns"`
	Requests        int     `json:"requests"`
	Unpriced        int     `json:"unpriced_requests"`
	// contains filtered or unexported fields
}

SessionTracker accumulates token usage and cost across a session.

func NewSessionTracker

func NewSessionTracker() *SessionTracker

NewSessionTracker creates a new empty tracker.

func (*SessionTracker) GetSummary

func (t *SessionTracker) GetSummary() CostSummary

GetSummary returns a snapshot of the current session cost state.

func (*SessionTracker) Load

func (t *SessionTracker) Load(path string) error

Load deserialises tracker state from a JSON file.

func (*SessionTracker) RecordCost

func (t *SessionTracker) RecordCost(model string, u Usage)

RecordCost adds usage and its cost without counting a turn: a reply a stream rule dropped was billed, but the person never saw it (2.0 W3).

func (*SessionTracker) RecordUsage

func (t *SessionTracker) RecordUsage(model string, u Usage)

RecordUsage adds a turn's token usage and computes the incremental cost.

func (*SessionTracker) Save

func (t *SessionTracker) Save(path string) error

Save serialises the tracker state to a JSON file.

type Usage

type Usage struct {
	Input, Output                       int
	CacheRead, CacheWrite, CacheWrite1h int
}

Usage is one request's tokens. Input is the whole prompt, cached tokens included (as llm.TokenUsage.PromptTokens counts it); CacheRead and CacheWrite are the parts of it read from and written to the cache, and CacheWrite1h the part of CacheWrite written with a 1-hour lifetime.

Jump to

Keyboard shortcuts

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