Local token usage tools for OpenCode and Pi. The OpenCode server plugin and Pi extension write token/TPS/request/tool-call data to SQLite; the OpenCode TUI plugin queries the DB for live display; the Go CLI reads aggregate tables.
See docs/design.md for full architecture, schema contract, event flow, and invariants.
plugins/opencode-tui/— OpenCode TUI pluginplugins/opencode-server/— OpenCode server pluginplugins/shared/— Shared types, schema migration, writer client, writer workerplugins/pi/— Pi extensioncli/— Go CLI (tokeninsights-cli) that queries the SQLite DBschema/schema.sql— single source of truth for SQLite schemascripts/check-schema.ts— cross-language schema contract validator
| You changed | Run |
|---|---|
schema/schema.sql |
npm run check-schema |
Any .ts in plugins/ |
npm run smoke:plugins |
Any .go in cli/ |
npm run test:go && npm run build:cli |
| Storage, schema, events, SQL, aggregation, rendering, or tests | Update both plugin and CLI; npm run verify:all |
⚠️ Schema changes are user-approved only. Never modifyschema/schema.sqlwithout explicit user approval, even for additive changes. Always explain the rationale and ask first.
npm run smoke:pluginsnpm run test:go
npm run build:cliBuild the CLI first, then run against your local database:
npm run build:cli
npm run smoke:dbOpenCode auto-discovers server plugins in ~/.config/opencode/plugins/. Symlink the server plugin there:
mkdir -p ~/.config/opencode/plugins
ln -s "$PWD/plugins/opencode-server/oc-tokeninsights-server.ts" ~/.config/opencode/plugins/Then remove any tokeninsights entry from opencode.jsonc.
TUI plugins do not auto-discover. Add the TUI plugin to your per-OS tui.json:
macOS (~/.config/opencode/tui.json):
{
"plugin": [
"/Users/dineshpandiyan/workspace/tokeninsights/plugins/opencode-tui/oc-tokeninsights.tsx"
]
}Linux (~/.config/opencode/tui.json):
{
"plugin": [
"/home/dee/workspace/tokeninsights/plugins/opencode-tui/oc-tokeninsights.tsx"
]
}Do not add plugins/shared/oc-tokeninsights-writer.ts or plugins/shared/writer-client.ts to config. The writer is a worker module loaded internally by the server plugin.
Default DB path: ~/.local/state/tokeninsights/tokeninsights.sqlite
Environment overrides for writers:
TOKENINSIGHTS_DB_PATH— absolute path, or relative to the TokenInsights state directoryTOKENINSIGHTS_RETENTION_DAYS— retention window for pruning durable rows
Pi extensions auto-discover from ~/.pi/agent/extensions/. Copy or symlink the extension directory there and install its dependency:
mkdir -p ~/.pi/agent/extensions
ln -s "$PWD/plugins/pi" ~/.pi/agent/extensions/pi-tokeninsights
cd ~/.pi/agent/extensions/pi-tokeninsights
npm installThe Pi extension writes to the same TokenInsights DB as the OpenCode plugins (~/.local/state/tokeninsights/tokeninsights.sqlite) but stores data in the pi_* table family. The CLI reads both oc_* and pi_* tables and shows a harness column (oc or pi) to distinguish sources.
Tool calls are tracked as lifecycle rows (started, completed, error). The CLI tool calls tab shows started-call counts plus error counts per normal group; tool breakdown adds per-tool grouping.
tokeninsights-cli --db-path ~/.local/state/tokeninsights/tokeninsights.sqlite --today
tokeninsights-cli --db-path ~/.local/state/tokeninsights/tokeninsights.sqlite --week
tokeninsights-cli --db-path ~/.local/state/tokeninsights/tokeninsights.sqlite --month
tokeninsights-cli --db-path ~/.local/state/tokeninsights/tokeninsights.sqlite --all-time
tokeninsights-cli --db-path ~/.local/state/tokeninsights/tokeninsights.sqlite --week --group-by=hour
tokeninsights-cli --db-path ~/.local/state/tokeninsights/tokeninsights.sqlite --week --group-by=session
tokeninsights-cli --db-path ~/.local/state/tokeninsights/tokeninsights.sqlite --week --provider openai --model gpt-5.5
tokeninsights-cli --db-path ~/.local/state/tokeninsights/tokeninsights.sqlite --week --harness pi
tokeninsights-cli --db-path ~/.local/state/tokeninsights/tokeninsights.sqlite --month
tokeninsights-cli --db-path ~/.local/state/tokeninsights/tokeninsights.sqlite --all-time --filter-day 2026-04-24,2026-04-23Interactive keys:
tab/shift+tab— switch tabs (tokens, tps, requests, tool calls, tool breakdown)g— open grouping popupf— open filter popup for provider or harness↑/↓/j/k— scroll vertically or move cursor in popup←/→/h/l— scroll the table horizontallyhome/end— jump to the start/end of the horizontal table viewport- grouping popup:
space/enterselects grouping mode - filter popup:
space/enterenters value selection; in value selection,spacetoggles values andenterapplies escin a popup closes without applying staged filter changesq/esc/ctrl+c— quit