opencode-usage-insights 0.1.2 → 0.1.3

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 CHANGED
@@ -5,38 +5,42 @@
5
5
  [![Lint](https://img.shields.io/github/actions/workflow/status/hugobatista/opencode-usage-insights/lint.yml?label=Lint)](https://github.com/hugobatista/opencode-usage-insights/actions/workflows/lint.yml)
6
6
  [![npm](https://img.shields.io/npm/v/opencode-usage-insights.svg)](https://www.npmjs.com/package/opencode-usage-insights)
7
7
 
8
- OpenCode plugin for one question the host cannot answer: **which provider is
9
- actually serving each model** — and what that costs. It shows a telemetry line
10
- above the composer after every finished turn and records every assistant step
11
- in a local SQLite file, read back through an interactive `/usage` panel.
8
+ **See what each turn costs.** OpenCode reports the cost of each request, but the
9
+ TUI no longer shows it. This plugin puts cost back in view: a telemetry line
10
+ after every turn, the session's running spend in the sidebar, and an interactive
11
+ `/usage` panel with charged cost, list-price estimates, tokens, prompt-cache
12
+ reuse and latency.
12
13
 
13
14
  ![The telemetry line after a turn](docs/images/line.png)
14
15
 
15
- Everything stays local: one SQLite file under the OpenCode state directory,
16
- written by the server-side recorder and read by the TUI.
16
+ Everything stays local: no prompts or responses are stored, and nothing leaves
17
+ the machine.
17
18
 
18
19
  ## What it does
19
20
 
20
- - **The line** (`session.composer.top`): end time, the gap since the previous
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.
21
+ - **The line** (`session.composer.top`): what the turn cost, the tokens and
22
+ prompt cache behind it, when it ended, the gap since the previous prompt, the
23
+ model and configured provider, and warnings when the prompt cache or the
24
+ provider changed. It belongs to finished work, so it stays hidden while a
25
+ turn is running.
24
26
  - **The `/usage` panel**: nine keyboard-driven views — `overview`, `models`,
25
27
  `providers`, `cache`, `periods`, `latency`, `sessions`, `turns`,
26
28
  `db` — over five ranges (`session`, `today`, `7d`, `30d`, `all`), with
27
29
  drill-downs, sorting, filters and state remembered between openings.
28
30
  `/usage models 7d`, `/usage providers 30d`, `/usage turns 7d`.
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 which
31
+ - **The session sidebar**: the session's running spend and its last turns, in
32
+ the same buckets as the line. Three lines per turn — when and through which
31
33
  provider, the prompt volumes, then `hit · tok/s · cost` — plus the model at
32
34
  both ends of the block and on any turn that changed it, so the history reads
33
35
  whether it is the whole session or a window of a long one:
34
36
  `16:54 opencode-go` / `deepseek-v4.1-flash max` /
35
37
  `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
+ - **The record** (server part): every assistant step with tokens, charged cost,
39
+ the list-price estimate when the host reported `$0`, timings, finish reason,
40
+ the configured provider and model it ran through, and its session and parent
41
+ ids; prompts, session lifecycle and compaction events too; and the host's
42
+ `session.usage.recorded` for compactions and title generations, which have no
43
+ assistant step of their own.
40
44
  - **Portable data** (from a checkout): `bun run report --ndjson` exports the
41
45
  whole database, and `--import` merges it back on another machine.
42
46
  - **Observational by design**: the plugin never rewrites prompts, requests or
@@ -62,8 +66,12 @@ The plugin is published on npm as `opencode-usage-insights`.
62
66
  opencode plugin add opencode-usage-insights
63
67
  ```
64
68
 
65
- Or add the package to `~/.config/opencode/opencode.jsonc` (server entry) and
66
- `~/.config/opencode/cli.json` (TUI entry):
69
+ This adds one entry to the global `opencode.json(c)`. That entry loads both
70
+ parts: the server recorder and the TUI contributions. A TUI-only package goes in
71
+ `~/.config/opencode/cli.json`; this plugin has both parts, so `opencode.json(c)`
72
+ is enough.
73
+
74
+ If you prefer to edit the configuration yourself:
67
75
 
68
76
  ```jsonc
69
77
  // ~/.config/opencode/opencode.jsonc
@@ -73,20 +81,12 @@ Or add the package to `~/.config/opencode/opencode.jsonc` (server entry) and
73
81
  }
74
82
  ```
75
83
 
76
- ```jsonc
77
- // ~/.config/opencode/cli.json
78
- {
79
- "$schema": "https://opencode.ai/v2/cli.json",
80
- "plugins": ["opencode-usage-insights"]
81
- }
82
- ```
83
-
84
84
  To pass [options](#configuration), use the object form:
85
85
 
86
86
  ```jsonc
87
87
  {
88
88
  "plugins": [
89
- { "package": "opencode-usage-insights", "options": { "sidebarTurns": 5 } }
89
+ { "package": "opencode-usage-insights", "options": { "sidebarTurns": 8 } }
90
90
  ]
91
91
  }
92
92
  ```
@@ -100,11 +100,6 @@ and the TUI contributions (`src/tui.tsx`) from source:
100
100
  { "plugins": [{ "package": "/home/you/code/projects/opencode-usage-insights/src" }] }
101
101
  ```
102
102
 
103
- ```jsonc
104
- // ~/.config/opencode/cli.json
105
- { "plugins": [{ "package": "/home/you/code/projects/opencode-usage-insights/src" }] }
106
- ```
107
-
108
103
  Configuration changes reload on their own; after changing the plugin's source,
109
104
  restart the background service with `opencode service restart`.
110
105
 
@@ -121,11 +116,11 @@ turn finishes, and `/usage` (alias `/insights`) opens the panel.
121
116
  | --- | --- |
122
117
  | `14:32:07` | When the response ended (`completed`, else `streamed`, else `created`). |
123
118
  | `Δ 3m12s` | Time since the previous prompt of the same session. |
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. |
119
+ | `opencode-go/deepseek-v4.1-flash max` | The identity of the turn: the configured provider the model ran through 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
120
  | `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
121
  | `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
122
  | `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`. |
123
+ | `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 (including its compaction and title generations), 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`. |
129
124
  | `⚠ 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. |
130
125
 
131
126
  The turn segment carries no label by default — the numbers are the turn's, and
@@ -216,6 +211,26 @@ A worked example, using the prices of `opencode-go/deepseek-v4.1-flash`
216
211
  = $0.004215282
217
212
  ```
218
213
 
214
+ ### Compactions and titles
215
+
216
+ The host also reports usage with no assistant step behind it:
217
+ `session.usage.recorded` with `source: "compaction"` or `source: "title"`. It is
218
+ part of the session's own `cost`, and the plugin stores it in the `usage` table,
219
+ keyed by the host event ID so a replay never counts twice.
220
+
221
+ Its cost is added to the **overview**, **periods** and **sessions** totals — the
222
+ same money the host already includes in the session. The `models`, `providers`
223
+ and `turns` rows stay step-based: the host's non-step usage record carries no
224
+ model, so attributing the money to one would invent an identity. The overview
225
+ shows the part as an `other` row, and a session row names it under its title.
226
+
227
+ This is host money, not a list-price estimate, so `pricing: "off"` does not hide
228
+ it. A database written by an older plugin is healed at open: the stored
229
+ compaction events already carry `cost` and `tokens`, and a migration recovers
230
+ them. Title usage recorded before the upgrade was never stored anywhere and
231
+ stays missing; the line's `sess` segment (which reads the host's own session
232
+ total) keeps covering the whole session meanwhile.
233
+
219
234
  What each surface shows:
220
235
 
221
236
  | Situation | Line | Panel |
@@ -223,6 +238,7 @@ What each surface shows:
223
238
  | Charged | `cost $0.12` | `$0.12` |
224
239
  | Subscription-covered (host `0`, host model price known) | `cost ≈ $3.40` | `≈$3.40` |
225
240
  | Mixed (some charged, some covered) | `cost $0.12 + ≈ $3.40` | overview `$0.12 + ≈ $3.40`; table cell `≈$3.52` (charged + estimate) |
241
+ | Compaction/title (host `session.usage.recorded`) | included in the host's `sess` total | overview `other $0.0042`; session and period cost include it |
226
242
  | Known free model | cost omitted | `$0` |
227
243
  | Price unknown | cost omitted | `$0` |
228
244
 
@@ -235,11 +251,14 @@ The server loads its model list in the background, refreshes it on
235
251
  `model.updated` and keeps the previous table if a refresh fails; the TUI
236
252
  invalidates its table on the same event and refreshes it on a coarse TTL.
237
253
 
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.
254
+ For a session, the charged total is the host's (including its compaction and
255
+ title generations); the estimate is `SUM(list_cost)` over the steps the plugin
256
+ recorded. The overview, periods and session rows additionally add the host's
257
+ non-step usage cost; the line's `sess` segment already has it in the host's
258
+ total, so it is not added twice. If the plugin started recording in the middle
259
+ of a session, the estimate covers only the recorded steps; the charged total
260
+ still follows the host. When the database is unavailable, the line falls back to
261
+ the host's charged total with no estimate.
243
262
 
244
263
  The `db` view counts the `$0` steps that have no known price yet. `e` retries
245
264
  them with today's host model list: free models become `0`, models still in that
@@ -292,15 +311,15 @@ durable storage, so the panel reopens where it was left.
292
311
 
293
312
  | View | Answers |
294
313
  | --- | --- |
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. |
314
+ | `overview` | Steps, sessions, prompts, `in`/`cached`/`write`/`out`, `hit`, cost (with an `other` row naming the compaction/title part), 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). |
315
+ | `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. Cost is assistant steps only; `enter` opens the per-provider detail. |
316
+ | `providers` | **Which configured provider ran each model**: one row per provider, with the models it served, steps, share, prompt, cached, `hit`, p50 throughput, wall time and cost. Cost is assistant steps only. `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
317
  | `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. |
318
+ | `periods` | Day, week or month totals — steps, sessions, `in`, `cached`, `write`, `hit`, `out`, cost (including compaction/title usage) — with a sparkline per metric and a totals row. `enter` lists the providers of a period. |
300
319
  | `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. |
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. |
302
- | `turns` | The per-prompt history: one line per finished turn, rendered exactly like the live line. `a` widens it to every session, titled. |
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. |
320
+ | `sessions` | Each session with steps, prompts, tokens, `hit`, cost (including compaction/title usage, named on the detail line), its directory and how many models it used; subagent sessions are indented under their parent and rolled up. |
321
+ | `turns` | The per-prompt history: one line per finished turn, rendered exactly like the live line. Cost is assistant steps only. `a` widens it to every session, titled. |
322
+ | `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), removing the steps' compaction/title usage with them; `v` gives the space back. It also counts the `$0` steps with no list estimate; `e` backfills them at today's list price. |
304
323
 
305
324
  The panel sums the stored per-step buckets over the selected range, so a number
306
325
  read in one place means the same in the other: `prompt` is everything the
@@ -310,16 +329,14 @@ A `cost` cell marked `≈` includes list-price estimates for steps the host
310
329
  reported as `$0`; the overview and the sidebar keep a mixed total in two parts
311
330
  (`$0.12 + ≈ $3.40`) so charged money is never mistaken for the estimate.
312
331
 
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).
332
+ The identity of a step is the **configured provider** the model ran through —
333
+ `opencode`, `opencode-go`, `deepseek`, whatever the provider is called in the
334
+ OpenCode configuration — and the model itself. That is what every view groups
335
+ by. A gateway may route a request to another operator, but that upstream is not
336
+ observable, so it is not guessed. The same model through `deepseek` and through
337
+ `opencode-go` is two rows, two caches and two latency profiles, and a switch
338
+ between them forfeits the cache the previous one held (the switch counts and the
339
+ cache losses in the `models` view count exactly that).
323
340
 
324
341
  `tps`/`wall`/`stream` are produced tokens per streaming second / request to
325
342
  completed step / request to the last chunk. The sidebar's rate (per turn, and
@@ -334,16 +351,16 @@ panel's `?` overlay defines every number in place.
334
351
 
335
352
  ## Configuration
336
353
 
337
- Options apply to both entries (each side reads the ones it needs):
354
+ Options apply to both parts (each side reads the ones it needs):
338
355
 
339
356
  | Option | Default | Meaning |
340
357
  | --- | --- | --- |
341
358
  | `enabled` | `true` | `false` disables that side completely — no line, no panel, no recording. |
342
- | `db` | per-OS state path | SQLite path override. Must match between the server and TUI entries. |
359
+ | `db` | per-OS state path | SQLite path override. Must match between the server and TUI parts. |
343
360
  | `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
361
  | `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. |
345
362
  | `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). |
346
- | `sidebarTurns` | `5` | How many turns the sidebar history shows. |
363
+ | `sidebarTurns` | `8` | How many turns the sidebar history shows. The host sidebar scrolls; raise it for a longer history. |
347
364
  | `panel` | `true` | Show the `/usage` statistics panel and its command. `false` keeps the line and the sidebar. |
348
365
  | `fields` | see below | Toggle line segments. |
349
366
  | `labels` | see below | Override line labels (the plugin is English-only by default). |
@@ -378,14 +395,15 @@ option overrides both. The file is private to the user: its directory is created
378
395
  `0700` and the file `0600` (POSIX), re-applied on every open. The TUI's views
379
396
  read through a read-only connection, so they never write, migrate or take the
380
397
  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
398
+ The server part applies schema migrations on open; after an upgrade, start it
382
399
  once before opening the panel if the file predates the new schema.
383
400
 
384
401
  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).
402
+ the host reported `$0`, timings, finish reason, the configured provider and the
403
+ model it ran through, and the session title and working directory. **Prompts and
404
+ responses are not stored**, and nothing leaves the machine. The schema, the
405
+ indexes, the identity rules and the security notes live in
406
+ [docs/internals.md](docs/internals.md).
389
407
 
390
408
  ## CLI report
391
409
 
@@ -429,7 +447,7 @@ bun run docs:images # re-render docs/images/*.png (needs ImageMagick)
429
447
  - `src/recorder.ts` — the event-stream state machine that fills the database.
430
448
  - `src/panel-build.ts`, `src/panel.tsx`, `src/sidebar.tsx` — panel assembly, the
431
449
  panel component, and the sidebar contribution.
432
- - `src/index.ts`, `src/tui.tsx` — the server and TUI entries.
450
+ - `src/index.ts`, `src/tui.tsx` — the server and TUI parts.
433
451
  - `scripts/` — the demo, report, bench, platform-check and documentation-image
434
452
  CLIs, plus the two-entry `Bun.build`.
435
453