<h1 align="center">opencode-token-usage</h1>

<p align="center">Token usage, context window, estimated cost and live performance for OpenCode 2.</p>

<p align="center">
  <a href="https://www.npmjs.com/package/@chenlongapps/opencode-token-usage" target="_blank" rel="noopener noreferrer"><img alt="npm" src="https://img.shields.io/npm/v/%40chenlongapps%2Fopencode-token-usage?style=flat-square&logo=npm" /></a>
  <a href="https://www.npmjs.com/package/@chenlongapps/opencode-token-usage" target="_blank" rel="noopener noreferrer"><img alt="npm downloads" src="https://img.shields.io/npm/dm/@chenlongapps/opencode-token-usage" /></a>
  <a href="https://opencode.ai/" target="_blank" rel="noopener noreferrer"><img alt="OpenCode 2" src="https://img.shields.io/badge/OpenCode-2-5A67D8?style=flat-square" /></a>
  <a href="https://github.com/chenlongapps/opencode-token-usage/actions/workflows/ci.yml" target="_blank" rel="noopener noreferrer"><img alt="CI" src="https://img.shields.io/github/actions/workflow/status/chenlongapps/opencode-token-usage/ci.yml?style=flat-square&branch=main&label=ci" /></a>
  <a href="LICENSE" target="_blank" rel="noopener noreferrer"><img alt="MIT License" src="https://img.shields.io/badge/license-MIT-yellow?style=flat-square" /></a>
</p>

<p align="center">
  <strong>English</strong> |
  <a href="https://github.com/chenlongapps/opencode-token-usage/blob/main/README.zh-CN.md">简体中文</a>
</p>

