diff --git a/app/cli/internal/trace/claude/parse.go b/app/cli/internal/trace/claude/parse.go index c9a2f65ad..dff5cf444 100644 --- a/app/cli/internal/trace/claude/parse.go +++ b/app/cli/internal/trace/claude/parse.go @@ -169,7 +169,7 @@ func parseJSONL(path string) (*sessionData, []json.RawMessage, error) { data.modelsSeen[model]++ if record.Message.Usage != nil { - getOrCreateUsage(data.tokenUsageByModel, model).add(record.Message.Usage) + addUsage(data.tokenUsageByModel, model, record.Message.Usage) } // Content can be a string or an array of blocks; only parse arrays @@ -256,7 +256,7 @@ func parseSubagentUsage(jsonlPath string, totalUsage map[string]*tokenUsage, war u := record.Message.Usage subInput += u.InputTokens subOutput += u.OutputTokens - getOrCreateUsage(totalUsage, model).add(u) + addUsage(totalUsage, model, u) } }, rawLines); err != nil { addWarning(warnings, fmt.Sprintf("Failed to read subagent %s: %v", filepath.Base(jsonlPath), err)) @@ -273,19 +273,25 @@ func mergeUsage(main, sub map[string]*tokenUsage) map[string]*tokenUsage { merged := make(map[string]*tokenUsage) for _, source := range []map[string]*tokenUsage{main, sub} { for model, u := range source { - bucket := getOrCreateUsage(merged, model) - bucket.InputTokens += u.InputTokens - bucket.OutputTokens += u.OutputTokens - bucket.CacheCreationInputTokens += u.CacheCreationInputTokens - bucket.CacheCreation5mTokens += u.CacheCreation5mTokens - bucket.CacheCreation1hTokens += u.CacheCreation1hTokens - bucket.CacheReadInputTokens += u.CacheReadInputTokens + getOrCreateUsage(merged, model).merge(u) } } return merged } +// addUsage adds the usage of one request to the model's bucket. A request whose prompt is +// larger than the model's long-context threshold is also counted in the long-context subset, +// so that computeCost prices it at the long-context rates. +func addUsage(m map[string]*tokenUsage, model string, u *jsonlUsage) { + bucket := getOrCreateUsage(m, model) + bucket.add(u) + + if p, ok := pricing[model]; ok && p.LongContext != nil && u.promptTokens() > p.LongContextThreshold { + bucket.longContext().add(u) + } +} + func computeCost(usage map[string]*tokenUsage, warnings *[]string) float64 { totalCost := 0.0 for model, u := range usage { @@ -296,12 +302,11 @@ func computeCost(usage map[string]*tokenUsage, warnings *[]string) float64 { fmt.Sprintf("Unknown model '%s', using default (sonnet) pricing", model)) } - cost := float64(u.InputTokens)*p.Input/1_000_000 + - float64(u.OutputTokens)*p.Output/1_000_000 + - float64(u.CacheCreation5mTokens)*p.CacheWrite/1_000_000 + - float64(u.CacheCreation1hTokens)*p.CacheWrite1h/1_000_000 + - float64(u.CacheReadInputTokens)*p.CacheRead/1_000_000 - totalCost += cost + totalCost += p.cost(u) + // Reprice the long-context subset: remove it at the standard rates, add it at the long-context rates. + if u.LongContext != nil && p.LongContext != nil { + totalCost += p.LongContext.cost(u.LongContext) - p.cost(u.LongContext) + } } return math.Round(totalCost*10000) / 10000 diff --git a/app/cli/internal/trace/claude/parse_test.go b/app/cli/internal/trace/claude/parse_test.go index bf16a97fc..c0a117a6f 100644 --- a/app/cli/internal/trace/claude/parse_test.go +++ b/app/cli/internal/trace/claude/parse_test.go @@ -198,6 +198,81 @@ func TestComputeCost_SplitCacheTiers(t *testing.T) { assert.Empty(t, warnings) } +// haiku55 is a model with long-context rates. +const haiku55 = "claude-haiku-5-5" + +func TestComputeCostLongContext(t *testing.T) { + small := &jsonlUsage{InputTokens: 50_000, CacheReadInputTokens: 40_000, OutputTokens: 1_000} + large := &jsonlUsage{InputTokens: 10_000, CacheReadInputTokens: 100_000, OutputTokens: 1_000} + + testCases := []struct { + name string + model string + requests []*jsonlUsage + want float64 + }{ + { + // 50k * $0.10 + 40k * $0.01 + 1k * $0.50 + name: "prompt under threshold uses standard rates", model: haiku55, + requests: []*jsonlUsage{small}, want: 0.0059, + }, + { + // 100k * $0.10: the long-context rates start above the threshold + name: "prompt at threshold uses standard rates", model: haiku55, + requests: []*jsonlUsage{{InputTokens: 100_000}}, want: 0.01, + }, + { + // 10k * $0.50 + 100k * $0.05 + 1k * $2.50 + name: "prompt over threshold uses long-context rates", model: haiku55, + requests: []*jsonlUsage{large}, want: 0.0125, + }, + { + name: "each request is priced on its own prompt size", model: haiku55, + requests: []*jsonlUsage{small, large}, want: 0.0059 + 0.0125, + }, + { + // 5m: 100k * $0.625, 1h: 100k * $1.00 + name: "cache writes count toward the prompt size", model: haiku55, + requests: []*jsonlUsage{{ + CacheCreationInputTokens: 200_000, + CacheCreation: &jsonlCacheCreationInfo{Ephemeral5mInputTokens: 100_000, Ephemeral1hInputTokens: 100_000}, + }}, + want: 0.1625, + }, + { + // 200k * $5.00: no long-context rates + name: "model without long-context rates", model: "claude-opus-4-7", + requests: []*jsonlUsage{{InputTokens: 200_000}}, want: 1.0, + }, + } + + for _, tc := range testCases { + t.Run(tc.name, func(t *testing.T) { + usage := make(map[string]*tokenUsage) + for _, r := range tc.requests { + addUsage(usage, tc.model, r) + } + var warnings []string + assert.InDelta(t, tc.want, computeCost(usage, &warnings), 0.00001) + assert.Empty(t, warnings) + }) + } +} + +func TestMergeUsageLongContext(t *testing.T) { + main := map[string]*tokenUsage{ + haiku55: {InputTokens: 300_000, LongContext: &tokenUsage{InputTokens: 200_000}}, + } + sub := map[string]*tokenUsage{ + haiku55: {InputTokens: 150_000, LongContext: &tokenUsage{InputTokens: 150_000}}, + } + + merged := mergeUsage(main, sub) + assert.Equal(t, 450_000, merged[haiku55].InputTokens) + require.NotNil(t, merged[haiku55].LongContext) + assert.Equal(t, 350_000, merged[haiku55].LongContext.InputTokens) +} + func TestComputeCostUnknownModel(t *testing.T) { usage := map[string]*tokenUsage{ "claude-unknown-99": {InputTokens: 1_000_000}, diff --git a/app/cli/internal/trace/claude/pricing.go b/app/cli/internal/trace/claude/pricing.go index 0dd042eb4..71589115c 100644 --- a/app/cli/internal/trace/claude/pricing.go +++ b/app/cli/internal/trace/claude/pricing.go @@ -33,6 +33,20 @@ type modelPricing struct { CacheWrite1h float64 `json:"cache_write_1h"` // CacheRead is the price per 1M tokens read from the prompt cache (0.1x base input on most models; see pricing.json notes for exceptions). CacheRead float64 `json:"cache_read"` + // LongContextThreshold is the prompt size in tokens (input, cache reads and cache writes) + // above which a request is priced at LongContext. Zero when the model has one price. + LongContextThreshold int `json:"long_context_threshold,omitempty"` + // LongContext holds the rates for requests whose prompt is larger than LongContextThreshold. + LongContext *modelPricing `json:"long_context,omitempty"` +} + +// cost returns the price in USD of the given usage at these rates. +func (p modelPricing) cost(u *tokenUsage) float64 { + return (float64(u.InputTokens)*p.Input + + float64(u.OutputTokens)*p.Output + + float64(u.CacheCreation5mTokens)*p.CacheWrite + + float64(u.CacheCreation1hTokens)*p.CacheWrite1h + + float64(u.CacheReadInputTokens)*p.CacheRead) / 1_000_000 } // pricingFile is the schema of the embedded pricing.json data file. @@ -61,6 +75,12 @@ func init() { panic("claude trace: embedded pricing.json has no models") } + for model, p := range f.Models { + if (p.LongContext != nil) != (p.LongContextThreshold > 0) { + panic(fmt.Sprintf("claude trace: pricing.json model %q must set both long_context and long_context_threshold, or neither", model)) + } + } + def, ok := f.Models[f.DefaultModel] if !ok { panic(fmt.Sprintf("claude trace: default_model %q missing from pricing.json models", f.DefaultModel)) diff --git a/app/cli/internal/trace/claude/pricing.json b/app/cli/internal/trace/claude/pricing.json index 60dac9b80..1f149f887 100644 --- a/app/cli/internal/trace/claude/pricing.json +++ b/app/cli/internal/trace/claude/pricing.json @@ -1,9 +1,23 @@ { - "version": "2026-10-02", + "version": "2026-11-11", "source": "https://platform.claude.com/docs/en/about-claude/pricing", - "notes": "cache_write = 5-minute tier (~1.25x base input). cache_write_1h = 1-hour tier (~2x base input). cache_read = ~0.1x base input (0.025x on claude-fable-5-1 / claude-mythos-5-1, 0.05x on claude-opus-5-5). Multipliers are approximate; the published rates at the source URL are authoritative and may differ due to rounding (e.g. claude-3-haiku-20240307 cache_write is $0.30, not 0.25 * 1.25 = $0.3125).", + "notes": "cache_write = 5-minute tier (~1.25x base input). cache_write_1h = 1-hour tier (~2x base input). cache_read = ~0.1x base input (0.025x on claude-fable-5-2 / claude-mythos-5-2 / claude-fable-5-1 / claude-mythos-5-1, 0.05x on claude-opus-5-5 / claude-sonnet-5-5). long_context = rates for a request whose prompt (input + cache reads + cache writes) is larger than long_context_threshold tokens; each request is priced on its own prompt size (claude-haiku-5-5 above 100,000 tokens). Multipliers are approximate; the published rates at the source URL are authoritative and may differ due to rounding (e.g. claude-3-haiku-20240307 cache_write is $0.30, not 0.25 * 1.25 = $0.3125).", "default_model": "claude-sonnet-4-6", "models": { + "claude-fable-5-2": { + "input": 8.00, + "output": 40.00, + "cache_write": 10.00, + "cache_write_1h": 16.00, + "cache_read": 0.20 + }, + "claude-mythos-5-2": { + "input": 8.00, + "output": 40.00, + "cache_write": 10.00, + "cache_write_1h": 16.00, + "cache_read": 0.20 + }, "claude-fable-5-1": { "input": 10.00, "output": 50.00, @@ -72,7 +86,7 @@ "output": 10.00, "cache_write": 2.50, "cache_write_1h": 4.00, - "cache_read": 0.20 + "cache_read": 0.10 }, "claude-sonnet-5": { "input": 2.00, @@ -88,6 +102,21 @@ "cache_write_1h": 6.00, "cache_read": 0.30 }, + "claude-haiku-5-5": { + "input": 0.10, + "output": 0.50, + "cache_write": 0.125, + "cache_write_1h": 0.20, + "cache_read": 0.01, + "long_context_threshold": 100000, + "long_context": { + "input": 0.50, + "output": 2.50, + "cache_write": 0.625, + "cache_write_1h": 1.00, + "cache_read": 0.05 + } + }, "claude-haiku-4-5-20251001": { "input": 1.00, "output": 5.00, diff --git a/app/cli/internal/trace/claude/pricing_test.go b/app/cli/internal/trace/claude/pricing_test.go index 160b8d5ed..7085c2751 100644 --- a/app/cli/internal/trace/claude/pricing_test.go +++ b/app/cli/internal/trace/claude/pricing_test.go @@ -56,17 +56,33 @@ func TestPricingModelRates(t *testing.T) { model string want modelPricing }{ + {"claude-fable-5-2", modelPricing{Input: 8.00, Output: 40.00, CacheWrite: 10.00, CacheWrite1h: 16.00, CacheRead: 0.20}}, + {"claude-mythos-5-2", modelPricing{Input: 8.00, Output: 40.00, CacheWrite: 10.00, CacheWrite1h: 16.00, CacheRead: 0.20}}, {"claude-fable-5-1", modelPricing{Input: 10.00, Output: 50.00, CacheWrite: 12.50, CacheWrite1h: 20.00, CacheRead: 0.25}}, {"claude-mythos-5-1", modelPricing{Input: 10.00, Output: 50.00, CacheWrite: 12.50, CacheWrite1h: 20.00, CacheRead: 0.25}}, {"claude-opus-5-5", modelPricing{Input: 4.00, Output: 20.00, CacheWrite: 5.00, CacheWrite1h: 8.00, CacheRead: 0.20}}, - {"claude-sonnet-5-5", modelPricing{Input: 2.00, Output: 10.00, CacheWrite: 2.50, CacheWrite1h: 4.00, CacheRead: 0.20}}, + {"claude-sonnet-5-5", modelPricing{Input: 2.00, Output: 10.00, CacheWrite: 2.50, CacheWrite1h: 4.00, CacheRead: 0.10}}, + {"claude-haiku-5-5", modelPricing{Input: 0.10, Output: 0.50, CacheWrite: 0.125, CacheWrite1h: 0.20, CacheRead: 0.01}}, } for _, tc := range testCases { t.Run(tc.model, func(t *testing.T) { got, ok := pricing[tc.model] require.True(t, ok, "expected %s in embedded pricing table", tc.model) + got.LongContext, got.LongContextThreshold = nil, 0 assert.Equal(t, tc.want, got) }) } } + +func TestPricingLongContextRates(t *testing.T) { + haiku, ok := pricing[haiku55] + require.True(t, ok) + assert.Equal(t, 100_000, haiku.LongContextThreshold) + require.NotNil(t, haiku.LongContext) + assert.Equal(t, modelPricing{Input: 0.50, Output: 2.50, CacheWrite: 0.625, CacheWrite1h: 1.00, CacheRead: 0.05}, *haiku.LongContext) + + sonnet, ok := pricing["claude-sonnet-5-5"] + require.True(t, ok) + assert.Nil(t, sonnet.LongContext) +} diff --git a/app/cli/internal/trace/claude/types.go b/app/cli/internal/trace/claude/types.go index 5f1f8ec96..8bc0a77b4 100644 --- a/app/cli/internal/trace/claude/types.go +++ b/app/cli/internal/trace/claude/types.go @@ -26,6 +26,29 @@ type tokenUsage struct { // CacheCreation1hTokens is the subset of CacheCreationInputTokens written to the 1-hour tier. CacheCreation1hTokens int CacheReadInputTokens int + // LongContext is the subset of the counters above that comes from requests priced at the + // model's long-context rates. Nil when no request passed the model's threshold. + LongContext *tokenUsage +} + +// merge adds the counters of o, including its long-context subset, to t. +func (t *tokenUsage) merge(o *tokenUsage) { + t.InputTokens += o.InputTokens + t.OutputTokens += o.OutputTokens + t.CacheCreationInputTokens += o.CacheCreationInputTokens + t.CacheCreation5mTokens += o.CacheCreation5mTokens + t.CacheCreation1hTokens += o.CacheCreation1hTokens + t.CacheReadInputTokens += o.CacheReadInputTokens + if o.LongContext != nil { + t.longContext().merge(o.LongContext) + } +} + +func (t *tokenUsage) longContext() *tokenUsage { + if t.LongContext == nil { + t.LongContext = &tokenUsage{} + } + return t.LongContext } func (t *tokenUsage) add(u *jsonlUsage) { @@ -67,6 +90,11 @@ type jsonlUsage struct { CacheCreation *jsonlCacheCreationInfo `json:"cache_creation,omitempty"` } +// promptTokens is the size of the request prompt: base input plus cache reads and writes. +func (u *jsonlUsage) promptTokens() int { + return u.InputTokens + u.CacheReadInputTokens + u.CacheCreationInputTokens +} + // jsonlCacheCreationInfo mirrors the nested usage.cache_creation object emitted by recent // Claude API responses, which splits cache writes by tier (5-minute vs 1-hour). type jsonlCacheCreationInfo struct { diff --git a/app/cli/internal/trace/codex/pricing.json b/app/cli/internal/trace/codex/pricing.json index 2a9dd638f..f68330217 100644 --- a/app/cli/internal/trace/codex/pricing.json +++ b/app/cli/internal/trace/codex/pricing.json @@ -1,5 +1,5 @@ { - "version": "2026-10-21", + "version": "2026-11-11", "source": "https://developers.openai.com/api/docs/pricing", "notes": "USD per million tokens, standard short-context API rates. Estimates exclude priority tiers, long-context surcharges, and subscription or third-party billing. Unknown models use the default with a warning.", "default_model": "gpt-5-mini", @@ -21,6 +21,12 @@ "output": 10.0, "cache_write": 2.5 }, + "gpt-6.1-terra": { + "input": 1.0, + "cached": 0.05, + "output": 5.0, + "cache_write": 1.25 + }, "gpt-6-luna": { "input": 0.1, "cached": 0.01, diff --git a/app/cli/internal/trace/codex/pricing_test.go b/app/cli/internal/trace/codex/pricing_test.go index e6c1a2204..eaa83536c 100644 --- a/app/cli/internal/trace/codex/pricing_test.go +++ b/app/cli/internal/trace/codex/pricing_test.go @@ -28,6 +28,7 @@ func TestNormalizedUsagePricing(t *testing.T) { }{ {"separate cache write price", "gpt-6.1-sol", 0.00103}, {"cache writes at input price", "gpt-5-mini", 0.00017}, + {"gpt-6.1-terra", "gpt-6.1-terra", 0.000515}, {"provider prefix", "openai/gpt-6.1-sol", 0.00103}, {"date pinned model", "gpt-6.1-sol-2026-10-21", 0.00103}, } { diff --git a/app/cli/internal/trace/copilot/parse_test.go b/app/cli/internal/trace/copilot/parse_test.go index 9bfdb647b..c46715f39 100644 --- a/app/cli/internal/trace/copilot/parse_test.go +++ b/app/cli/internal/trace/copilot/parse_test.go @@ -238,6 +238,10 @@ func TestEstimateCost(t *testing.T) { {model: "claude-sonnet-4.6", want: 3 + 15 + 0.3}, {model: "anthropic/claude-sonnet-4.6", want: 3 + 15 + 0.3}, {model: testModel, want: 2.5 + 15 + 0.25}, + {model: "claude-fable-5.2", want: 8 + 40 + 0.2}, + {model: "claude-sonnet-5.5", want: 2 + 10 + 0.1}, + {model: "claude-haiku-5.5", want: 0.1 + 0.5 + 0.01}, + {model: "gpt-6.1-terra", want: 1 + 5 + 0.05}, {model: "some-new-model", want: 3 + 15 + 0.3, wantWarning: true}, } for _, tc := range cases { diff --git a/app/cli/internal/trace/copilot/pricing.json b/app/cli/internal/trace/copilot/pricing.json index 7360acca2..bf0d90505 100644 --- a/app/cli/internal/trace/copilot/pricing.json +++ b/app/cli/internal/trace/copilot/pricing.json @@ -4,6 +4,18 @@ "notes": "USD per million tokens. Copilot bills by subscription and premium requests, so this is an API-price estimate, not the Copilot bill. Model names are normalized: lowercase, no vendor prefix, dots replaced with dashes. Unknown models use the default with a warning.", "default_model": "claude-sonnet-4-6", "models": { + "claude-fable-5-2": { + "input": 8.0, + "cached": 0.2, + "output": 40.0, + "cache_write": 10.0 + }, + "claude-haiku-5-5": { + "input": 0.1, + "cached": 0.01, + "output": 0.5, + "cache_write": 0.125 + }, "claude-haiku-4-5": { "input": 1.0, "cached": 0.1, @@ -84,7 +96,7 @@ }, "claude-sonnet-5-5": { "input": 2.0, - "cached": 0.2, + "cached": 0.1, "output": 10.0, "cache_write": 2.5 }, @@ -187,6 +199,12 @@ "output": 10.0, "cache_write": 2.5 }, + "gpt-6-1-terra": { + "input": 1.0, + "cached": 0.05, + "output": 5.0, + "cache_write": 1.25 + }, "gpt-6-astra": { "input": 10.0, "cached": 1.0,