opencode-usage-insights 0.1.0 → 0.1.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +189 -66
- package/dist/core.d.ts +38 -31
- package/dist/index.d.ts +10 -3
- package/dist/index.js +207 -257
- package/dist/index.js.map +10 -10
- package/dist/line.d.ts +20 -7
- package/dist/panel-build.d.ts +5 -1
- package/dist/panel-data.d.ts +36 -23
- package/dist/panel.d.ts +8 -0
- package/dist/pricing.d.ts +72 -0
- package/dist/recorder.d.ts +10 -17
- package/dist/sidebar.d.ts +3 -3
- package/dist/stats/aggregates.d.ts +39 -37
- package/dist/stats/db.d.ts +17 -18
- package/dist/stats/maintenance.d.ts +17 -0
- package/dist/stats/queries.d.ts +25 -30
- package/dist/text.d.ts +5 -8
- package/dist/tui.d.ts +11 -10
- package/dist/tui.js +583 -428
- package/dist/tui.js.map +16 -15
- package/package.json +2 -2
- package/dist/endpoint.d.ts +0 -46
package/README.md
CHANGED
|
@@ -5,7 +5,7 @@
|
|
|
5
5
|
[](https://github.com/hugobatista/opencode-usage-insights/actions/workflows/lint.yml)
|
|
6
6
|
[](https://www.npmjs.com/package/opencode-usage-insights)
|
|
7
7
|
|
|
8
|
-
OpenCode plugin for one question the host cannot answer: **which
|
|
8
|
+
OpenCode plugin for one question the host cannot answer: **which provider is
|
|
9
9
|
actually serving each model** — and what that costs. It shows a telemetry line
|
|
10
10
|
above the composer after every finished turn and records every assistant step
|
|
11
11
|
in a local SQLite file, read back through an interactive `/usage` panel.
|
|
@@ -18,23 +18,25 @@ written by the server-side recorder and read by the TUI.
|
|
|
18
18
|
## What it does
|
|
19
19
|
|
|
20
20
|
- **The line** (`session.composer.top`): end time, the gap since the previous
|
|
21
|
-
prompt, the
|
|
22
|
-
warnings when the prompt cache or the
|
|
23
|
-
finished work, so it stays hidden while a turn is running.
|
|
24
|
-
- **The `/usage` panel**:
|
|
25
|
-
`
|
|
21
|
+
prompt, the provider and model that ran the turn, turn and session token
|
|
22
|
+
totals, and warnings when the prompt cache or the provider identity changed.
|
|
23
|
+
It belongs to finished work, so it stays hidden while a turn is running.
|
|
24
|
+
- **The `/usage` panel**: nine keyboard-driven views — `overview`, `models`,
|
|
25
|
+
`providers`, `cache`, `periods`, `latency`, `sessions`, `turns`,
|
|
26
26
|
`db` — over five ranges (`session`, `today`, `7d`, `30d`, `all`), with
|
|
27
27
|
drill-downs, sorting, filters and state remembered between openings.
|
|
28
|
-
`/usage models 7d`, `/usage
|
|
28
|
+
`/usage models 7d`, `/usage providers 30d`, `/usage turns 7d`.
|
|
29
29
|
- **The session sidebar**: the session's own totals and its last turns, in the
|
|
30
|
-
same buckets as the line. Three lines per turn — when and through
|
|
31
|
-
prompt volumes, then `hit · tok/s · cost` —
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
`
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
30
|
+
same buckets as the line. Three lines per turn — when and through which
|
|
31
|
+
provider, the prompt volumes, then `hit · tok/s · cost` — plus the model at
|
|
32
|
+
both ends of the block and on any turn that changed it, so the history reads
|
|
33
|
+
whether it is the whole session or a window of a long one:
|
|
34
|
+
`16:54 opencode-go` / `deepseek-v4.1-flash max` /
|
|
35
|
+
`in 1.7k (427.3k) · out 1.06k` / `hit 99.7% · 1.2k/s · $0.0006`.
|
|
36
|
+
- **The record** (server entry): every assistant step with tokens, charged cost,
|
|
37
|
+
the list-price estimate when the host reported `$0`,
|
|
38
|
+
timings, finish reason, the provider and model it ran as, and its session and
|
|
39
|
+
parent ids; prompts, session lifecycle and compaction events too.
|
|
38
40
|
- **Portable data** (from a checkout): `bun run report --ndjson` exports the
|
|
39
41
|
whole database, and `--import` merges it back on another machine.
|
|
40
42
|
- **Observational by design**: the plugin never rewrites prompts, requests or
|
|
@@ -49,15 +51,12 @@ written by the server-side recorder and read by the TUI.
|
|
|
49
51
|
|
|
50
52
|
| | |
|
|
51
53
|
| --- | --- |
|
|
52
|
-
| **OS** | Linux, macOS and Windows. The database lives in the per-user state directory of each (table below).
|
|
54
|
+
| **OS** | Linux, macOS and Windows. The database lives in the per-user state directory of each (table below); `test/paths.test.ts` asserts the path rules of all three from any machine, and `bun run check:platform` exercises the data layer — paths, database, recorder, queries, panel assembly, dump round-trip, file permissions — on the OS it runs on. CI runs the whole suite (including the TUI render tests) on Linux. |
|
|
53
55
|
| **Runtime** | Bun, the runtime OpenCode ships with. SQLite comes from `bun:sqlite`: there is no native module to build and nothing to compile on install. |
|
|
54
56
|
|
|
55
57
|
## Install
|
|
56
58
|
|
|
57
|
-
|
|
58
|
-
> **Not on the npm registry yet.** Until the first release is published,
|
|
59
|
-
> `opencode plugin add` and the package-name form below cannot resolve it — use
|
|
60
|
-
> the local checkout, the last form of this section.
|
|
59
|
+
The plugin is published on npm as `opencode-usage-insights`.
|
|
61
60
|
|
|
62
61
|
```sh
|
|
63
62
|
opencode plugin add opencode-usage-insights
|
|
@@ -87,7 +86,7 @@ To pass [options](#configuration), use the object form:
|
|
|
87
86
|
```jsonc
|
|
88
87
|
{
|
|
89
88
|
"plugins": [
|
|
90
|
-
{ "package": "opencode-usage-insights", "options": { "
|
|
89
|
+
{ "package": "opencode-usage-insights", "options": { "sidebarTurns": 5 } }
|
|
91
90
|
]
|
|
92
91
|
}
|
|
93
92
|
```
|
|
@@ -115,26 +114,140 @@ turn finishes, and `/usage` (alias `/insights`) opens the panel.
|
|
|
115
114
|
## The line
|
|
116
115
|
|
|
117
116
|
```text
|
|
118
|
-
14:32:07 · Δ 3m12s ·
|
|
117
|
+
14:32:07 · Δ 3m12s · opencode-go/deepseek-v4.1-flash max · in 85.4k (1.62M), cached 1.53M, out 3.7k, cost $0.21 · sess in 2.1M (14.5M), cached 12.4M, out 88k, cost $1.90 · ⚠ cache expired (−11.7k)
|
|
119
118
|
```
|
|
120
119
|
|
|
121
120
|
| Segment | Meaning |
|
|
122
121
|
| --- | --- |
|
|
123
122
|
| `14:32:07` | When the response ended (`completed`, else `streamed`, else `created`). |
|
|
124
123
|
| `Δ 3m12s` | Time since the previous prompt of the same session. |
|
|
125
|
-
| `
|
|
126
|
-
| `in 85.4k (1.62M), cached 1.53M, out 3.7k` | `in` is the uncached part of the prompt (fresh input + cache writes), the parenthesised value the whole prompt — shown only when the two differ — `cached` its cached part,
|
|
127
|
-
| `
|
|
124
|
+
| `opencode-go/deepseek-v4.1-flash max` | The identity of the turn: the provider the model ran through, as OpenCode is configured, and the model (the variant as a trailing word; `fields.model`/`fields.variant` hide either). Yellow with `changed` when the provider differs from the previous prompt, `model changed` when only the model does. |
|
|
125
|
+
| `in 85.4k (1.62M), cached 1.53M, out 3.7k, cost $0.21` | `in` is the uncached part of the prompt (fresh input + cache writes), the parenthesised value the whole prompt — shown only when the two differ — `cached` its cached part, `out` everything generated (visible output + reasoning), and `cost` what the turn was billed. With several steps the segment reads `3 steps · …` and reports the step the turn ended with (`latest`) for the tokens, while the cost is always the turn's own — every step is paid for; `turnTotals: "sum"` adds every step's tokens instead. |
|
|
126
|
+
| `cost $0.21` | Money, not tokens: a turn sums every step's cost whatever the token view shows. A cost of zero is left out, so a free model reads as free. `fields.cost: false` hides it. |
|
|
127
|
+
| `cost ≈ $0.21` | A **list-price estimate**, not money charged. When the host reports `cost: 0` for a paid model (OpenCode 2.2.0+ with an OpenCode Go subscription), the plugin prices the step from the host's model list. A turn mixing covered and paid steps keeps both parts visible: `cost $0.12 + ≈ $3.40`. `pricing: "off"` hides estimates everywhere; `fields.cost: false` hides both. |
|
|
128
|
+
| `sess in 2.1M (14.5M), cached 12.4M, out 88k, cost $1.90` | The session totals: tokens and charged money come from the host, and the list estimate comes from the steps the plugin recorded. A Go session reads `cost ≈ $3.40`; a session mixing charged and covered steps keeps both parts, `cost $0.12 + ≈ $3.40`. |
|
|
128
129
|
| `⚠ cache expired (−11.7k)` | Yellow. The prompt cache read fell by that many tokens against a comparable previous step; `×N` when several steps in the turn lost cache. |
|
|
129
130
|
|
|
130
131
|
The turn segment carries no label by default — the numbers are the turn's, and
|
|
131
132
|
`sess` marks the session's; `labels.turn` (e.g. `"turn"`) brings a prefix back.
|
|
132
133
|
|
|
134
|
+
`≈` is an estimate at the host's list price, never a charge. It appears when
|
|
135
|
+
OpenCode reports a step at `$0` — subscription-covered Go requests, or a provider
|
|
136
|
+
that stopped reporting a cost — and the host's model list still has a price. A
|
|
137
|
+
genuinely free model has no price and therefore no estimate; it is recorded as
|
|
138
|
+
known free rather than as an unknown price. The estimate is stored per step, so
|
|
139
|
+
the line's `sess` segment, the panel, the sidebar, the NDJSON export and the CLI
|
|
140
|
+
report all show the same number. The `db` view lists how many `$0` steps still
|
|
141
|
+
lack one; `e` backfills them using today's model list.
|
|
142
|
+
|
|
133
143
|
A turn is every consecutive assistant step after a prompt. Compaction, shell,
|
|
134
144
|
`idle` and info messages do not split a turn; `user`, `synthetic` and `system`
|
|
135
145
|
messages do. From a checkout, `bun run demo` prints one example per case,
|
|
136
146
|
including the streaming one and both turn-total views.
|
|
137
147
|
|
|
148
|
+
## How costs are calculated
|
|
149
|
+
|
|
150
|
+
**Charged money is never recalculated by the plugin.** `steps.cost` is what the
|
|
151
|
+
host reports for the step (possibly `0` for an OpenCode Go subscription). When
|
|
152
|
+
that number is `0` or missing and the model has a price in the host's model
|
|
153
|
+
list, the plugin computes a **list-price estimate** and stores it in
|
|
154
|
+
`steps.list_cost`. Prices are USD per 1,000,000 tokens.
|
|
155
|
+
|
|
156
|
+
The price source is the model list exposed by the OpenCode process that is
|
|
157
|
+
currently serving the session, already resolved by the host (provider config,
|
|
158
|
+
overrides, Zen/Go pricing):
|
|
159
|
+
|
|
160
|
+
| Surface | Where the price comes from |
|
|
161
|
+
| --- | --- |
|
|
162
|
+
| Server recorder | `ctx.model.list()` → `ModelInfo.cost` |
|
|
163
|
+
| Live turn / backfill | `ctx.data.location.model.list(ctx.location)` → `ModelInfo.cost` |
|
|
164
|
+
| Panel, sidebar, NDJSON, CLI report | stored `steps.list_cost` |
|
|
165
|
+
| `sess` segment | `SUM(list_cost)` from the plugin database |
|
|
166
|
+
|
|
167
|
+
The plugin does **not** read `~/.cache/opencode/models.json`, does **not** use
|
|
168
|
+
`opencode models` for prices (it lists model IDs only), and makes no network
|
|
169
|
+
call of its own. Each `ModelInfo` carries the prices as
|
|
170
|
+
`cost: Array<{ input, output, cache: { read, write }, tier? }>` in USD per
|
|
171
|
+
1,000,000 tokens.
|
|
172
|
+
|
|
173
|
+
The estimate uses the host's own fallback formula:
|
|
174
|
+
|
|
175
|
+
```text
|
|
176
|
+
input fresh (uncached) prompt tokens
|
|
177
|
+
cache.read prompt tokens read from the provider cache
|
|
178
|
+
cache.write prompt tokens written to the provider cache
|
|
179
|
+
output visible output tokens
|
|
180
|
+
reasoning reasoning tokens
|
|
181
|
+
|
|
182
|
+
prompt = input + cache.read + cache.write
|
|
183
|
+
cost = (input * inputPrice
|
|
184
|
+
+ (output + reasoning) * outputPrice
|
|
185
|
+
+ cache.read * cacheReadPrice
|
|
186
|
+
+ cache.write * cacheWritePrice) / 1_000_000
|
|
187
|
+
```
|
|
188
|
+
|
|
189
|
+
Reasoning is billed at the output price. For models with context tiers, the
|
|
190
|
+
tier applies only when the whole prompt is **strictly greater** than the tier
|
|
191
|
+
size; at exactly the threshold the untiered price applies. An unknown model or
|
|
192
|
+
no applicable price means no estimate; an all-zero price means the model is
|
|
193
|
+
known to be free.
|
|
194
|
+
|
|
195
|
+
The buckets behind the line are the same ones the estimate uses, but folded for
|
|
196
|
+
display:
|
|
197
|
+
|
|
198
|
+
```text
|
|
199
|
+
in = input + cache.write (the uncached part of the prompt)
|
|
200
|
+
cached = cache.read
|
|
201
|
+
out = output + reasoning
|
|
202
|
+
prompt = in + cached
|
|
203
|
+
```
|
|
204
|
+
|
|
205
|
+
`list_cost` has three meanings: `NULL` means the step was charged or its price
|
|
206
|
+
is unknown; `0` means the model is known to be free; a positive value is the
|
|
207
|
+
estimate. That is why a free model reads as `$0`, while an older model the
|
|
208
|
+
host's model list no longer contains stays without a price.
|
|
209
|
+
|
|
210
|
+
A worked example, using the prices of `opencode-go/deepseek-v4.1-flash`
|
|
211
|
+
(`input 0.15`, `output 0.6`, `cache.read 0.003`):
|
|
212
|
+
|
|
213
|
+
```text
|
|
214
|
+
2,591 × 0.15 + (564 + 3,239) × 0.6 + 514,944 × 0.003
|
|
215
|
+
= 4,215.282 per 1,000,000 tokens
|
|
216
|
+
= $0.004215282
|
|
217
|
+
```
|
|
218
|
+
|
|
219
|
+
What each surface shows:
|
|
220
|
+
|
|
221
|
+
| Situation | Line | Panel |
|
|
222
|
+
| --- | --- | --- |
|
|
223
|
+
| Charged | `cost $0.12` | `$0.12` |
|
|
224
|
+
| Subscription-covered (host `0`, host model price known) | `cost ≈ $3.40` | `≈$3.40` |
|
|
225
|
+
| Mixed (some charged, some covered) | `cost $0.12 + ≈ $3.40` | overview `$0.12 + ≈ $3.40`; table cell `≈$3.52` (charged + estimate) |
|
|
226
|
+
| Known free model | cost omitted | `$0` |
|
|
227
|
+
| Price unknown | cost omitted | `$0` |
|
|
228
|
+
|
|
229
|
+
The **turn** estimate on the live line uses the model list the TUI currently
|
|
230
|
+
has. The **`sess`** segment uses the estimate recorded per step and summed for
|
|
231
|
+
the session, as do the panel, the sidebar, the NDJSON export and the CLI report.
|
|
232
|
+
If a price changes after a step is recorded, the live turn can therefore differ
|
|
233
|
+
from the stored history; `list_cost` freezes the price from the recording time.
|
|
234
|
+
The server loads its model list in the background, refreshes it on
|
|
235
|
+
`model.updated` and keeps the previous table if a refresh fails; the TUI
|
|
236
|
+
invalidates its table on the same event and refreshes it on a coarse TTL.
|
|
237
|
+
|
|
238
|
+
For a session, the charged total is the host's; the estimate is
|
|
239
|
+
`SUM(list_cost)` over the steps the plugin recorded. If the plugin started
|
|
240
|
+
recording in the middle of a session, the estimate covers only the recorded
|
|
241
|
+
steps; the charged total still follows the host. When the database is
|
|
242
|
+
unavailable, the line falls back to the host's charged total with no estimate.
|
|
243
|
+
|
|
244
|
+
The `db` view counts the `$0` steps that have no known price yet. `e` retries
|
|
245
|
+
them with today's host model list: free models become `0`, models still in that
|
|
246
|
+
list get an estimate, and models the list no longer contains stay unknown. Steps
|
|
247
|
+
without a provider/model or without tokens have nothing to price and are not
|
|
248
|
+
counted. `pricing: "off"` stops new estimates, hides stored ones everywhere and
|
|
249
|
+
disables the backfill, without deleting the data.
|
|
250
|
+
|
|
138
251
|
## The panel
|
|
139
252
|
|
|
140
253
|
`/usage [view] [range]` opens the panel (`/usage` alone opens the last view, or
|
|
@@ -146,7 +259,7 @@ and any argument — including an unknown token, which is ignored — keeps it o
|
|
|
146
259
|
There is no default key binding — the command is palette-only, so it never
|
|
147
260
|
shadows a key you use.
|
|
148
261
|
|
|
149
|
-

|
|
150
263
|
|
|
151
264
|
The view bar at the top names every view and brackets the active one; `?` opens
|
|
152
265
|
a help overlay that repeats the keys and defines every number.
|
|
@@ -157,16 +270,17 @@ a help overlay that repeats the keys and defines every number.
|
|
|
157
270
|
| --- | --- |
|
|
158
271
|
| `↑` `↓` / `j` `k` | Move the cursor; the row under it is highlighted. |
|
|
159
272
|
| `pageup` `pagedown` / `home` `end` | Move by ten rows / to the first or last row. |
|
|
160
|
-
| `enter` | Drill into the row: a model → the
|
|
273
|
+
| `enter` | Drill into the row: a model → the providers it ran through and their latency; a provider → the models it served and its latency; a session → its turns; a period → the providers of the period. In `db`, prune the steps recorded before the selected retention's cutoff date (asks first). |
|
|
161
274
|
| `backspace` | Go back from a drill-down. |
|
|
162
|
-
| `
|
|
275
|
+
| `1`–`9` / `[` `]` | Pick a view (`9` is `db`) / cycle views. |
|
|
163
276
|
| `t` / `shift+t` | Next / previous range. |
|
|
164
277
|
| `s` / `shift+s` | Cycle the sort column / reverse it. |
|
|
165
278
|
| `b` | `periods` bucket: day → week → month. |
|
|
166
279
|
| `a` | `turns` scope: this session ↔ every session. |
|
|
167
|
-
| `m` `
|
|
280
|
+
| `m` `p` | Filter by model or by **provider** (a picker of the values actually observed). |
|
|
168
281
|
| `x` | Clear the filters. |
|
|
169
282
|
| `r` | Re-read the database. |
|
|
283
|
+
| `e` | `db`: backfill list-price estimates for the `$0` steps that have none (asks first; uses today's model list). |
|
|
170
284
|
| `v` | `db`: compact the file, giving the space of deleted rows back. |
|
|
171
285
|
| `?` | Help overlay. |
|
|
172
286
|
| `f` / `escape` | Full screen / close. |
|
|
@@ -178,40 +292,45 @@ durable storage, so the panel reopens where it was left.
|
|
|
178
292
|
|
|
179
293
|
| View | Answers |
|
|
180
294
|
| --- | --- |
|
|
181
|
-
| `overview` | Steps, sessions, prompts, `in`/`cached`/`write`/`out`, `hit`, cost, an activity sparkline, a `this session` block and a **data coverage** block (how many steps lack a model
|
|
182
|
-
| `models` | Every model with its cache (`hit` and the per-step `hit p50`), tokens, cost, the **
|
|
183
|
-
| `
|
|
184
|
-
| `
|
|
185
|
-
| `
|
|
186
|
-
| `
|
|
187
|
-
| `latency` | Wall time, streaming window and throughput as p50/p90/p99 overall and per model (per endpoint when a model is filtered). `n < 5` is dimmed. |
|
|
295
|
+
| `overview` | Steps, sessions, prompts, `in`/`cached`/`write`/`out`, `hit`, cost, an activity sparkline, a `this session` block and a **data coverage** block (how many models and providers were observed, and how many steps lack a model or a completion — a zero elsewhere is only trustworthy when this is complete). |
|
|
296
|
+
| `models` | Every model with its cache (`hit` and the per-step `hit p50`), tokens, cost, the **providers it ran through** and how often it switched model or provider — with the cache a provider switch cost. `enter` opens the per-provider detail. |
|
|
297
|
+
| `providers` | **Who ran the work**: one row per provider, with the models it served, steps, share, prompt, cached, `hit`, p50 throughput, wall time and cost. `enter` lists every model it served. Steps recorded without a provider (a model that was never observed) are dimmed and cannot be drilled into. |
|
|
298
|
+
| `cache` | Reuse by model, losses grouped by reason (`expired`, `compaction`, `unknown`), **cache survival against idle time**, reuse kept per provider, the observed TTL bounds (`hits up to` / `drops from`) and the most recent losses. |
|
|
299
|
+
| `periods` | Day, week or month totals — steps, sessions, `in`, `cached`, `write`, `hit`, `out`, cost — with a sparkline per metric and a totals row. `enter` lists the providers of a period. |
|
|
300
|
+
| `latency` | Wall time, streaming window and throughput as p50/p90/p99 overall and per model (per provider when a model is filtered). `n < 5` is dimmed. |
|
|
188
301
|
| `sessions` | Each session with steps, prompts, tokens, `hit`, cost, its directory and how many models it used; subagent sessions are indented under their parent and rolled up. |
|
|
189
302
|
| `turns` | The per-prompt history: one line per finished turn, rendered exactly like the live line. `a` widens it to every session, titled. |
|
|
190
|
-
| `db` | Where the file lives, what it holds and how much of it is old. The retention table lists, per preset (`30`/`90`/`365` days), how many steps a prune would remove and the cutoff date it prunes from — pick one with `↑`/`↓` and `enter` prunes it (a confirmation first); `v` gives the space back. |
|
|
303
|
+
| `db` | Where the file lives, what it holds and how much of it is old. The retention table lists, per preset (`30`/`90`/`365` days), how many steps a prune would remove and the cutoff date it prunes from — pick one with `↑`/`↓` and `enter` prunes it (a confirmation first); `v` gives the space back. It also counts the `$0` steps with no list estimate; `e` backfills them at today's list price. |
|
|
191
304
|
|
|
192
305
|
The panel sums the stored per-step buckets over the selected range, so a number
|
|
193
306
|
read in one place means the same in the other: `prompt` is everything the
|
|
194
307
|
provider saw (`cached + fresh + writes`), `in` the uncached part, `hit` is
|
|
195
308
|
`cached / prompt`, and `out` everything generated (visible output + reasoning).
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
309
|
+
A `cost` cell marked `≈` includes list-price estimates for steps the host
|
|
310
|
+
reported as `$0`; the overview and the sidebar keep a mixed total in two parts
|
|
311
|
+
(`$0.12 + ≈ $3.40`) so charged money is never mistaken for the estimate.
|
|
312
|
+
|
|
313
|
+
The identity of a step is the **provider the model ran through** — `opencode`,
|
|
314
|
+
`opencode-go`, `deepseek`, whatever the provider is called in the OpenCode
|
|
315
|
+
configuration — and the model itself. That is what every view groups by, and it
|
|
316
|
+
is also the whole answer available: a gateway such as OpenCode Console may
|
|
317
|
+
re-route a request to another operator, but that upstream is not observable from
|
|
318
|
+
the plugin, and the identity a request *would* have been routed to is not
|
|
319
|
+
evidence of who ran it. The provider is: the same model through `deepseek` and
|
|
320
|
+
through `opencode-go` are two rows, two caches and two latency profiles, and a
|
|
321
|
+
switch between them forfeits the cache the previous one held (the switch counts
|
|
322
|
+
and the cache losses in the `models` view count exactly that).
|
|
205
323
|
|
|
206
324
|
`tps`/`wall`/`stream` are produced tokens per streaming second / request to
|
|
207
325
|
completed step / request to the last chunk. The sidebar's rate (per turn, and
|
|
208
326
|
the session average on its totals) is the **weighted** one — produced tokens
|
|
209
327
|
divided by the summed streaming windows — so it answers "how fast did this
|
|
210
328
|
produce"; the `latency` view reports the p50 of the per-step rates instead, which
|
|
211
|
-
answers "how fast is a typical step". A
|
|
212
|
-
itself a fault —
|
|
213
|
-
count with the cache
|
|
214
|
-
|
|
329
|
+
answers "how fast is a typical step". A switch from one step to the next is not
|
|
330
|
+
by itself a fault — moving to another model or provider is a deliberate choice —
|
|
331
|
+
so the `models` view pairs the count with the cache the switch actually cost,
|
|
332
|
+
which only a provider switch can: two models never share a cache to lose. The
|
|
333
|
+
panel's `?` overlay defines every number in place.
|
|
215
334
|
|
|
216
335
|
## Configuration
|
|
217
336
|
|
|
@@ -222,10 +341,10 @@ Options apply to both entries (each side reads the ones it needs):
|
|
|
222
341
|
| `enabled` | `true` | `false` disables that side completely — no line, no panel, no recording. |
|
|
223
342
|
| `db` | per-OS state path | SQLite path override. Must match between the server and TUI entries. |
|
|
224
343
|
| `turnTotals` | `"latest"` | How the turn segment counts prompt usage: `latest` shows the prompt size the turn ended with; `sum` adds every step of the turn. |
|
|
344
|
+
| `pricing` | `"auto"` | `"off"` disables list-price estimates everywhere: no new `list_cost` records, no `≈` on the line, in the panel or in the sidebar, and no historical backfill. Charged money is unaffected. |
|
|
225
345
|
| `sidebar` | `true` | Show the session summary and the per-prompt history — cache hit and cost per turn — in the session sidebar (the slot is invisible while the sidebar is hidden). |
|
|
226
346
|
| `sidebarTurns` | `5` | How many turns the sidebar history shows. |
|
|
227
347
|
| `panel` | `true` | Show the `/usage` statistics panel and its command. `false` keeps the line and the sidebar. |
|
|
228
|
-
| `identity` | `"endpoint"` | Which identity the line leads with: `endpoint` shows the endpoint that served the turn (falling back to `provider/model`); `model` always shows `provider/model#variant`. |
|
|
229
348
|
| `fields` | see below | Toggle line segments. |
|
|
230
349
|
| `labels` | see below | Override line labels (the plugin is English-only by default). |
|
|
231
350
|
|
|
@@ -234,9 +353,9 @@ Options apply to both entries (each side reads the ones it needs):
|
|
|
234
353
|
```jsonc
|
|
235
354
|
{
|
|
236
355
|
"options": {
|
|
237
|
-
"fields": { "clock": true, "delta": true, "model": true, "variant": true, "turn": true, "session": true, "reasoning": false, "cost":
|
|
356
|
+
"fields": { "clock": true, "delta": true, "model": true, "variant": true, "turn": true, "session": true, "reasoning": false, "cost": true, "cacheExpiry": true },
|
|
238
357
|
"turnTotals": "latest",
|
|
239
|
-
"labels": { "delta": "Δ", "turn": "", "session": "sess", "step": "step", "steps": "steps", "input": "in", "cached": "cached", "output": "out", "reasoning": "reason", "cost": "cost", "cacheExpired": "cache expired", "providerChanged": "changed", "modelChanged": "model changed" }
|
|
358
|
+
"labels": { "delta": "Δ", "turn": "", "session": "sess", "step": "step", "steps": "steps", "input": "in", "cached": "cached", "output": "out", "reasoning": "reason", "cost": "cost", "estimate": "≈", "cacheExpired": "cache expired", "providerChanged": "changed", "modelChanged": "model changed" }
|
|
240
359
|
}
|
|
241
360
|
}
|
|
242
361
|
```
|
|
@@ -256,15 +375,17 @@ The recorder writes to the per-user state directory:
|
|
|
256
375
|
|
|
257
376
|
`OPENCODE_USAGE_INSIGHTS_DB=/path/to/usage.db` overrides the path, and the `db`
|
|
258
377
|
option overrides both. The file is private to the user: its directory is created
|
|
259
|
-
`0700` and the file `0600` (POSIX), re-applied on every open. The TUI
|
|
260
|
-
through a read-only connection, so
|
|
261
|
-
recorder's lock.
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
378
|
+
`0700` and the file `0600` (POSIX), re-applied on every open. The TUI's views
|
|
379
|
+
read through a read-only connection, so they never write, migrate or take the
|
|
380
|
+
recorder's lock; prune, backfill and compact use a separate short-lived writer.
|
|
381
|
+
The server entry applies schema migrations on open; after an upgrade, start it
|
|
382
|
+
once before opening the panel if the file predates the new schema.
|
|
383
|
+
|
|
384
|
+
Per step the database holds tokens, charged cost, the list-price estimate when
|
|
385
|
+
the host reported `$0`, timings, finish reason, the provider and the model it ran
|
|
386
|
+
as, and the session title and working directory. **Prompts and responses are not
|
|
387
|
+
stored**, and nothing leaves the machine. The schema, the indexes, the identity
|
|
388
|
+
rules and the security notes live in [docs/internals.md](docs/internals.md).
|
|
268
389
|
|
|
269
390
|
## CLI report
|
|
270
391
|
|
|
@@ -274,9 +395,10 @@ From a checkout, the database can be read without opening the TUI:
|
|
|
274
395
|
bun run report # overview over all time
|
|
275
396
|
bun run report models 7d # one view, one range
|
|
276
397
|
bun run report all # every view
|
|
277
|
-
bun run report
|
|
398
|
+
bun run report providers --model deepseek-v4.1-flash
|
|
278
399
|
bun run report periods --granularity month
|
|
279
|
-
bun run report
|
|
400
|
+
bun run report models --provider opencode-go
|
|
401
|
+
bun run report --no-estimates # charged money only, like pricing: "off"
|
|
280
402
|
bun run report --json # raw rows and every analysis, for scripting
|
|
281
403
|
bun run report --ndjson > backup.jsonl # export the whole database
|
|
282
404
|
bun run report --import backup.jsonl # merge an export (idempotent)
|
|
@@ -285,7 +407,7 @@ bun run report --help
|
|
|
285
407
|
|
|
286
408
|
The views come from the same `buildPanel` the TUI renders, so the terminal and
|
|
287
409
|
the panel never drift apart. `--width`, `--granularity`, `--model`,
|
|
288
|
-
`--
|
|
410
|
+
`--provider`, `--sort` and `--asc` mirror what the panel's keys do.
|
|
289
411
|
|
|
290
412
|
## Development
|
|
291
413
|
|
|
@@ -302,7 +424,8 @@ bun run docs:images # re-render docs/images/*.png (needs ImageMagick)
|
|
|
302
424
|
turn accounting, the host-shape adapter, the view models, and the database,
|
|
303
425
|
queries and analysis layer. No host imports, so they are unit-testable without
|
|
304
426
|
a TUI runtime.
|
|
305
|
-
- `src/
|
|
427
|
+
- `src/text.ts` — text hygiene and the token shape a provider id is validated
|
|
428
|
+
against.
|
|
306
429
|
- `src/recorder.ts` — the event-stream state machine that fills the database.
|
|
307
430
|
- `src/panel-build.ts`, `src/panel.tsx`, `src/sidebar.tsx` — panel assembly, the
|
|
308
431
|
panel component, and the sidebar contribution.
|
package/dist/core.d.ts
CHANGED
|
@@ -30,6 +30,8 @@ export type AssistantMessage = {
|
|
|
30
30
|
} | undefined;
|
|
31
31
|
} | undefined;
|
|
32
32
|
cost?: number | undefined;
|
|
33
|
+
/** List-price estimate for a step the host reported as costing nothing. */
|
|
34
|
+
listCost?: number | undefined;
|
|
33
35
|
finish?: string | null | undefined;
|
|
34
36
|
rawFinish?: string | null | undefined;
|
|
35
37
|
};
|
|
@@ -55,6 +57,8 @@ export type Totals = {
|
|
|
55
57
|
reasoning: number;
|
|
56
58
|
cacheWrite: number;
|
|
57
59
|
cost: number;
|
|
60
|
+
/** Sum of list-price estimates for steps the host reported as $0. */
|
|
61
|
+
listCost: number;
|
|
58
62
|
steps: number;
|
|
59
63
|
};
|
|
60
64
|
export type CacheDrop = {
|
|
@@ -78,15 +82,6 @@ export type TurnStats = {
|
|
|
78
82
|
providerID: string | undefined;
|
|
79
83
|
modelID: string | undefined;
|
|
80
84
|
variant: string | undefined;
|
|
81
|
-
/** Gateway routing endpoint that served the turn, when a source identifies it. */
|
|
82
|
-
router: string | undefined;
|
|
83
|
-
/**
|
|
84
|
-
* The displayed identity differs from the previous turn's. The TUI fills it
|
|
85
|
-
* in (the transcript carries no routing information); the line warns with the
|
|
86
|
-
* same `changed` marker it uses for a provider switch.
|
|
87
|
-
*/
|
|
88
|
-
identityChanged: boolean;
|
|
89
|
-
affinityKey: string | undefined;
|
|
90
85
|
/** Provider ID differs from the previous turn in the same transcript. */
|
|
91
86
|
providerChanged: boolean;
|
|
92
87
|
/** Provider is the same but the model or variant differs. */
|
|
@@ -105,13 +100,6 @@ export type TurnStats = {
|
|
|
105
100
|
cacheExpired: boolean;
|
|
106
101
|
/** Assistant steps in the turn. */
|
|
107
102
|
steps: number;
|
|
108
|
-
/**
|
|
109
|
-
* Message ids of the settled steps of the turn, oldest first. The TUI looks
|
|
110
|
-
* up the endpoint the recorder stored for these steps, so the line reports
|
|
111
|
-
* who served *this* turn: a lookup by provider and model alone would tag a
|
|
112
|
-
* turn with the endpoint of an older one.
|
|
113
|
-
*/
|
|
114
|
-
stepIDs: string[];
|
|
115
103
|
};
|
|
116
104
|
export type TurnSegment = {
|
|
117
105
|
assistants: AssistantMessage[];
|
|
@@ -141,16 +129,29 @@ export declare function tokenCounts(message: AssistantMessage): {
|
|
|
141
129
|
write: number;
|
|
142
130
|
};
|
|
143
131
|
export declare function totalize(steps: readonly AssistantMessage[]): Totals;
|
|
144
|
-
|
|
132
|
+
/**
|
|
133
|
+
* The identity of a model reference: the provider it runs through, the model
|
|
134
|
+
* and its variant. Two steps with the same key ran the same thing the same way,
|
|
135
|
+
* which is what `modelChanged` compares.
|
|
136
|
+
*/
|
|
137
|
+
export declare function identityKey(model: ModelRefLike | undefined): string | undefined;
|
|
145
138
|
export declare function cacheRead(message: AssistantMessage | undefined): number;
|
|
146
139
|
export declare function stepCreated(message: AssistantMessage | undefined): number | undefined;
|
|
147
140
|
export declare function stepEnd(message: AssistantMessage | undefined): number | undefined;
|
|
141
|
+
/**
|
|
142
|
+
* Which identity the two steps must share for a reuse loss to be measurable.
|
|
143
|
+
* `provider` is the strict one: two providers are two caches, so a switch
|
|
144
|
+
* between them is not a loss but a different cache. `model` drops that test,
|
|
145
|
+
* for the reports that measure what an identity change costs: forfeiting the
|
|
146
|
+
* previous provider's cache is exactly the cost they report.
|
|
147
|
+
*/
|
|
148
|
+
export type CacheDropScope = "provider" | "model";
|
|
148
149
|
/**
|
|
149
150
|
* Cache reuse loss against a reference step, mirroring OpenCode's own
|
|
150
151
|
* `turn-usage` heuristic: only comparable when the model is identical, and
|
|
151
|
-
* OpenAI's
|
|
152
|
+
* OpenAI's API reports small reads that are not worth flagging.
|
|
152
153
|
*/
|
|
153
|
-
export declare function cacheDrop(reference: AssistantMessage | undefined, current: AssistantMessage): number | undefined;
|
|
154
|
+
export declare function cacheDrop(reference: AssistantMessage | undefined, current: AssistantMessage, scope?: CacheDropScope): number | undefined;
|
|
154
155
|
/** Milliseconds between a reference step's completion and the next request. */
|
|
155
156
|
export declare function stepGap(reference: AssistantMessage | undefined, current: AssistantMessage): number | undefined;
|
|
156
157
|
/**
|
|
@@ -179,13 +180,14 @@ export declare function stepTps(message: AssistantMessage): number | undefined;
|
|
|
179
180
|
export declare function isSettledStep(message: AssistantMessage): boolean;
|
|
180
181
|
/**
|
|
181
182
|
* Compute the stats of the most recent turn in a transcript, including the
|
|
182
|
-
* comparison against the previous turn (
|
|
183
|
+
* comparison against the previous turn (a provider or model switch, and cache
|
|
184
|
+
* reuse).
|
|
183
185
|
*
|
|
184
186
|
* Returns undefined while the newest turn is still being generated: the line
|
|
185
187
|
* appears when a turn ends, never with the zeroes of an in-flight response.
|
|
186
188
|
*/
|
|
187
189
|
export declare function computeTurn(messages: readonly TranscriptMessage[], session?: Totals): TurnStats | undefined;
|
|
188
|
-
/** True when a cache drop is small enough to be
|
|
190
|
+
/** True when a cache drop is small enough to be provider noise. */
|
|
189
191
|
export declare function isNoiseDrop(providerID: string | undefined, drop: number): boolean;
|
|
190
192
|
export type LineTone = "base" | "muted" | "warning";
|
|
191
193
|
export type LineSegment = {
|
|
@@ -203,6 +205,8 @@ export type LineLabels = {
|
|
|
203
205
|
output: string;
|
|
204
206
|
reasoning: string;
|
|
205
207
|
cost: string;
|
|
208
|
+
/** Prefix for a list-price estimate, e.g. `cost ≈ $0.03`. */
|
|
209
|
+
estimate: string;
|
|
206
210
|
cacheExpired: string;
|
|
207
211
|
providerChanged: string;
|
|
208
212
|
modelChanged: string;
|
|
@@ -214,14 +218,6 @@ export type LineLabels = {
|
|
|
214
218
|
* tool-call loop re-sends (and re-reads) the same context once per step.
|
|
215
219
|
*/
|
|
216
220
|
export type TurnTotalsView = "latest" | "sum";
|
|
217
|
-
/**
|
|
218
|
-
* Which identity the line leads with. `endpoint` shows the gateway endpoint
|
|
219
|
-
* that served the turn (`orcarouter-dsv4.1flash`) when a recorder learned one,
|
|
220
|
-
* falling back to `provider/model`; `model` always shows `provider/model`.
|
|
221
|
-
* The same provider and model can be routed to different endpoints per request,
|
|
222
|
-
* so the endpoint is the identity a change is measured against.
|
|
223
|
-
*/
|
|
224
|
-
export type LineIdentity = "endpoint" | "model";
|
|
225
221
|
export type LineFields = {
|
|
226
222
|
clock: boolean;
|
|
227
223
|
delta: boolean;
|
|
@@ -239,6 +235,17 @@ export declare function formatClock(ms: number): string;
|
|
|
239
235
|
export declare function formatDuration(ms: number): string;
|
|
240
236
|
export declare function formatTokens(value: number): string;
|
|
241
237
|
export declare function formatCost(value: number): string;
|
|
238
|
+
/**
|
|
239
|
+
* Charged and estimated money as one string. A mixed total keeps both parts
|
|
240
|
+
* visible so list price is never mistaken for money that was charged:
|
|
241
|
+
* `$0.12 + ≈ $3.40`. Returns undefined when there is no money to show.
|
|
242
|
+
*/
|
|
243
|
+
export declare function formatMoney(charged: number, estimated: number, marker?: string): string | undefined;
|
|
244
|
+
/**
|
|
245
|
+
* The list-equivalent total for a compact table cell: charged money plus the
|
|
246
|
+
* estimates, marked with `≈` when any part is estimated.
|
|
247
|
+
*/
|
|
248
|
+
export declare function formatSpend(charged: number, estimated: number, marker?: string): string;
|
|
242
249
|
/**
|
|
243
250
|
* The buckets the line displays, derived from a totals group. `input` is the
|
|
244
251
|
* *uncached* part of the prompt (fresh input plus cache writes, i.e. what the
|
|
@@ -261,11 +268,11 @@ export declare function formatLineSegments(stats: TurnStats, options?: {
|
|
|
261
268
|
labels?: Partial<LineLabels>;
|
|
262
269
|
fields?: Partial<LineFields>;
|
|
263
270
|
turnTotals?: TurnTotalsView;
|
|
264
|
-
|
|
271
|
+
estimates?: boolean;
|
|
265
272
|
}): LineSegment[];
|
|
266
273
|
export declare function formatLine(stats: TurnStats, options?: {
|
|
267
274
|
labels?: Partial<LineLabels>;
|
|
268
275
|
fields?: Partial<LineFields>;
|
|
269
276
|
turnTotals?: TurnTotalsView;
|
|
270
|
-
|
|
277
|
+
estimates?: boolean;
|
|
271
278
|
}): string;
|
package/dist/index.d.ts
CHANGED
|
@@ -4,15 +4,22 @@ import { Plugin } from "@opencode/plugin";
|
|
|
4
4
|
* every assistant step, prompt and lifecycle event into the stats database;
|
|
5
5
|
* the TUI entry reads the same database for its live line and stats panel.
|
|
6
6
|
*
|
|
7
|
-
*
|
|
8
|
-
*
|
|
9
|
-
* never
|
|
7
|
+
* Everything the recorder stores comes from the event stream — the provider
|
|
8
|
+
* and model a step ran as, the tokens and the timing. The plugin is
|
|
9
|
+
* observational: it never rewrites prompts, requests or responses, registers
|
|
10
|
+
* no hook on the request path, and a failure inside the recorder is swallowed
|
|
11
|
+
* so a session can never be interrupted by its own telemetry.
|
|
10
12
|
*/
|
|
11
13
|
export type Options = {
|
|
12
14
|
/** Turn recording off: no event subscription, no database. */
|
|
13
15
|
enabled?: boolean | undefined;
|
|
14
16
|
/** SQLite database path override (defaults to the per-OS state directory). */
|
|
15
17
|
db?: string | undefined;
|
|
18
|
+
/**
|
|
19
|
+
* List-price estimates for steps the host reports as costing nothing.
|
|
20
|
+
* `off` records charged money only; the TUI hides any stored estimates.
|
|
21
|
+
*/
|
|
22
|
+
pricing?: "auto" | "off" | undefined;
|
|
16
23
|
};
|
|
17
24
|
declare const _default: Plugin.Plugin;
|
|
18
25
|
export default _default;
|