[![OpenCode Token Usage plugin preview](https://raw.githubusercontent.com/chenlongapps/opencode-token-usage/main/docs/assets/opencode-token-usage-preview.webp)](https://www.npmjs.com/package/@chenlongapps/opencode-token-usage)

### Overview

**opencode-token-usage** is a community plugin that brings token usage, context window occupancy, estimated cost and performance metrics into OpenCode 2's terminal interface.

Use it to see how many tokens a session and its subagents consume, how full the current context window is, and how estimated costs are distributed across models—without leaving your conversation.

### Features

- **Session-tree totals:** Track input, output, reasoning and cache tokens across the session tree, including unopened subagents, without double-counting inherited fork history.
- **Context visibility:** See the viewed session's context usage, latest reported request and estimated prompt composition.
- **Estimated cost by model:** Price recorded calls using their actual models, with a built-in first-party price snapshot as a fallback. Estimates are not provider bills.
- **Performance at a glance:** Monitor tokens per second (TPS), time to first token (TTFT) and the viewed session's own elapsed response time. Live TPS and elapsed-time estimates are marked with `~`.
- **Native terminal UI:** Keep a sidebar panel in the main session and a single-line summary in subagent views; open details only when needed.
- **On-demand details:** Use `/token-usage` for compact or detailed statistics, including per-model usage and rates, without sending a prompt to the model.

---

### Installation

Requires **OpenCode 2** and **Node.js 22+**. See [Compatibility](#compatibility) for verified host versions and the OpenTUI engine caveat.

```bash
opencode plugin add @chenlongapps/opencode-token-usage
```

Restart OpenCode after installation.

### Quick start

1. **View usage:** Open a session. The **Token Usage** panel appears in the native sidebar when `session.sidebar` is set to `auto` and the terminal is wide enough.
2. **Explore details:** Enter `/token-usage` or left-click the panel's **Token Usage** title. Press `d` to toggle compact/detailed mode and Escape to close. Opening details does not send a prompt to the model.
3. **Inspect subagents:** In subagent views, click the live summary above the composer to open the full panel. Closing it does not interrupt the subagent.

For exact definitions and edge cases, see [Detailed usage](#detailed-usage), [Metrics](#metrics) and [price sources and limitations](docs/pricing.md).

Questions about setup, costs, subagents, reasoning speed or privacy? See the [FAQ](docs/faq.md).

### Other installation options

Instead of the command above, add the package to your project's `opencode.json` or `opencode.jsonc`:

```json
{
  "$schema": "https://opencode.ai/config.json",
  "plugins": ["@chenlongapps/opencode-token-usage"]
}
```

For remote sessions, add the package name to `plugins` in your local `~/.config/opencode/cli.json` to load only the terminal entry point. The configuration path follows `XDG_CONFIG_HOME`.

### Compatibility

Requires Node.js 22+ (see the OpenTUI engine note under [Development](#development)). With SDK **2.3.0**, the full packaged integration suite has passed on OpenCode **2.0.28** and **2.3.0**. Earlier SDK/release combinations were tested on 2.0.9–2.0.11, 2.0.22, 2.0.24, 2.0.26 and 2.1.2; those historical results do not establish compatibility with the current SDK. See the [verification record](docs/verification.md) for tested combinations.

Since **0.4.24**, the plugin uses `/token-usage` for details; `/usage` is reserved for OpenCode. Native first-token timestamps improve TTFT and historical TPS when available, with fallbacks for older hosts.

OpenCode **2.3.0 now passes the full suite**, including plugin details alongside built-in `/usage`, native performance timing, history restoration, subagents, forks and compaction. The earlier 6,350 vs 5,080 mismatch was an outdated fixture assumption: background child completion triggers an additional parent model call. Smoke now independently reconciles totals against actual mock-provider requests.

### Detailed usage

Enter `/token-usage` in a session or left-click the sidebar's **Token Usage** title to open the same centered native dialog with the session tree's token totals and estimated cost by recorded model. It opens in compact mode. Scroll with ↑/↓, Page Up/Down, Home/End, press `d` to switch between compact and detailed numbers and reveal model rates, and close with Escape or **esc**. Neither entry sends a prompt to the model. Other sidebar rows remain selectable; dragging to select text does not open the dialog.

The plugin command was renamed from `/usage` to `/token-usage` on all supported hosts to avoid OpenCode's built-in `/usage` command. The old name is not registered as an alias. The **Token usage** command-palette entry is also available.

The subagent picker keeps the summary in the form `Token Usage · Context … · Total … · Cost … · Time … · TPS …`. Time sums the viewed subagent's own native per-turn response durations; token totals and cost still cover the whole tree. Narrow terminals omit lower-priority fields, keeping Time visible when present without adding a second line. Missing values remain unavailable rather than becoming zero, and the line does not advertise a keyboard shortcut. Clicking it opens a smaller dialog with the sidebar's exact rows (including Steps, TPS, TTFT and Run Time) without reserving space for the full panel while closed. When shown, Run Time is the last row in both the sidebar and this dialog.

The dialog is split into five sections with separate statistics:

| Section | Contents |
| --- | --- |
| Context Window | Used / limit and a percentage of the active model's context limit, with a usage bar |
| Last Request | The five token categories and cache rate of the viewed session's most recent reported call |
| Context Breakdown | Estimated prompt composition, largest first, with bars |
| Session | Tree totals: steps, calls, tokens, cache rate, cost (single line, or per-category rows in detailed mode); a separate `Run Time (this session)` row sums only the viewed session's native per-turn response durations |
| By Model | Tokens, calls and cost per recorded model, highest cost first; detailed mode shows the rates used for each model's cost estimate |

One line under the title names the session and its active model, and the body never repeats the model name. Compact mode uses `K`/`M` abbreviations (`812`, `139.4K`, `3.70M`), hides empty rows and merges the two tool families in Context Breakdown into a single `Tools` row. Detailed mode shows exact numbers, empty rows, and the `System Tools` / `MCP Tools` split. The sidebar keeps exact numbers and its own layout.

Costs follow the sidebar's pricing and `partial`/unavailable/free conventions. In detailed mode, By Model shows the applied Input, Output, Reasoning, Cache Read and Cache Write rates in USD per million text tokens. Each rate group names its source (OpenCode or the built-in snapshot), incoming-token tier and call count; a model can have multiple groups. `Incoming` means Input + Cache Read + Cache Write. Missing rates show `—`, confirmed free rates show `$0.00`, and unpriced calls are counted separately. These are the current rates used to estimate recorded calls, not historical provider bills.

**Context Window** requires reported usage and a valid active-model context limit. **Last Request** shows the selected assistant call's reported tokens even when that limit is unknown or source estimates are unavailable. Its timestamp comes from that same message: stream end, completion, then creation time, using the first valid value and hiding the timestamp if none is available. Request selection follows the viewed session's revert cutoff and most recent completed compaction boundary.

**Context Breakdown** separates Messages, System Tools, System Prompt, Skills, MCP Tools, and Other. It estimates the text and tool definitions in the viewed session's **latest assembled model request**; its percentages use the sum of those estimates. Its timestamp is the estimate's own capture time, which may refer to a different request than Last Request. These are not provider-reported token counts and do not add up to the measured context window, which also includes output and reasoning. Media and provider-specific framing cannot be measured from the request. A server plugin records only aggregate estimates (not prompt text); sessions without a captured request or an available server RPC show “Source estimates unavailable”.

### Metrics

| Metric | Definition |
| --- | --- |
| `Input` | Input tokens, excluding cache reads and writes |
| `Output` | Output tokens, excluding reasoning tokens |
| `Reasoning` | Reasoning tokens |
| `Cache Rate` | `Cache Read ÷ (Input + Cache Read + Cache Write)` |
| `Total` | Sum of all five token categories |
| `Context` | Latest context usage after the most recent completed compaction in the viewed session; not aggregated across the subtree |
| `Steps` | Assistant message count across the session tree, including subagents; follows OpenCode's own stats definition, so compaction and user messages are not steps |
| `Est. Cost` | Estimated cost across the tree, pricing each assistant and compaction call with its actual model |
| `Run Time` | The viewed session's cumulative native per-turn response duration; while running, includes a display-only estimate of the current turn's elapsed time marked with `~`; never adds child-session durations |
| `TPS` | Provider-reported average throughput for `Output + Reasoning` across the tree. While streaming, a valid recent observable text, reasoning-summary and tool-input estimate (UTF-8 bytes / 4) takes precedence and is marked with `~`. It requires at least 20 estimated tokens accumulated in that response, three merged batches in the current window and a span of at least 500 ms, excluding pre-first-token waiting. APIs such as OpenAI Responses do not expose complete hidden reasoning tokens in real time, so live values cannot represent hidden reasoning throughput. Completion restores provider-reported throughput without `~` |
| `TTFT` | Average time to first token across measurable assistant steps in the tree, preferring OpenCode's native first-token timestamp |

#### Behavior

- Usage includes server-reported assistant and compaction messages throughout the session tree, including unopened subagents. Token counts are never inferred from text length.
- A fork is a separate session tree. Inherited message copies are attributed only to their original source to prevent double counting.
- `Context` only searches messages after the most recent compaction with `status === "completed"` and is hidden when reliable usage or a model context limit is unavailable.
- `Steps` counts every assistant message in the tree, whether or not it reported usage, and reuses the fork-copy de-duplication so inherited history is never counted twice.
- `Run Time` follows OpenCode 2.0.26's native response footer: assistant completion time minus the first user/synthetic input's creation time after the previous idle marker. A new session whose native time metadata records no previous idle uses the first input even before its first idle marker: synthetic deliveries and steers do not reset that turn. Older histories without idle markers use the nearest input; missing session metadata preserves this legacy fallback. Without any input, it uses the assistant's creation time. Each turn contributes only its final completed response duration, not the overlapping duration of every assistant step. Model calls, tools, retries and foreground waits within that span are included; idle time between turns is not. Waiting for a subagent is already part of the parent's span and is never added separately.
- While the viewed session is running, `Run Time` estimates **elapsed time so far**, not final or remaining time, and prefixes the cumulative display with `~` (for example, `~42.5s`). A shared 500 ms clock replaces the current turn's already-counted duration rather than adding an overlapping steer/retry duration. Clock ticks only update memory: history reads remain event-driven. One optional read-only server-plugin clock RPC at startup/reconnection supplies a server-time anchor, with timestamped live events as a fallback; local monotonic time advances it without assuming that a remote server shares the client's clock. Opening an already-running session restores the turn's start from history. A reconnect history refresh arriving before clock resynchronization keeps the last trusted display for the same retained open turn and unchanged historical subtotal, rather than dropping it to zero. Without a trusted anchor or valid timing, native totals remain unchanged.
- Completion, failure or interruption stops projection; reported native completion timing replaces the estimate and removes `~`, so a small downward correction is possible. Only native completed durations enter the historical subtotal. Totals use unrounded milliseconds before formatting (milliseconds below one second, one decimal below one minute, then minutes/hours), so they need not equal the sum of rounded footer labels. The existing message-history read restores totals after restarts, including sessions predating installation; no experimental execution-log API or server plugin is required. Missing historical timing data displays `—`, not zero. Read failures freeze the last displayed value and mark it `Not updated` (`stale` in the short summary).
- `Run Time` / `Time` shows the duration and its estimate/freshness markers, without running-status text or animation. An idle parent's time never advances just because a child is active. Closing or switching views cleans up their estimates, and the shared elapsed-time ticker stops when no view needs it. Unavailable/stale timing keeps its own marker.
- A confirmed zero `Run Time` / `Time` is hidden in the sidebar, subagent summary/dialog and both `/token-usage` modes, without leaving an empty row or field. A nonzero live estimate makes the first turn visible even before any response has completed. Nonzero millisecond durations, unavailable values and stale timing remain visible.
- Runtime follows retained history: agent/model switches and compaction preserve past turns; fork copies never contribute to the fork's own time. A staged revert does not remove turns, but a committed revert deleting messages also removes their durations. Deleted historical timing cannot be reconstructed.
- `Est. Cost` prices every message with its recorded model. A complete non-zero price resolved by OpenCode takes precedence. If OpenCode reports a complete zero price, a complete first-party snapshot price overrides it; incomplete prices fall back for the whole message without mixing rates.
- The checked-in fallback snapshot is generated from [models.dev](https://models.dev/api.json) using only reviewed first-party provider/model families; a small set of manufacturer-verified exceptions is kept separately. It covers priced text models, without downloading prices while the plugin runs. Gateway models match exact manufacturer IDs, documented aliases, and known wrappers; only a terminal `-free` or `:free` can be removed for a second exact lookup.
- Confirmed free usage displays `$0.00`; positive estimates below `$0.001` display `<$0.001`, estimates through and including `$0.01` use three decimal places, and larger estimates use two. Unavailable prices display `—`; known subtotals with unpriced messages are marked `partial`. The snapshot excludes gateway markups, regional premiums, unlisted discounts, non-text billing, tool fees, and taxes, so Est. Cost is not a provider bill. See [price sources and limitations](docs/pricing.md).
- Live `TPS` uses a ~2s sliding window and batches deliveries within 100 ms. It starts calculating and displaying only when all three conditions hold: that response has accumulated at least 20 estimated tokens (80 UTF-8 bytes), the current window contains at least three merged batches, and those batches span at least 500 ms. The first batch counts toward the response-wide threshold but is only a speed baseline: its bytes are not counted as output generated after its timestamp. Token readiness is separate for each response, never pooled across responses or subagents. Smoothing follows elapsed time rather than delta count. Before readiness, historical TPS takes over, or the row is hidden if unavailable. After 2s without a new delta, a shared in-memory timeout expires the estimate without reading history. Resuming output after expiry starts a fresh baseline and smoothing state and must satisfy the batch/span conditions again; the same response's accumulated token count is retained.
- Reconnection resets live TPS windows and reconciles all tracked sessions, including offscreen sessions, with the server's active-session set. Quiet unfinished streams get a shared activity check after 60s, without reading message history. Confirmed inactive streams and their TTFT observations expire after a further 60s. Running tools, retries and pre-first-token waits have no hard lifetime; failed activity reads retain state and retry. Provider `streamed` metadata alone never means that tools have finished. Checks stop when no unfinished streams remain.
- Live TPS measures observable delivery, not the model's internal token-generation clock. Responses summary bursts, done-only content and transport buffering can still affect estimates; complete hidden reasoning cannot be reconstructed. Without `~`, TPS is `Σ(Output + Reasoning) ÷ Σ(time.streamed − (time.firstToken ?? time.created))` in seconds across valid assistant samples in the tree. Valid native first-token timing excludes pre-first-token waiting, matching the host's per-step denominator; older hosts/history without it retain the full request-duration fallback. This is a tree-wide weighted average, not just the current turn or recent delivery window. No model-specific multipliers, hard TPS caps or hidden-reasoning guesses are applied.
- `TTFT` prefers native `time.firstToken − time.created` in history and `session.step.first-token` during streaming. A buffered delta cannot overwrite native timing. Older hosts fall back to the first nonempty text/reasoning/tool-input delta, or historical reasoning/tool creation times when measurable; text-only history without a native first-token timestamp has no recoverable TTFT. Native timing survives reopening and live-observation expiry. Retries reusing a message ID replace the previous attempt's sample. TTFT remains the average across measurable steps in the whole tree, whereas OpenCode's footer uses the first measurable step in the current turn, so multi-step/subagent values need not match.
- Initial read failures display `Unavailable`. Later failures retain the last complete snapshot, display `Not updated`, and retry automatically.

### Development

```bash
npm ci
npm run lint
npm run typecheck
npm test
npm run build
npm run test:smoke
```

Lint uses ESLint 10 Flat Config with the JavaScript/TypeScript recommended rules, without formatting or React rules. It covers source, tests, scripts, and root JavaScript files, excluding build output, dependencies, temporary directories, and the generated price snapshot. Use `npm run lint:fix` for supported automatic fixes. On Node 22, ESLint requires >=22.13; Node >=24 is also supported. This development-tool requirement does not change the plugin's Node.js 22+ runtime minimum.

For native TTFT/TPS checks on a host with `time.firstToken`, run `npm run test:smoke -- --performance-only`. This targeted lane verifies streaming timing, the native footer and history restoration after reopening; it does not replace the full integration suite.

The OpenCode SDK packages are pinned to `2.3.0`, with OpenTUI `0.8.4` and Solid/compiler `1.9.16`. OpenTUI declares Node.js `>=26.4.0` for its native Node runtime, so npm reports `EBADENGINE` on Node 22; `engine-strict` installations require a Node version meeting that dependency's engine requirement. The plugin's TUI runs inside OpenCode, not as a standalone Node 22 renderer. The project's Node.js 22+ minimum is unchanged; see the [verification record](docs/verification.md) for checks of this dependency set.

The [price update workflow](.github/workflows/update-prices.yml) checks models.dev every 4 hours (or on demand via **Run workflow** on `main`). No snapshot diff means no release. Changes within the reviewed first-party source allowlist pass lint, typecheck, tests, build, and pack validation, then automatically bump the patch version (for example, `0.4.4` → `0.4.5`), commit only `src/prices.generated.ts`, `package.json`, and `package-lock.json`, and atomically push `main` and its version tag. The workflow creates a GitHub Release and explicitly dispatches [`publish.yml`](.github/workflows/publish.yml) on that tag to publish via npm Trusted Publishing/OIDC; no long-lived npm token is needed. GitHub Actions must be allowed to push to `main` and create tags, and npm must trust `publish.yml` with permission to run `npm publish`.

Failed validation never bumps the version. An unfinished tagged price release is retried with the same version before checking new prices; registry outages are not treated as missing versions. Publishing skips versions already on npm, prevents moving `latest` backwards, and polls registry visibility for up to 10 minutes after publication, logging progress. A verification timeout is not proof that `npm publish` failed: check the registry and retry the same tag without bumping the version; already-published versions are not republished. Manufacturer/source allowlist additions, aliases, and manually verified price exceptions still require human review. See the [release guide](docs/releasing.md) for setup, verification timing, and retries.

For a manual refresh, run `npm run prices:update` in a networked environment, review the generated diff and exceptions, then run the checks above. Builds and plugin refreshes do not contact models.dev.

`test:smoke` packages the real artifact and validates loading, refreshes, `/token-usage`, sidebar title clicks, subagent aggregation, per-message pricing, first-party price fallback, model switching, TPS, TTFT, Responses summary bursts/done-only summaries/quiet expiry, and session-local native turn durations (unfinished responses, first-turn synthetic continuations, reopening a running TUI, terminal focus events, idle gaps, interruption, message-history restoration, forks and narrow child views) against an isolated OpenCode instance and local mock providers. On OpenCode 2.3+, it also checks that built-in `/usage` opens the host's own dialog. It requires Python 3, an available local port, and npm network access. It never modifies your existing OpenCode configuration or calls paid models. Terminal focus checks do not establish physical macOS multi-display, App Nap or sleep/wake behavior.

Performance lifecycle checks also use the real host's active-session API, with ending events withheld from an offscreen monitor, injected reconnect handshakes and an accelerated 25-hour tool/child wait. They verify preservation while running and reclamation after completion without history reads.

By default, smoke uses `opencode` from `PATH`. Stable OpenCode 2 versions from 2.0.9 onward can run; a host not yet verified with the current SDK prints a warning and continues through the actual compatibility checks. Passing the version check alone does not establish compatibility. The SDK dependencies remain pinned independently of your local CLI updates.

CI configures full packaged smoke jobs for pinned OpenCode **2.0.28** and **2.3.0**, using isolated local mock providers and uploading diagnostic artifacts. Tree totals are checked against actual mock-provider requests: background child-completion notifications can trigger an additional parent call on newer hosts.

To reproduce a baseline with a separately installed binary, without downgrading your normal installation:

```bash
OPENCODE_BIN=/absolute/path/to/opencode-2.0.28 npm run test:smoke
```

`OPENCODE_BIN` selects the same executable for the version check, isolated server, and TUI. Relative paths resolve from the directory where you run the command. The host version and selected binary are recorded in the smoke output and `result.json`; see the [verification record](docs/verification.md) for tested combinations.

To load the plugin from source, build the project and add the repository's absolute path to `plugins` in the target project.

### Documentation

- [FAQ](docs/faq.md) · [简体中文](docs/faq.zh-CN.md)
- [Roadmap](ROADMAP.md)
- [Verification record](docs/verification.md)
- [Release guide](docs/releasing.md)
- [OpenCode 2 plugin documentation](https://opencode.ai/v2/docs/build/plugins/)
- [OpenCode 2 CLI plugin API](https://opencode.ai/v2/docs/build/plugins/cli/)

The implementation follows OpenCode's [TokenUsage schema](https://github.com/anomalyco/opencode/blob/v2/packages/schema/src/token-usage.ts), [cost calculation](https://github.com/anomalyco/opencode/blob/v2/packages/core/src/session/usage.ts), and [fork history projection](https://github.com/anomalyco/opencode/blob/v2/packages/core/src/session/projector.ts).

### Disclaimer

This is an independent community plugin. It is not built, maintained, or endorsed by the OpenCode team.

### License

[MIT](LICENSE)
