Openrouter Budget
OpenCode plugin that surfaces real OpenRouter spend: per-request costs in the session sidebar, the model the router picked, key budget, and account credits
0
0
20.0
Multi-signal model
1 day ago
2026-10-19
Install and configure
opencode.jsonWrites to this project's opencode.json — applies to this repository only.
opencode.json
{
"$schema": "https://opencode.ai/config.json",
"plugin": ["opencode-openrouter-budget@0.3.0"]
}Writes to ~/.config/opencode/opencode.json — applies to every project.
~/.config/opencode/opencode.json
{
"$schema": "https://opencode.ai/config.json",
"plugin": ["opencode-openrouter-budget@0.3.0"]
}If you want to modify the plugin locally, install it into the project and reference the local path.
shell
pnpm add -D opencode-openrouter-budgetOpenCode loads npm dependencies through its embedded runtime on startup and caches them locally — no manual global install needed.
An OpenCode plugin that shows what you are actually spending on OpenRouter — live, per session, from OpenRouter's own numbers.
OpenCode computes its built-in cost figure from static catalog prices. That
works for fixed models, but routing aliases such as openrouter/auto have no
catalog price (the router picks a different model per request), so OpenCode
shows $0.00. This plugin replaces the guesswork with authoritative data:
it reads usage.cost from every OpenRouter response and reports real spend.
What it does
Session sidebar — while the active session uses an OpenRouter model, a block appears in the right sidebar:
OpenRouter
Session $0.1832 · 3 req
Last: z-ai/glm-5.3 · $0.0581
$70.31 key left (weekly)
Global: $392.08 left
Session— real charged cost of this session (cache discounts included)Last— the model the Auto Router actually picked for the last request, with what it costkey left— the key's remaining budget and its reset period, fromGET /api/v1/key(limit_remaining,limit_reset)Global— remaining account credits, shown only when the API key can read them (e.g. the workspace admin). Member keys see nothing here.
The block is hidden entirely when the session is not talking to OpenRouter,
and shows waiting for first request… until the first response completes.
/openrouter-budget command — a dialog with the full picture: every captured request
(time, kind, routed model, in/out tokens, cost, generation ID), the session
total, and the wallet block — key label, key budget, account credits, and
fetch time. Press r to refresh the wallet, Esc to close.
Install
Add the package to the plugins array in your opencode.json(c) — OpenCode
downloads and loads it automatically:
{
"$schema": "https://opencode.ai/config.json",
"plugins": ["opencode-openrouter-budget"],
}
You can also pin a version:
{
"plugins": ["opencode-openrouter-budget@0.1.0"],
}
Requirements
- OpenRouter must be connected (via
/connector theOPENROUTER_API_KEYcredential). The plugin resolves the key from OpenCode's credential store at runtime; it never asks for one itself. - The sidebar block and capture only apply to the
openrouterprovider.
How it works
- The server plugin hooks OpenCode's
http.responsefor theopenrouterprovider and tees the response stream (fail-open: the model stream is never at risk). It parsesusage.cost, the routedmodel, token counts, and the generation ID from the final SSE chunk, and keeps a per-session ledger in plugin storage. - The wallet poller calls
GET /api/v1/key(key budget, reset period, lifetime usage) andGET /api/v1/credits(account credits; silently skipped when the key cannot read them). It runs at startup, every 5 minutes, and on demand from the/openrouter-budgetdialog. - OpenRouter responses are decoded with Effect Schema (the same schema library OpenCode's runtime provides to plugins): lenient, so shape drift degrades to missing data instead of hard failures. The RPC contract between the server and TUI halves is defined with the same schemas, exposed through Standard Schema v1.
- The TUI plugin subscribes over the plugin RPC channel and renders the
sidebar block and the
/openrouter-budgetdialog.
Privacy
Your API key is resolved from OpenCode's credential store and used only to
call openrouter.ai endpoints listed above. Nothing else leaves your
machine; the plugin collects no telemetry and reports nothing anywhere else.
Limitations
- The ledger starts when the plugin is first installed; spend from before installation is not reconstructed.
- Only requests made through this OpenCode instance are counted.
- Auxiliary requests (title generation, compaction) are counted too, and
labeled by kind in the
/openrouter-budgetdialog. - OpenCode's built-in
$0.00figure foropenrouter/autoremains — this plugin adds its own, labeled block beside it.
Development
pnpm install
pnpm run check # format:check + lint + typecheck + test
To hack on it live, point OpenCode at a local checkout instead of the package:
{
"plugins": ["/absolute/path/to/opencode-openrouter-budget"],
}
Local-directory loading resolves server.ts, tui.ts, and rpc.ts at the
package root, so keep those shims in sync with src/. OpenCode reloads
plugin files on save.
License
Similar plugins
Openrouter Costs
opencode-openrouter-costs
OpenCode TUI plugin — sidebar widget showing OpenRouter session cost and account balance
Oc Usage
@taraksh011/oc-usage
OpenCode plugin — live token usage, cost, and TPS display
Hard Limit
opencode-hard-limit
OpenCode plugin that hard-stops model calls before you burn through your AI quota. Monitors the 5h or weekly window for Claude and OpenAI, blocks at a configurable percent-remaining threshold, and shows a live usage bar in the sidebar. Set your goal per-p