You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Token usage: no stable machine-readable export and no turn boundaries for external consumers #60278
OpenCode tracks token usage per assistant message and per session, but exposes
no stable, documented way to consume that data outside the TUI. External
consumers (scripts, CI, cost dashboards, agent tooling) are forced to
reverse-engineer the internal SQLite store, including WAL side files.
Current gaps
No machine-readable export. Usage lives in session_v2.tokens_* and session_message.data (JSON-in-TEXT) inside ~/.local/share/opencode/opencode.db.
There is no CLI command or documented API to export per-session /
per-message usage as JSON/Markdown.
No turn or invocation boundary. Sessions have no turn_id or
task-boundary event. A long-lived or reused session cannot be split into
separate invocations, so usage from earlier work bleeds into later reports.
No per-response identity beyond message.id. There is no response-level
id comparable to other hosts, which limits dedup and cross-checking.
Effort is a model variant, not reasoning effort.model.variant
(default/low/high) is the only signal; there is no reasoning-effort
field, so consumers cannot distinguish effort levels reliably.
Schema is internal and versioned.session vs session_v2, message vs session_message, JSON-in-TEXT columns. Any rename breaks
parsers, and the DB is live with WAL files, requiring snapshot copying.
Requested improvement
Please consider adding a stable, documented way to obtain per-session and
per-message token usage:
a CLI command such as opencode usage export --session <id> --format json
(or extending opencode stats) emitting per-message records with session_id, parent_session_id, agent, model, provider, variant, tokens { input, output, reasoning, cache { read, write } }, time { created, completed };
optional per-turn boundaries (a turn id or task-boundary event) so a reused
session's usage can be attributed to the correct invocation;
alternatively, document the SQLite schema as a supported read-only interface.
Why this matters
Token accounting is increasingly performed by external tooling, not only by the
TUI. A stable export would let consumers avoid coupling to OpenCode internals.
(Related but different in scope: #41915 requests an in-TUI /usage report.)
Thanks for the detailed write-up, @suale-dev! As I understand it, you'd like a stable, documented way to get token usage out of OpenCode for each session and each message. That could be something like opencode usage export --session <id> --format json or an extension of opencode stats. You'd also like turn or invocation boundaries, so that usage in a reused session can be attributed to the right run, instead of external tools having to parse the internal SQLite database.
For reference, v2 currently has opencode stats --json, which outputs aggregate totals only, and opencode session export, which dumps the session as JSON through an experimental endpoint. Neither offers a documented per-message usage schema or turn ids, so this is a fair gap to raise.
Activity
opencodejr commentedon Oct 29, 2026
Thanks for the detailed write-up, @suale-dev! As I understand it, you'd like a stable, documented way to get token usage out of OpenCode for each session and each message. That could be something like
opencode usage export --session <id> --format jsonor an extension ofopencode stats. You'd also like turn or invocation boundaries, so that usage in a reused session can be attributed to the right run, instead of external tools having to parse the internal SQLite database.For reference, v2 currently has
opencode stats --json, which outputs aggregate totals only, andopencode session export, which dumps the session as JSON through an experimental endpoint. Neither offers a documented per-message usage schema or turn ids, so this is a fair gap to raise.Related issues:
/usagereport, as you noted.opencode statstotals differ from stored session usage, which matters if you rely onstats --jsonin the meantime.stats --days Nomits the cost/token/cache breakdown.The maintainers will consider this request.