Skip to content

About

A QoL feature for OpenCode to show remaining credits with various providers (widget + notification).

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Repository files navigation

opencode-usage-tui

A persistent OpenRouter credits widget and notifier for OpenCode v2 TUI plugins.

Renders a compact, always-visible line into the prompt.footer.status slot showing your API credit balance, usage, and reset period. It refreshes automatically, paints instantly from a persisted snapshot, degrades to a muted state on network failure, and notifies you when credits run low.

Important

This plugin targets opencode v2 (prompt.footer.status slot). V1 TUI plugins do not run in v2, and this version does not run in opencode v1.

  • On opencode 1.x, install the last v1-compatible release instead: opencode-usage-tui@0.4.x (config in tui.json(c) with the v1 "plugin" array / tuple form).
  • On opencode 2.0.x plugin directives live in cli.json; tui.json(c) is a leftover v1 TUI-config file and is not what plugins are loaded from. Local plugins must be registered as a plugin package directory (a folder with a tui.js entrypoint) via a string path — see Install.

Screenshots/Examples

OpenCode V1

Widget:

widget example

Verbose mode:

verbose mode example

OpenCode V2

Widget homepage

widget home page

alt text Widget

widget

Verbose mode: homepage

verbose mode homepage

Verbose mode: chat

verbose mode chat

Features

  • Persistent prompt.footer.status widget: a compact status cell in the session prompt's footer, so it stays visible while you work (append-mode slot, coexists with OpenCode's internal status).
  • 15-minute auto-refresh: auto-updates to keep you informed. No manually checking a command to keep up to date.
  • Instant paint: the last-known snapshot is seeded from the durable store before the first network round-trip, and written back after each successful fetch.
  • Low-credit notification: notifies when remaining balance drops below a configurable threshold.
  • Toast on first fetch / tier change: visual feedback when the status line changes tier or first loads.
  • Muted failure state: missing key, API errors, or parse failures degrade to Credits · ⚠ unavailable.
  • Configurable: refresh interval, endpoint, low-credit threshold, and auth file path via plugin options.
  • Widget insights (verbose): opt-in verbose mode adds a days-left estimate, free-model daily requests remaining, and weekly/monthly usage.
  • Secure: No key is ever hardcoded in the plugin; it is read at runtime and sent only to the configured provider endpoint as a Bearer token.

