Skip to content

About

Know exactly where your OpenCode tokens and money go. A clean dashboard shows token usage, cost, and cache hit rate at a glance — drill down by model or any time window, down to the minute. Data stays fresh with auto-updates. One command and you're done.

Topics

Resources

Stars

4 stars

Watchers

2 watching

Forks

Repository files navigation

OpenCode Usage

OpenCode Usage

See exactly where your OpenCode tokens and money go.

Python 3.11+ Chart.js 4.4 Playwright


OpenCode Usage turns your OpenCode spending into a clean, visual dashboard: token volume, cache hit rate, and cost at a glance, with drill-downs by model and by any time range — from months down to individual minutes.

OpenCode Usage

Features

  • Clear overview — record count, total tokens, and total cost in cards right at the top
  • Cache insights — cache hit rate on every bar, plus an overlay line
  • Full time control — view by month, week, day, hour, or minute, and filter by model
  • Break down by model — tick the option beside Cache hit rate and every model becomes its own colored segment of the same bar; it holds when you switch between tokens and cost, and unticking it falls back to the totals
  • Multiple accounts — register several opencode.ai accounts, each with its own cached login and organization; update one account or all of them, and filter the dashboard by account
  • Always current — incremental updates fetch only what's new (per account), and auto-update keeps the dashboard fresh even while you're away
  • Export on demand — one click builds a spreadsheet from the database; the database stays the single source of truth
  • Two ways to view — tokens or cost, light or dark theme, English or Chinese

Quick Start

cd opencode_usage
pip install -r requirements.txt
python opencode_usage.py

Your browser does not open automatically — visit http://127.0.0.1:9901 (set OPEN_BROWSER=1 to open it on startup).

First update needs a real opencode.ai login: the tool opens a browser at opencode.ai/console/login and waits for you to pass the hCaptcha and OAuth by hand, then caches the session for later runs.

Usage

From the dashboard you can:

  • Click Update to refresh the selected account — or every account at once when All Accounts is selected
  • Pick an Account and a Model, choose the metric and time range, and tick Break down by model or Cache hit rate above the chart
  • Open Settings to add or remove accounts (email, password, and one of GitHub / Google / email magic link), and set the time range and update interval
  • Enable Auto Update to keep data fresh on a schedule, even when the page is closed
  • Click Export Excel to download a spreadsheet generated from the database at that moment

Adding an account opens a browser on the machine running the service so you can finish sign-in by hand — OAuth and hCaptcha, plus clicking the verification link in your inbox when you choose email; after that its session is cached and reused. Each account's identity comes from GET /console/api/user, so every record is tagged with the account it really belongs to — re-submitting the same email just refreshes its password.

To scrape data from the command line instead, run python scrape_usage.py --help (--account all is the default; pass an account id to refresh just that one).

Storage

One store: opencode_usage.db (SQLite). The scraper writes only into it, and the page reads only from it — pre-aggregated buckets grouped by minute × account × model (/api/agg), so the dashboard stays fast as the history grows. Records are keyed by (account_key, id) because a request id is only unique within one account. The store is append-only from the dashboard's point of view: choosing a time range decides what gets written next, it never deletes history you already have.

Need a spreadsheet? Click Export Excel (or GET /api/export.xlsx) — the file is built from the database on demand, with the same columns as before (账号 included, naming the account each request belongs to). It's a report, not a second copy of the data.

Upgrading an older install: the first run migrates the database in place — it adds the account columns and attributes existing rows to the account configured before the upgrade. If the database is still empty while a legacy opencode_usage_history.xlsx exists, that spreadsheet is imported once and then never read again.

Local-only files (gitignored): opencode_accounts.json holds each account's credentials, resolved identity and organization; storage_state_<account>.json holds that account's cached browser session.

License

Released under the MIT License.

About

Know exactly where your OpenCode tokens and money go. A clean dashboard shows token usage, cost, and cache hit rate at a glance — drill down by model or any time window, down to the minute. Data stays fresh with auto-updates. One command and you're done.

Topics

Resources

Stars

4 stars

Watchers

2 watching

Forks

Releases

Packages

Contributors

Languages