OpenCode

OpenCode token tracker for the CLI

Track OpenCode tokens from the terminal: the stats command, a SQLite query, and a small script that prints a daily total.

Quick answer

For a quick check, run `opencode stats` if your build has it. For anything repeatable, query the SQLite database with `sqlite3` and sum the token columns for a date range. A five-line shell function that prints today's total is usually enough for terminal users.

One command

opencode --help | grep -i stats
opencode stats

The output is a snapshot of the local data set. It does not take a date range in every version, so check the help text before scripting around it.

A repeatable query

sqlite3 ~/.local/share/opencode/opencode.db \
  "select sum(tokens_input), sum(tokens_output), sum(cost) from message where created_at >= date('now','-1 day');"

Column and table names differ between versions. Run .schema message first and adapt the query rather than copying it blindly.

Wrap it in a function

  1. Put the query in a shell function.
  2. Format the output as one line.
  3. Cache it for a minute if you call it from a prompt.
  4. Print tokens and cost together so the number has context.

If you would rather not maintain SQL, UsageAtlas runs the same read and adds history.

Reading totals from the command line

A CLI tracker is the lowest-friction option if you live in a terminal: one command prints totals for a window, and you can pipe the output into a report. The trade-off is formatting and the lack of alerts.

  1. Find the session table and the token columns.
  2. Group by day for a trend, and by model for cost.
  3. Filter by working directory for project totals.
  4. Cache the query if the database is large.
SELECT date(created_at) AS day, SUM(tokens_input), SUM(tokens_output)
FROM sessions
GROUP BY day ORDER BY day DESC;

Column names differ between versions, so inspect the schema first. The stats command guide covers the built-in option.

Questions, answered

Can I track OpenCode tokens in real time?

Near real time. A message is written when a turn completes, so an in-flight response appears after it finishes.

Does the CLI tracker work on Windows?

Yes, with sqlite3 available. Point the query at %APPDATA%\opencode instead of the Linux path.

Is there an official OpenCode token API?

No public usage API. The local database is the supported source.

Browse by topic