0
56
近 7 天 12
30.8
生态多维模型
6 小时前
2026-10-21
快速安装与配置
opencode.json写入当前项目的 opencode.json,只对这个仓库生效。
opencode.json
{
"$schema": "https://opencode.ai/config.json",
"plugin": ["opencode-cost@0.2.0"]
}写入 ~/.config/opencode/opencode.json,对所有项目生效。
~/.config/opencode/opencode.json
{
"$schema": "https://opencode.ai/config.json",
"plugin": ["opencode-cost@0.2.0"]
}若你要在本地改造这个插件,先装到项目里再从本地路径引用。
shell
pnpm add -D opencode-costOpenCode 启动时会通过内嵌运行时自动加载 npm 依赖并缓存至本地目录,无需手动在全局环境执行安装。
Persistent cost and token tracking with a native OpenCode TUI dashboard.
/cost opens a full-screen dashboard with date, project, and model filters. Costs come directly from OpenCode's assistant and completed-compaction records, so this plugin does not maintain a separate provider pricing table.

Published on npm as opencode-cost.
Requirements
- OpenCode 2.1.1 or newer within the 2.x release line
- Bun, which is already used by OpenCode's plugin runtime
The TUI plugin API is new and version-coupled. This package declares an engines.opencode range so incompatible OpenCode versions skip it rather than loading an unsupported interface.
Install
Install globally so usage from every project is recorded:
opencode plugin add opencode-cost
The package exposes a V2 server plugin at its main entrypoint and a native dashboard at ./tui. OpenCode automatically loads both from the global server configuration:
~/.config/opencode/opencode.jsonoropencode.jsonc
For a local checkout, add its absolute directory to plugins in that file after building it. Watched configuration changes reload automatically; restart OpenCode if a local build is not picked up.
Then run:
/cost
Dashboard
d: choose today, 7 days, 30 days, or all timep: filter by projectm: filter by provider/modelx: reset filtersr: refresh immediatelyesc: return to the session or home screen that opened the dashboard
The screen also refreshes automatically while it is open.
Storage
The server plugin stores an idempotent SQLite ledger at:
<opencode-state>/opencode-cost/cost.sqlite
Each usage record is keyed by its OpenCode message ID. Repeated message updates replace the existing values instead of double-counting cost.
Recorded costs remain in the ledger if their original OpenCode session is later deleted. To clear all history, quit OpenCode and remove the database file.
Existing sessions
The first time a project is opened in the terminal with the plugin enabled, its existing sessions are backfilled, including paginated history before compaction and sessions in other worktrees of the same project. A project is marked complete only after every session was read successfully; interrupted imports retry on the next startup. Projects that are never opened after installation are imported when they are next opened.
In V2, historical backfill runs in the terminal plugin because the server plugin context exposes only messages after the latest compaction. Live usage is still collected by the server, including headless sessions. Open the project in the terminal to import any older usage.
The dashboard and collector share a local SQLite file; a dashboard connected to a remote server does not read that server's ledger.
Custom Database Path
Both plugin targets accept a database option. Configure it in opencode.jsonc; the automatically loaded dashboard receives the same options:
{
"$schema": "https://opencode.ai/config.json",
"plugins": [
{ "package": "opencode-cost", "options": { "database": "/absolute/path/cost.sqlite" } }
]
}
A relative path is resolved from OpenCode's state directory.
Upgrading from V1
This version uses the OpenCode V2 plugin API and no longer loads in OpenCode 1.x. Use the V2 plugins configuration shown above and remove any old explicit opencode-cost entry from tui.json; V2 uses cli.json for terminal-only plugins, but this package's dashboard is loaded automatically with its server plugin.
The SQLite path and schema are unchanged. Existing recorded usage is preserved without conversion.
Development
The project pins Bun 1.4.3 with mise and packageManager in package.json.
mise trust
mise install
bun install
bun run typecheck
bun run build
bun test
To load a local checkout, reference its absolute directory in plugins in opencode.jsonc, then restart OpenCode if needed.
Data Model
The ledger records:
- OpenCode project, directory, session, and message IDs
- Provider and model IDs
- Message creation and completion timestamps
- Provider-calculated USD cost
- Input, output, reasoning, cache-read, and cache-write tokens
No prompts, responses, tool arguments, file contents, or credentials are stored.
同类生态推荐
Oc Usage
@taraksh011/oc-usage
OpenCode plugin — live token usage, cost, and TPS display
Usage Panel
opencode-usage-panel
Sidebar token-usage and cost panel for the opencode TUI. Breaks a session down by model with per-model cost, context usage, and configurable peak/off-peak pricing.
Openrouter Costs
opencode-openrouter-costs
OpenCode TUI plugin — sidebar widget showing OpenRouter session cost and account balance