// Package metered keeps the soft budget of metered connections // (connectors spec 2026-11-12, §2.3, slice S1): versioned price tables, // usage normalised into billable components and priced in integer // micro-units, the budget ledger, the one durable slot per connection, // its restart recovery, and the pause when spend is unknown. It decides // nothing about which connection runs a step; it admits an attempt to a // metered connection or says why it is held. package metered import ( "math" "math/bits" "amadan.net/oficina/equipo/internal/wire" ) // Components are an attempt's usage as billable parts (§2.3). Reasoning // is apart from Output only where the price table says the provider // bills it separately; otherwise it is part of Output and not added. type Components struct { UncachedInput, CachedInput, CacheWrite, Output, Reasoning int64 } // Price is one model's price per component, in micro-units of the // table's currency per million tokens. A nil component is one the table // cannot price: any use of it makes the attempt's spend unknown. // InputIncludesCached: the harness counts cached input inside input (as // Codex does), so the uncached part is input less cached. type Price struct { UncachedInput, CachedInput, CacheWrite, Output, Reasoning *int64 ReasoningSeparate bool InputIncludesCached bool } // Table is a provider's price table at one version (§2.3: versioned per // connection; a connection keeps the version it was made with). type Table struct { Provider, Version, Currency string Models map[string]Price } func micros(dollars float64) *int64 { v := int64(math.Round(dollars * 1_000_000)) return &v } // tensorx prices are TensorX's published per-million-token prices read // on 2026-11-12 (tensorx.ai/pricing, matching models.dev's TensorX // files). Cache writes and separate reasoning billing are not stated, so // cache writes are unpriced (any makes spend unknown) and reasoning is // part of output. func tensorx(in, cached, out float64) Price { return Price{UncachedInput: micros(in), CachedInput: micros(cached), Output: micros(out)} } // goPrice is one OpenCode Go model's price per million tokens: input, // cached read, and cache write where Go states one (negative: none, so // any cache write makes spend unknown). Reasoning is part of output // ("output includes reasoning", Go's inference API). func goPrice(in, cached, write, out float64) Price { p := Price{UncachedInput: micros(in), CachedInput: micros(cached), Output: micros(out)} if write >= 0 { p.CacheWrite = micros(write) } return p } // opencodeGo prices are the OpenCode Go page's per-model prices // (opencode.ai/docs/console/go/, "Token prices", read 2026-11-12), the // same on Go, Go Plus and Go Teams. Only models Go serves on chat // completions are here: the OpenCode harness speaks chat completions to // Go (spec §4.2), so the Responses, Messages and Gemini models are left // out, as are the free models. DeepSeek's two models are priced at // their peak price: Equipo cannot tell peak from off-peak, and the // higher figure keeps the soft budget on the safe side. Within its // plan's limits Go reports a cost of 0 (it counts against the plan's // allowance instead), so these list prices are what the budget charges. func opencodeGo() map[string]Price { return map[string]Price{ "glm-5.3-flash": goPrice(0.15, 0.03, -1, 0.50), "glm-5.3": goPrice(1.40, 0.26, -1, 4.40), "glm-5.2": goPrice(1.40, 0.26, -1, 4.40), "kimi-k3.1-pro": goPrice(2.00, 0.20, 2.00, 10.00), "kimi-k3": goPrice(3.00, 0.30, 3.00, 15.00), "kimi-k2.7-code": goPrice(0.95, 0.19, -1, 4.00), "longcat-2.0": goPrice(0.30, 0.006, -1, 1.20), "deepseek-v4.1-flash": goPrice(0.30, 0.006, -1, 1.20), "deepseek-v4-pro": goPrice(1.32, 0.044, -1, 3.96), "mimo-v2.6-flash": goPrice(0.14, 0.0028, -1, 0.28), "mimo-v2.5": goPrice(0.14, 0.0028, -1, 0.28), "mimo-v2.6-pro": goPrice(0.435, 0.003625, -1, 0.87), "mimo-v2.5-pro": goPrice(0.435, 0.003625, -1, 0.87), "minimax-m3.1-flash": goPrice(0.15, 0.015, -1, 0.60), "hy4": goPrice(0.834, 0.042, -1, 2.501), "hy4-preview": goPrice(0.834, 0.042, -1, 2.501), "hy3": goPrice(0.14, 0.035, -1, 0.58), "step-5-preview": goPrice(1.00, 0.10, -1, 2.70), } } // Tables are every price table this runner knows, by version. var Tables = map[string]Table{ "tensorx-2026-11-12": {Provider: wire.ProviderTensorX, Version: "tensorx-2026-11-12", Currency: "USD", Models: map[string]Price{ "z-ai/glm-5.3": tensorx(1.75, 0.44, 4.50), "z-ai/glm-5.3-flash": tensorx(0.20, 0.05, 0.50), "z-ai/glm-5.2": tensorx(1.50, 0.38, 4.50), "moonshotai/kimi-k3": tensorx(3.00, 0.75, 15.00), "moonshotai/kimi-k3.1-pro": tensorx(3.00, 0.75, 15.00), "deepseek/deepseek-v4.1-flash": tensorx(0.50, 0.13, 1.50), "deepseek/deepseek-v4-pro-0813": tensorx(2.00, 0.50, 4.00), "deepseek/deepseek-v4-flash-0731": tensorx(0.25, 0.06, 0.30), "minimax/minimax-m3.1-flash": tensorx(0.30, 0.08, 1.20), "qwen/qwen3.8-27b": tensorx(0.40, 0.10, 2.40), }}, "opencode-go-2026-11-12": {Provider: wire.ProviderOpenCodeGo, Version: "opencode-go-2026-11-12", Currency: "USD", Models: opencodeGo()}, } // Current is the price version a new connection to a provider takes. var Current = map[string]string{wire.ProviderTensorX: "tensorx-2026-11-12", wire.ProviderOpenCodeGo: "opencode-go-2026-11-12"} // TableFor is the table a connection's version names (false: none). func TableFor(version string) (Table, bool) { t, ok := Tables[version] return t, ok } // Priced reports whether the table prices model at all. func (t Table) Priced(model string) bool { _, ok := t.Models[model] return ok } // Normalise turns a usage row into components under price p. It is // false when the row cannot be priced: not complete, input or output not // reported, or a figure negative. A cached or cache-write figure the // harness did not report counts as none of that kind. func Normalise(u wire.Usage, p Price) (Components, bool) { if !u.Complete || u.InputTokens == nil || u.OutputTokens == nil { return Components{}, false } figure := func(v *int64) int64 { if v == nil { return 0 } return *v } c := Components{UncachedInput: *u.InputTokens, CachedInput: figure(u.CachedInputTokens), CacheWrite: figure(u.CacheWriteTokens), Output: *u.OutputTokens} if p.ReasoningSeparate { c.Reasoning = figure(u.ReasoningOutputTokens) } if p.InputIncludesCached { c.UncachedInput -= c.CachedInput } if c.UncachedInput < 0 || c.CachedInput < 0 || c.CacheWrite < 0 || c.Output < 0 || c.Reasoning < 0 { return Components{}, false } return c, true } // Cost prices components in micro-units, each part rounded up. It is // false when a used component has no price or the sum overflows. func Cost(c Components, p Price) (int64, bool) { var total int64 for _, part := range []struct { tokens int64 price *int64 }{{c.UncachedInput, p.UncachedInput}, {c.CachedInput, p.CachedInput}, {c.CacheWrite, p.CacheWrite}, {c.Output, p.Output}, {c.Reasoning, p.Reasoning}} { if part.tokens == 0 { continue } if part.price == nil || *part.price < 0 { return 0, false } v, ok := perMillion(part.tokens, *part.price) if !ok || total > math.MaxInt64-v { return 0, false } total += v } return total, true } // perMillion is ceil(tokens * price / 1e6) without overflow. func perMillion(tokens, price int64) (int64, bool) { hi, lo := bits.Mul64(uint64(tokens), uint64(price)) if hi >= 1_000_000 { return 0, false } q, r := bits.Div64(hi, lo, 1_000_000) if r > 0 { q++ } if q > math.MaxInt64 { return 0, false } return int64(q), true } // Price prices one usage row against the table: the row with its cost, // currency and version set, and whether the cost is known. Where the // harness reported its own cost, the larger figure is charged and the // reported one kept beside it. An unpriced model, an unpriceable // component or an incomplete row leaves the cost null (unknown). func (t Table) Price(u wire.Usage) (wire.Usage, bool) { u.Currency, u.PriceVersion, u.CostMicros = t.Currency, t.Version, nil p, ok := t.Models[u.Model] if !ok { return u, false } c, ok := Normalise(u, p) if !ok { return u, false } cost, ok := Cost(c, p) if !ok { return u, false } if u.CostReportedMicros != nil && *u.CostReportedMicros > cost { cost = *u.CostReportedMicros } u.CostMicros = &cost return u, true }