Requirements

  • OpenCode v2 (npm plugins are auto-installed by Bun into OpenCode's plugin cache at startup).
  • An OpenRouter API key. The plugin reads it from auth.json in OpenCode's data dir, which OpenCode itself writes:
OS data dir (auth.json lives here)
macOS ~/.local/share/opencode/auth.json
Linux ~/.local/share/opencode/auth.json (or $XDG_DATA_HOME/opencode/auth.json when set)
Windows %USERPROFILE%\.local\share\opencode\auth.json

[!NOTE] macOS OpenCode uses the same ~/.local/share/opencode paths on macOS as on Linux — not ~/Library/Application Support (that only applies to admin-managed config at /Library/Application Support/opencode/). Finder hides dotfiles — press Cmd+Shift+. to reveal ~/.local/….

{
	"openrouter": {
		"type": "api",
		"key": "sk-or-v1-..."
	}
}

Install

Add the package to the plugins array in your OpenCode v2 config — ~/.config/opencode/cli.json on macOS/Linux, %USERPROFILE%\.config\opencode\cli.json on Windows (.jsonc files load too). The npm package form loads the widget from the plugin cache; to run from this checkout instead, use the local-directory form — see From a local build:

{
	"plugins": [
		{
			"package": "opencode-usage-tui",
			"options": {
				"refreshIntervalMs": 1800000,
				"lowThreshold": 5
			}
		}
	]
}

Restart OpenCode. The widget should now render in the home footer status row of the TUI. Alternatively, install it from the OpenCode CLI: opencode plugin add opencode-usage-tui --global.

From a local build (development)

To run the widget straight from this checkout on opencode 2.0.x, register it as a local plugin package directory. opencode only discovers local plugins that are directories with a tui.js entrypoint referenced by a string path — a raw .js file, or a file path inside the object form, is ignored.

Create a dispatcher entry under your global config directory (absolute path shown for Windows; adapt per OS):

// ~/.config/opencode/usage-tui/tui.js
export { default } from "C:/absolute/path/to/opencode-usage-tui/dist/tui.js";

then register the directory (not the file) in cli.json — the object form still passes options:

{
	"plugins": [{ "package": "./usage-tui", "options": { "verbose": true } }]
}

Restart OpenCode (or let its config watcher reload the plugin). dist/tui.js is dependency-free apart from @opentui/solid, so it loads from the config directory without adding the v2 SDK to ~/.config/opencode/package.json.

Where opencode stores files

macOS Linux Windows
data dir (auth.json) ~/.local/share/opencode ~/.local/share/opencode ($XDG_DATA_HOME wins) %USERPROFILE%\.local\share\opencode
config (cli.json) ~/.config/opencode ~/.config/opencode ($XDG_CONFIG_HOME wins) %USERPROFILE%\.config\opencode
npm plugin cache ~/.cache/opencode/node_modules ~/.cache/opencode/node_modules %USERPROFILE%\.cache\opencode\node_modules

Options

Pass options in the object form of the plugins array (works for npm packages and local-directory packages alike):

{
	"plugins": [
		{
			"package": "opencode-usage-tui",
			"options": {
				"refreshIntervalMs": 1800000,
				"lowThreshold": 5,
				"authPath": "/Users/me/opencode/auth.json" // e.g. C:\Users\me\opencode\auth.json on Windows
			}
		}
	]
}
Option Type Default Description
refreshIntervalMs number 900000 (15 min) How often to re-fetch the credits snapshot.
endpoint string https://openrouter.ai/api/v1/auth/key OpenRouter endpoint to query.
lowThreshold number 10 Remaining balance (in USD) below which a low-credit notification fires. Set to 0 to disable.
verbose boolean false Show extended info: days-left estimate, free-model requests remaining, and weekly/monthly usage.
authPath string $XDG_DATA_HOME/opencode/auth.json, else ~/.local/share/opencode/auth.json Absolute path to the auth.json holding openrouter.key. The default resolves the same on every OS (OpenCode does not use ~/Library on macOS) — no tilde expansion is performed, so pass an absolute path when overriding (e.g. /Users/me/opencode/auth.json, C:\Users\me\opencode\auth.json).
envVar string OPENROUTER_API_KEY Environment variable holding the OpenRouter API key. Read first — when set it takes precedence over auth.json; unset or empty falls back to authPath.
provider string openrouter Which provider to poll: openrouter (credit balance) or weave (hosted Weave Router spend). Anything else falls back to openrouter.
apiKey string "" Weave only. The provisioned read-only analytics key (ra_…), sent as Authorization: Bearer <apiKey>. No key means the Weave widget shows unavailable.
baseUrl string https://router.workweave.ai Weave only. Base URL of the hosted Weave Router instance to poll.

All monetary values shown by the widget are in USD — OpenRouter reports credit balances in USD, and Weave rows are costed in USD.

OpenRouter (usage)

The default provider shows your OpenRouter credit balance. The API key is read at runtime from the OPENROUTER_API_KEY environment variable, falling back to auth.json (opencode writes it when you log in):

  • The plugin reads the key from OPENROUTER_API_KEY first; if it is unset or empty it reads openrouter.key from auth.json at runtime — it is never hardcoded. Override the env var name with envVar, or the file location with authPath.
  • Each refresh calls GET {endpoint} (default https://openrouter.ai/api/v1/auth/key) with the key as a Bearer token and parses limit, limit_remaining, limit_reset, and the usage_* fields.
  • The line reads Credits · 🟢 $75.00 / $100.00 (75%) · weekly (emoji tier by % remaining: green >50%, yellow >25%, orange >10%, red below). With "verbose": true it appends a days-left estimate, free-model requests remaining, and weekly/monthly usage.
  • Below lowThreshold (default $10) a low-credit notification fires, deduplicated until credits recover. Set lowThreshold to 0 to disable it.
  • Failures degrade to Credits · ⚠ unavailable.
{
	"plugins": [
		{
			"package": "opencode-usage-tui",
			"options": {
				"endpoint": "https://openrouter.ai/api/v1/auth/key",
				"lowThreshold": 10,
				"authPath": "/Users/me/opencode/auth.json",
				"verbose": false
			}
		}
	]
}

Weave Router (usage)

The widget can also show monthly spend from a hosted Weave Router instance instead of an OpenRouter credit balance. Weave is a spend model (it tracks cost, not a pre-funded pool), so there is no balance/limit/reset — the widget polls the analytics export and sums per-request cost.

  • Set "provider": "weave" and pass your read-only analytics key as "apiKey" (an ra_… key, provisioned in the Weave dashboard/setup flow — it can read analytics but cannot route or infer). The key is sent only to the configured baseUrl as a Bearer token.
  • The widget fetches GET {baseUrl}/v1/analytics/routing-decisions?since=<month-start>&limit=1000, cursor-paginates the NDJSON export, and aggregates actual_input_cost_usd + actual_output_cost_usd per row.
  • The line reads Spend · $12.34 · 850 req (month-to-date spend and request count), and once a second sample is available it appends an estimated · ≈$2.1/day burn rate.
  • Weave has no low-credit alert: the lowThreshold notification is balance-mode (OpenRouter) only. Failures degrade to Credits · ⚠ unavailable just like OpenRouter.
{
	"plugins": [
		{
			"package": "opencode-usage-tui",
			"options": {
				"provider": "weave",
				"apiKey": "ra_...",
				"baseUrl": "https://router.workweave.ai"
			}
		}
	]
}

Development

bun install
bun run build           # Bun.build (Solid transform) → dist/tui.js (dependency-free bundle)
bun run typecheck       # tsc --emitDeclarationOnly
bun test                # unit tests for the pure helpers + the fake-context plugin tests
npm pack --dry-run      # inspect the publish tarball

bun run build emits a dependency-free bundle: dist/tui.js imports only @opentui/solid (plus Node built-ins), and the @opencode/plugin/tui import is type-only — export default { id, setup }. That lets the widget run as a local plugin directory without the v2 SDK present in ~/.config/opencode/node_modules.

The durable snapshot lives under opencode's state dir at latest/tui/plugin.usage.snapshot.json (segment plugin.usage.<key>). Plugin storage keys must match ^[a-zA-Z0-9][a-zA-Z0-9._-]*$ — keep STORE_KEY / the storage key free of : (usage-tui:snapshot is rejected with Invalid storage segment); the widget uses snapshot.

The source is split so the pure logic is testable without the TUI/Solid runtime:

opencode-usage-tui/
├── src/
│   ├── tui.tsx         # v2 plugin entry: export default { id, setup } → prompt.footer.status widget
│   ├── tui.test.ts     # fake-context tests (no JSX execution)
│   ├── core.ts         # Core logic: fetch, auth, parse, format, options, theme mapping
│   ├── core.test.ts    # bun test unit tests
│   └── providers/      # openrouter + weave providers
├── build.ts            # Bundles src/tui.tsx with @opentui/solid's transform
├── package.json
├── tsconfig.json
├── README.md
└── LICENSE

License

MIT. See LICENSE.

About

A QoL feature for OpenCode to show remaining credits with various providers (widget + notification).

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Used by

Contributors

Languages