Skip to content

Token usage: no stable machine-readable export and no turn boundaries for external consumers #60278

Open

Description

@suale-dev

Summary

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

  1. 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.

  2. 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.

  3. No per-response identity beyond message.id. There is no response-level
    id comparable to other hosts, which limits dedup and cross-checking.

  4. 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.

  5. 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.)

Environment

  • OpenCode v2.0.27
  • macOS, data dir ~/.local/share/opencode/

Activity

  1. opencodejr commented on Oct 29, 2026

    @opencodejr

    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.

    Related issues:

    The maintainers will consider this request.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions