oficina / equipo Public

Clone
git clone https://amadan.net/oficina/equipo

Plain git — no account needed to clone.

Download

Download this file

1// Package metered keeps the soft budget of metered connections
2// (connectors spec 2026-11-12, §2.3, slice S1): versioned price tables,
3// usage normalised into billable components and priced in integer
4// micro-units, the budget ledger, the one durable slot per connection,
5// its restart recovery, and the pause when spend is unknown. It decides
6// nothing about which connection runs a step; it admits an attempt to a
7// metered connection or says why it is held.
8package metered
9
10import (
11 "math"
12 "math/bits"
13
14 "amadan.net/oficina/equipo/internal/wire"
15)
16
17// Components are an attempt's usage as billable parts (§2.3). Reasoning
18// is apart from Output only where the price table says the provider
19// bills it separately; otherwise it is part of Output and not added.
20type Components struct {
21 UncachedInput, CachedInput, CacheWrite, Output, Reasoning int64
22}
23
24// Price is one model's price per component, in micro-units of the
25// table's currency per million tokens. A nil component is one the table
26// cannot price: any use of it makes the attempt's spend unknown.
27// InputIncludesCached: the harness counts cached input inside input (as
28// Codex does), so the uncached part is input less cached.
29type Price struct {
30 UncachedInput, CachedInput, CacheWrite, Output, Reasoning *int64
31 ReasoningSeparate bool
32 InputIncludesCached bool
33}
34
35// Table is a provider's price table at one version (§2.3: versioned per
36// connection; a connection keeps the version it was made with).
37type Table struct {
38 Provider, Version, Currency string
39 Models map[string]Price
40}
41
42func micros(dollars float64) *int64 {
43 v := int64(math.Round(dollars * 1_000_000))
44 return &v
45}
46
47// tensorx prices are TensorX's published per-million-token prices read
48// on 2026-11-12 (tensorx.ai/pricing, matching models.dev's TensorX
49// files). Cache writes and separate reasoning billing are not stated, so
50// cache writes are unpriced (any makes spend unknown) and reasoning is
51// part of output.
52func tensorx(in, cached, out float64) Price {
53 return Price{UncachedInput: micros(in), CachedInput: micros(cached), Output: micros(out)}
54}
55
56// goPrice is one OpenCode Go model's price per million tokens: input,
57// cached read, and cache write where Go states one (negative: none, so
58// any cache write makes spend unknown). Reasoning is part of output
59// ("output includes reasoning", Go's inference API).
60func goPrice(in, cached, write, out float64) Price {
61 p := Price{UncachedInput: micros(in), CachedInput: micros(cached), Output: micros(out)}
62 if write >= 0 {
63 p.CacheWrite = micros(write)
64 }
65 return p
66}
67
68// opencodeGo prices are the OpenCode Go page's per-model prices
69// (opencode.ai/docs/console/go/, "Token prices", read 2026-11-12), the
70// same on Go, Go Plus and Go Teams. Only models Go serves on chat
71// completions are here: the OpenCode harness speaks chat completions to
72// Go (spec §4.2), so the Responses, Messages and Gemini models are left
73// out, as are the free models. DeepSeek's two models are priced at
74// their peak price: Equipo cannot tell peak from off-peak, and the
75// higher figure keeps the soft budget on the safe side. Within its
76// plan's limits Go reports a cost of 0 (it counts against the plan's
77// allowance instead), so these list prices are what the budget charges.
78func opencodeGo() map[string]Price {
79 return map[string]Price{
80 "glm-5.3-flash": goPrice(0.15, 0.03, -1, 0.50),
81 "glm-5.3": goPrice(1.40, 0.26, -1, 4.40),
82 "glm-5.2": goPrice(1.40, 0.26, -1, 4.40),
83 "kimi-k3.1-pro": goPrice(2.00, 0.20, 2.00, 10.00),
84 "kimi-k3": goPrice(3.00, 0.30, 3.00, 15.00),
85 "kimi-k2.7-code": goPrice(0.95, 0.19, -1, 4.00),
86 "longcat-2.0": goPrice(0.30, 0.006, -1, 1.20),
87 "deepseek-v4.1-flash": goPrice(0.30, 0.006, -1, 1.20),
88 "deepseek-v4-pro": goPrice(1.32, 0.044, -1, 3.96),
89 "mimo-v2.6-flash": goPrice(0.14, 0.0028, -1, 0.28),
90 "mimo-v2.5": goPrice(0.14, 0.0028, -1, 0.28),
91 "mimo-v2.6-pro": goPrice(0.435, 0.003625, -1, 0.87),
92 "mimo-v2.5-pro": goPrice(0.435, 0.003625, -1, 0.87),
93 "minimax-m3.1-flash": goPrice(0.15, 0.015, -1, 0.60),
94 "hy4": goPrice(0.834, 0.042, -1, 2.501),
95 "hy4-preview": goPrice(0.834, 0.042, -1, 2.501),
96 "hy3": goPrice(0.14, 0.035, -1, 0.58),
97 "step-5-preview": goPrice(1.00, 0.10, -1, 2.70),
98 }
99}
100
101// Tables are every price table this runner knows, by version.
102var Tables = map[string]Table{
103 "tensorx-2026-11-12": {Provider: wire.ProviderTensorX, Version: "tensorx-2026-11-12", Currency: "USD", Models: map[string]Price{
104 "z-ai/glm-5.3": tensorx(1.75, 0.44, 4.50),
105 "z-ai/glm-5.3-flash": tensorx(0.20, 0.05, 0.50),
106 "z-ai/glm-5.2": tensorx(1.50, 0.38, 4.50),
107 "moonshotai/kimi-k3": tensorx(3.00, 0.75, 15.00),
108 "moonshotai/kimi-k3.1-pro": tensorx(3.00, 0.75, 15.00),
109 "deepseek/deepseek-v4.1-flash": tensorx(0.50, 0.13, 1.50),
110 "deepseek/deepseek-v4-pro-0813": tensorx(2.00, 0.50, 4.00),
111 "deepseek/deepseek-v4-flash-0731": tensorx(0.25, 0.06, 0.30),
112 "minimax/minimax-m3.1-flash": tensorx(0.30, 0.08, 1.20),
113 "qwen/qwen3.8-27b": tensorx(0.40, 0.10, 2.40),
114 }},
115 "opencode-go-2026-11-12": {Provider: wire.ProviderOpenCodeGo, Version: "opencode-go-2026-11-12", Currency: "USD", Models: opencodeGo()},
116}
117
118// Current is the price version a new connection to a provider takes.
119var Current = map[string]string{wire.ProviderTensorX: "tensorx-2026-11-12", wire.ProviderOpenCodeGo: "opencode-go-2026-11-12"}
120
121// TableFor is the table a connection's version names (false: none).
122func TableFor(version string) (Table, bool) {
123 t, ok := Tables[version]
124 return t, ok
125}
126
127// Priced reports whether the table prices model at all.
128func (t Table) Priced(model string) bool {
129 _, ok := t.Models[model]
130 return ok
131}
132
133// Normalise turns a usage row into components under price p. It is
134// false when the row cannot be priced: not complete, input or output not
135// reported, or a figure negative. A cached or cache-write figure the
136// harness did not report counts as none of that kind.
137func Normalise(u wire.Usage, p Price) (Components, bool) {
138 if !u.Complete || u.InputTokens == nil || u.OutputTokens == nil {
139 return Components{}, false
140 }
141 figure := func(v *int64) int64 {
142 if v == nil {
143 return 0
144 }
145 return *v
146 }
147 c := Components{UncachedInput: *u.InputTokens, CachedInput: figure(u.CachedInputTokens), CacheWrite: figure(u.CacheWriteTokens), Output: *u.OutputTokens}
148 if p.ReasoningSeparate {
149 c.Reasoning = figure(u.ReasoningOutputTokens)
150 }
151 if p.InputIncludesCached {
152 c.UncachedInput -= c.CachedInput
153 }
154 if c.UncachedInput < 0 || c.CachedInput < 0 || c.CacheWrite < 0 || c.Output < 0 || c.Reasoning < 0 {
155 return Components{}, false
156 }
157 return c, true
158}
159
160// Cost prices components in micro-units, each part rounded up. It is
161// false when a used component has no price or the sum overflows.
162func Cost(c Components, p Price) (int64, bool) {
163 var total int64
164 for _, part := range []struct {
165 tokens int64
166 price *int64
167 }{{c.UncachedInput, p.UncachedInput}, {c.CachedInput, p.CachedInput}, {c.CacheWrite, p.CacheWrite}, {c.Output, p.Output}, {c.Reasoning, p.Reasoning}} {
168 if part.tokens == 0 {
169 continue
170 }
171 if part.price == nil || *part.price < 0 {
172 return 0, false
173 }
174 v, ok := perMillion(part.tokens, *part.price)
175 if !ok || total > math.MaxInt64-v {
176 return 0, false
177 }
178 total += v
179 }
180 return total, true
181}
182
183// perMillion is ceil(tokens * price / 1e6) without overflow.
184func perMillion(tokens, price int64) (int64, bool) {
185 hi, lo := bits.Mul64(uint64(tokens), uint64(price))
186 if hi >= 1_000_000 {
187 return 0, false
188 }
189 q, r := bits.Div64(hi, lo, 1_000_000)
190 if r > 0 {
191 q++
192 }
193 if q > math.MaxInt64 {
194 return 0, false
195 }
196 return int64(q), true
197}
198
199// Price prices one usage row against the table: the row with its cost,
200// currency and version set, and whether the cost is known. Where the
201// harness reported its own cost, the larger figure is charged and the
202// reported one kept beside it. An unpriced model, an unpriceable
203// component or an incomplete row leaves the cost null (unknown).
204func (t Table) Price(u wire.Usage) (wire.Usage, bool) {
205 u.Currency, u.PriceVersion, u.CostMicros = t.Currency, t.Version, nil
206 p, ok := t.Models[u.Model]
207 if !ok {
208 return u, false
209 }
210 c, ok := Normalise(u, p)
211 if !ok {
212 return u, false
213 }
214 cost, ok := Cost(c, p)
215 if !ok {
216 return u, false
217 }
218 if u.CostReportedMicros != nil && *u.CostReportedMicros > cost {
219 cost = *u.CostReportedMicros
220 }
221 u.CostMicros = &cost
222 return u, true
223}
224