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 statsThe 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
- Put the query in a shell function.
- Format the output as one line.
- Cache it for a minute if you call it from a prompt.
- 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.
- Find the session table and the token columns.
- Group by day for a trend, and by model for cost.
- Filter by working directory for project totals.
- 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.