opencode-token-tracker 1.6.5 → 1.7.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +77 -14
- package/README.zh-CN.md +149 -13
- package/dist/bin/opencode-tokens.js +531 -111
- package/dist/index.d.ts +0 -3
- package/dist/index.js +9 -133
- package/dist/lib/shared.d.ts +33 -0
- package/dist/lib/shared.js +219 -28
- package/package.json +11 -4
- package/dist/test/cli.test.d.ts +0 -1
- package/dist/test/cli.test.js +0 -55
- package/dist/test/shared.test.d.ts +0 -1
- package/dist/test/shared.test.js +0 -536
package/README.md
CHANGED
|
@@ -23,8 +23,18 @@ This project uses the [AI Engineering Framework (AIEF)](https://github.com/tongs
|
|
|
23
23
|
|
|
24
24
|
If you are building AI-assisted engineering workflows, we strongly recommend adopting AIEF in your own repositories for clearer context management and more consistent agent outputs.
|
|
25
25
|
|
|
26
|
+
|
|
27
|
+
## Accuracy & Limitations
|
|
28
|
+
|
|
29
|
+
- **Costs are estimates**, computed locally from your token logs and the built-in (or user-configured) pricing table. They may differ from your provider's official invoice — for example, when promotional credits, discounts, or enterprise pricing structures apply.
|
|
30
|
+
- **Budgets are warnings, not enforcement.** This plugin does not block API calls, throttle requests, or interrupt active sessions. It is designed purely as an observability and tracking tool.
|
|
31
|
+
- **Subscription or bundled providers** (such as GitHub Copilot, Cursor, etc.) or free local models should be configured with zero-cost overrides in your configuration file (see [Configuration](#configuration)).
|
|
32
|
+
- **Pricing freshness**: The built-in pricing table is manually maintained. Please run `opencode-tokens models` to inspect which of your used models currently fall back to the default pricing, and configure overrides if necessary.
|
|
33
|
+
|
|
26
34
|
## Installation
|
|
27
35
|
|
|
36
|
+
### Plugin
|
|
37
|
+
|
|
28
38
|
Add to your OpenCode config file (`~/.config/opencode/opencode.json`):
|
|
29
39
|
|
|
30
40
|
```json
|
|
@@ -36,8 +46,40 @@ Add to your OpenCode config file (`~/.config/opencode/opencode.json`):
|
|
|
36
46
|
|
|
37
47
|
Restart OpenCode and the plugin will be automatically installed.
|
|
38
48
|
|
|
49
|
+
This installs the package for OpenCode's plugin runtime. It does not add the
|
|
50
|
+
`opencode-tokens` CLI command to your shell `PATH`.
|
|
51
|
+
|
|
52
|
+
### CLI
|
|
53
|
+
|
|
54
|
+
If you only need to run the CLI occasionally, use it through npm without a
|
|
55
|
+
persistent install:
|
|
56
|
+
|
|
57
|
+
```bash
|
|
58
|
+
npx -y --package opencode-token-tracker opencode-tokens today
|
|
59
|
+
```
|
|
60
|
+
|
|
61
|
+
Equivalent `npm exec` form:
|
|
62
|
+
|
|
63
|
+
```bash
|
|
64
|
+
npm exec --yes --package opencode-token-tracker -- opencode-tokens today
|
|
65
|
+
```
|
|
66
|
+
|
|
67
|
+
If you want `opencode-tokens` to be available as a normal shell command, install
|
|
68
|
+
the package with npm's global bin linking:
|
|
69
|
+
|
|
70
|
+
```bash
|
|
71
|
+
npm install -g opencode-token-tracker
|
|
72
|
+
opencode-tokens today
|
|
73
|
+
```
|
|
74
|
+
|
|
75
|
+
If `opencode-tokens: command not found` appears after configuring the plugin,
|
|
76
|
+
the plugin is still installed for OpenCode, but the CLI command has not been
|
|
77
|
+
installed into your shell `PATH`. Use one of the CLI options above.
|
|
78
|
+
|
|
39
79
|
## Usage
|
|
40
80
|
|
|
81
|
+
For an end-to-end setup and verification path, see [walkthrough.md](./walkthrough.md).
|
|
82
|
+
|
|
41
83
|
### Toast Notifications
|
|
42
84
|
|
|
43
85
|
Once installed, you'll see Toast notifications after each AI response:
|
|
@@ -228,6 +270,9 @@ Config changes are automatically backed up to `token-tracker.json.bak`.
|
|
|
228
270
|
# Check budget status
|
|
229
271
|
opencode-tokens budget
|
|
230
272
|
|
|
273
|
+
# Diagnose plugin config, logs, and pricing fallbacks
|
|
274
|
+
opencode-tokens doctor
|
|
275
|
+
|
|
231
276
|
# Show built-in pricing table
|
|
232
277
|
opencode-tokens pricing
|
|
233
278
|
|
|
@@ -237,8 +282,12 @@ opencode-tokens models
|
|
|
237
282
|
# Show current config
|
|
238
283
|
opencode-tokens config
|
|
239
284
|
|
|
240
|
-
#
|
|
285
|
+
# Print clean example JSON to stdout without writing a file
|
|
241
286
|
opencode-tokens config init
|
|
287
|
+
|
|
288
|
+
# Write example config to ~/.config/opencode/token-tracker.json
|
|
289
|
+
# Existing config is backed up to token-tracker.json.bak
|
|
290
|
+
opencode-tokens config generate
|
|
242
291
|
```
|
|
243
292
|
|
|
244
293
|
Example `models` output:
|
|
@@ -255,6 +304,12 @@ This helps you understand:
|
|
|
255
304
|
- Whether pricing is from built-in table, your config, or default fallback
|
|
256
305
|
- What to add to your config file
|
|
257
306
|
|
|
307
|
+
`config init` is safe for piping because stdout contains only valid JSON and no file is written. `config generate` is the file-writing path: stdout stays empty, guides and status messages go to stderr, the parent directory is created when needed, and an existing config is backed up before overwrite. Both commands inspect local logs and pre-fill likely zero-cost providers such as GitHub Copilot, Cursor, and Ollama.
|
|
308
|
+
|
|
309
|
+
`doctor` is a read-only setup check. It reports whether the OpenCode plugin
|
|
310
|
+
entry is present, whether the tracker config and token log exist, the latest log
|
|
311
|
+
record, default-priced models, and the next command to run.
|
|
312
|
+
|
|
258
313
|
## Log Files
|
|
259
314
|
|
|
260
315
|
Token usage is logged to:
|
|
@@ -359,29 +414,34 @@ All prices are in **USD per 1 million tokens**:
|
|
|
359
414
|
|
|
360
415
|
| Scenario | Config |
|
|
361
416
|
|----------|--------|
|
|
362
|
-
| Subscription service (GitHub Copilot, Cursor) | `{ "input": 0, "output": 0 }` |
|
|
363
|
-
| Free/local
|
|
417
|
+
| Subscription service (GitHub Copilot, Cursor) | Provider override: `{ "input": 0, "output": 0 }` |
|
|
418
|
+
| Free/local provider (Ollama, LM Studio, localhost) | Provider override: `{ "input": 0, "output": 0 }` |
|
|
419
|
+
| Free/local model under a paid provider | Model override: `{ "input": 0, "output": 0 }` |
|
|
364
420
|
| Custom API with known pricing | Look up provider's pricing page |
|
|
365
421
|
|
|
366
422
|
### Pricing Override
|
|
367
423
|
|
|
368
424
|
Pricing is resolved in this order (first match wins):
|
|
369
425
|
|
|
370
|
-
1. **Provider-level** - Override all models for a provider
|
|
371
|
-
2. **
|
|
372
|
-
3. **
|
|
373
|
-
4. **Built-in
|
|
374
|
-
5. **
|
|
426
|
+
1. **Provider-level override** - Override all models for a provider
|
|
427
|
+
2. **Exact user model config** - Custom pricing for a specific model or provider-specific model entry
|
|
428
|
+
3. **Built-in exact match** - Exact key in the built-in pricing table
|
|
429
|
+
4. **Built-in partial match** - Longest matching built-in key for variant model names
|
|
430
|
+
5. **User model partial match** - Longest matching user config key
|
|
431
|
+
6. **Fallback** - $1/M input, $4/M output
|
|
432
|
+
|
|
433
|
+
Exact user config is intentionally checked before built-ins, while broad partial user keys are checked after built-ins so a generic key like `"claude"` does not accidentally override a precise built-in model price.
|
|
375
434
|
|
|
376
435
|
#### Example: Free providers
|
|
377
436
|
|
|
378
|
-
If you're using GitHub Copilot or other subscription
|
|
437
|
+
If you're using GitHub Copilot, Ollama, LM Studio, or other subscription/local providers, set their provider cost to $0:
|
|
379
438
|
|
|
380
439
|
```json
|
|
381
440
|
{
|
|
382
441
|
"providers": {
|
|
383
442
|
"github-copilot": { "input": 0, "output": 0 },
|
|
384
|
-
"cursor": { "input": 0, "output": 0 }
|
|
443
|
+
"cursor": { "input": 0, "output": 0 },
|
|
444
|
+
"ollama": { "input": 0, "output": 0 }
|
|
385
445
|
}
|
|
386
446
|
}
|
|
387
447
|
```
|
|
@@ -437,12 +497,15 @@ npm install
|
|
|
437
497
|
# Build
|
|
438
498
|
npm run build
|
|
439
499
|
|
|
440
|
-
#
|
|
441
|
-
npm
|
|
442
|
-
|
|
443
|
-
|
|
500
|
+
# Unit and CLI tests
|
|
501
|
+
npm test
|
|
502
|
+
|
|
503
|
+
# Real local OpenCode CLI dogfood
|
|
504
|
+
node scripts/real-opencode-cli-smoke.mjs --use-temporary-link --model deepseek/deepseek-chat
|
|
444
505
|
```
|
|
445
506
|
|
|
507
|
+
The dogfood script is repo-only and is not published as an npm command. It verifies the real local `opencode run` path, including OpenCode's cache package directory, and restores any temporary package links after the run.
|
|
508
|
+
|
|
446
509
|
## License
|
|
447
510
|
|
|
448
511
|
MIT © [tongsh6](https://github.com/tongsh6)
|
package/README.zh-CN.md
CHANGED
|
@@ -23,8 +23,18 @@
|
|
|
23
23
|
|
|
24
24
|
如果你在构建 AI-assisted engineering 工作流,推荐在你的仓库中采用 AIEF,以获得更清晰的上下文管理与更稳定的 agent 输出。
|
|
25
25
|
|
|
26
|
+
|
|
27
|
+
## 准确性与限制
|
|
28
|
+
|
|
29
|
+
- **成本均为估算值**,由本地 Token 日志及内置(或用户配置)的定价表计算得出。这可能与您的 Provider 官方账单存在差异 —— 例如,当您使用促销额度、企业折扣或特定定价优惠时。
|
|
30
|
+
- **预算提醒仅为警告,非强制阻断**。本插件不会拦截 API 调用、节流请求或中断您的会话。其设计初衷纯粹是为了可观测性与用量追踪。
|
|
31
|
+
- **订阅制或打包服务商**(如 GitHub Copilot、Cursor 等)或免费的本地模型,应在您的配置文件中将其价格覆写为 0(参见 [配置说明](#配置说明))。
|
|
32
|
+
- **定价数据时效性**:内置的定价表为手动维护。请运行 `opencode-tokens models` 来检查您当前使用的哪些模型退化(fallback)到了默认定价,并在需要时手动配置覆写。
|
|
33
|
+
|
|
26
34
|
## 安装
|
|
27
35
|
|
|
36
|
+
### 插件
|
|
37
|
+
|
|
28
38
|
在 OpenCode 配置文件 `~/.config/opencode/opencode.json` 中添加插件:
|
|
29
39
|
|
|
30
40
|
```json
|
|
@@ -36,8 +46,39 @@
|
|
|
36
46
|
|
|
37
47
|
重启 OpenCode 后会自动安装插件。
|
|
38
48
|
|
|
49
|
+
这一步只会让 OpenCode 的插件运行时安装并加载该包,不会把
|
|
50
|
+
`opencode-tokens` CLI 命令加入你的 shell `PATH`。
|
|
51
|
+
|
|
52
|
+
### CLI
|
|
53
|
+
|
|
54
|
+
如果只是偶尔查看统计,可以不做持久安装,直接通过 npm 运行:
|
|
55
|
+
|
|
56
|
+
```bash
|
|
57
|
+
npx -y --package opencode-token-tracker opencode-tokens today
|
|
58
|
+
```
|
|
59
|
+
|
|
60
|
+
等价的 `npm exec` 写法:
|
|
61
|
+
|
|
62
|
+
```bash
|
|
63
|
+
npm exec --yes --package opencode-token-tracker -- opencode-tokens today
|
|
64
|
+
```
|
|
65
|
+
|
|
66
|
+
如果希望 `opencode-tokens` 成为普通 shell 命令,需要让 npm 建立全局
|
|
67
|
+
bin 链接:
|
|
68
|
+
|
|
69
|
+
```bash
|
|
70
|
+
npm install -g opencode-token-tracker
|
|
71
|
+
opencode-tokens today
|
|
72
|
+
```
|
|
73
|
+
|
|
74
|
+
如果你已经在 OpenCode 配置里添加了插件,但执行 `opencode-tokens today`
|
|
75
|
+
提示 `opencode-tokens: command not found`,说明插件已供 OpenCode 加载,
|
|
76
|
+
但 CLI 命令还没有安装到 shell `PATH`。请使用上面的任一 CLI 运行方式。
|
|
77
|
+
|
|
39
78
|
## 使用
|
|
40
79
|
|
|
80
|
+
端到端安装、验证与 dogfood 路径见 [walkthrough.md](./walkthrough.md)。
|
|
81
|
+
|
|
41
82
|
### Toast 提示
|
|
42
83
|
|
|
43
84
|
安装后,每次 AI 响应会看到类似提示:
|
|
@@ -152,12 +193,90 @@ opencode-tokens --by daily
|
|
|
152
193
|
gpt-5.2 86.9K $0.0000 18
|
|
153
194
|
```
|
|
154
195
|
|
|
196
|
+
`--by` 可选:
|
|
197
|
+
- `model`:按模型分组
|
|
198
|
+
- `agent`:按 agent 分组
|
|
199
|
+
- `provider`:按 provider 分组
|
|
200
|
+
- `daily`:按天分组
|
|
201
|
+
- `session`:按 session ID 分组
|
|
202
|
+
- `all`:显示全部分组
|
|
203
|
+
|
|
204
|
+
### 趋势图
|
|
205
|
+
|
|
206
|
+
查看每日 token、成本或消息数趋势:
|
|
207
|
+
|
|
208
|
+
```bash
|
|
209
|
+
# 30 天成本趋势(默认)
|
|
210
|
+
opencode-tokens trend
|
|
211
|
+
|
|
212
|
+
# 7 天 token 趋势
|
|
213
|
+
opencode-tokens trend --days 7 --metric tokens
|
|
214
|
+
|
|
215
|
+
# 更紧凑的图表
|
|
216
|
+
opencode-tokens trend --width 40
|
|
217
|
+
```
|
|
218
|
+
|
|
219
|
+
可选参数:
|
|
220
|
+
- `--days N`:统计天数,默认 30
|
|
221
|
+
- `--metric`:`cost`、`tokens` 或 `messages`,默认 `cost`
|
|
222
|
+
- `--width W`:图表宽度,默认 60
|
|
223
|
+
|
|
224
|
+
### 数据导出
|
|
225
|
+
|
|
226
|
+
导出 JSONL 聚合后的用量数据:
|
|
227
|
+
|
|
228
|
+
```bash
|
|
229
|
+
# 导出全部数据为 CSV
|
|
230
|
+
opencode-tokens export
|
|
231
|
+
|
|
232
|
+
# 导出本月数据为 JSON
|
|
233
|
+
opencode-tokens export --format json --period month
|
|
234
|
+
|
|
235
|
+
# 写入文件
|
|
236
|
+
opencode-tokens export --format csv --output usage.csv
|
|
237
|
+
```
|
|
238
|
+
|
|
239
|
+
可选参数:
|
|
240
|
+
- `--format`:`csv` 或 `json`,默认 `csv`
|
|
241
|
+
- `--period`:`today`、`week`、`month`、`all`,默认 `all`
|
|
242
|
+
- `--output FILE`:写入文件,而不是 stdout
|
|
243
|
+
|
|
244
|
+
### 配置管理
|
|
245
|
+
|
|
246
|
+
可直接通过 CLI 管理预算与 Toast 配置:
|
|
247
|
+
|
|
248
|
+
```bash
|
|
249
|
+
# 查看当前配置
|
|
250
|
+
opencode-tokens config
|
|
251
|
+
|
|
252
|
+
# 设置每日预算为 $10
|
|
253
|
+
opencode-tokens config set budget.daily 10
|
|
254
|
+
|
|
255
|
+
# 关闭 Toast
|
|
256
|
+
opencode-tokens config set toast.enabled false
|
|
257
|
+
|
|
258
|
+
# 查看某个值
|
|
259
|
+
opencode-tokens config get budget.warnAt
|
|
260
|
+
|
|
261
|
+
# 恢复默认
|
|
262
|
+
opencode-tokens config unset budget.daily
|
|
263
|
+
```
|
|
264
|
+
|
|
265
|
+
可设置字段:
|
|
266
|
+
- `budget.daily`、`budget.weekly`、`budget.monthly`、`budget.warnAt`
|
|
267
|
+
- `toast.enabled`、`toast.duration`、`toast.showOnIdle`
|
|
268
|
+
|
|
269
|
+
配置变更会自动备份到 `token-tracker.json.bak`。
|
|
270
|
+
|
|
155
271
|
### 定价与配置命令
|
|
156
272
|
|
|
157
273
|
```bash
|
|
158
274
|
# 查看预算状态
|
|
159
275
|
opencode-tokens budget
|
|
160
276
|
|
|
277
|
+
# 诊断插件配置、日志与默认定价回退
|
|
278
|
+
opencode-tokens doctor
|
|
279
|
+
|
|
161
280
|
# 查看内置定价表
|
|
162
281
|
opencode-tokens pricing
|
|
163
282
|
|
|
@@ -167,8 +286,12 @@ opencode-tokens models
|
|
|
167
286
|
# 查看当前配置
|
|
168
287
|
opencode-tokens config
|
|
169
288
|
|
|
170
|
-
#
|
|
289
|
+
# 输出干净 JSON 到 stdout,不写文件
|
|
171
290
|
opencode-tokens config init
|
|
291
|
+
|
|
292
|
+
# 写入 ~/.config/opencode/token-tracker.json
|
|
293
|
+
# 已有配置会备份到 token-tracker.json.bak
|
|
294
|
+
opencode-tokens config generate
|
|
172
295
|
```
|
|
173
296
|
|
|
174
297
|
`models` 示例输出:
|
|
@@ -186,6 +309,11 @@ opencode-tokens config init
|
|
|
186
309
|
- 定价来源是内置表、用户配置还是默认回退
|
|
187
310
|
- 需要在配置文件中补充哪些模型定价
|
|
188
311
|
|
|
312
|
+
`config init` 适合管道重定向,因为 stdout 只包含合法 JSON,且不会写文件。`config generate` 是写文件路径:stdout 保持为空,说明和状态信息输出到 stderr;父目录不存在时会自动创建,覆盖已有配置前会先备份。两个命令都会读取本地日志,并为 GitHub Copilot、Cursor、Ollama 这类疑似零成本 provider 预填覆盖配置。
|
|
313
|
+
|
|
314
|
+
`doctor` 是只读诊断命令,会检查 OpenCode 插件入口、tracker 配置、token log、
|
|
315
|
+
最新记录、默认定价回退模型,并给出下一步应该运行的命令。
|
|
316
|
+
|
|
189
317
|
## 日志文件
|
|
190
318
|
|
|
191
319
|
token 记录保存在:
|
|
@@ -276,8 +404,9 @@ token 记录保存在:
|
|
|
276
404
|
|
|
277
405
|
| 场景 | 配置 |
|
|
278
406
|
| --- | --- |
|
|
279
|
-
| 订阅制服务(GitHub Copilot、Cursor) |
|
|
280
|
-
|
|
|
407
|
+
| 订阅制服务(GitHub Copilot、Cursor) | Provider 覆盖:`{ "input": 0, "output": 0 }` |
|
|
408
|
+
| 免费/本地 provider(Ollama、LM Studio、localhost) | Provider 覆盖:`{ "input": 0, "output": 0 }` |
|
|
409
|
+
| 付费 provider 下的免费/本地模型 | Model 覆盖:`{ "input": 0, "output": 0 }` |
|
|
281
410
|
| 自定义 API(已知定价) | 查看 provider 官方定价页 |
|
|
282
411
|
|
|
283
412
|
### 定价优先级
|
|
@@ -285,20 +414,24 @@ token 记录保存在:
|
|
|
285
414
|
定价解析顺序(命中即止):
|
|
286
415
|
|
|
287
416
|
1. **Provider 覆盖** — 为某个 provider 的所有模型统一设置
|
|
288
|
-
2.
|
|
289
|
-
3.
|
|
290
|
-
4.
|
|
291
|
-
5.
|
|
417
|
+
2. **用户 model 精确匹配** — 为特定模型或 provider-specific model entry 自定义定价
|
|
418
|
+
3. **内置定价精确匹配** — 命中内置定价表中的精确 key
|
|
419
|
+
4. **内置定价部分匹配** — 对变体模型名使用最长的内置 key 匹配
|
|
420
|
+
5. **用户 model 部分匹配** — 使用最长的用户配置 key 匹配
|
|
421
|
+
6. **默认回退** — $1/M input,$4/M output
|
|
422
|
+
|
|
423
|
+
精确用户配置会优先于内置定价;但宽泛的用户部分匹配会排在内置匹配之后,避免 `"claude"` 这类泛 key 意外覆盖精确的内置模型价格。
|
|
292
424
|
|
|
293
425
|
#### 示例:免费 provider
|
|
294
426
|
|
|
295
|
-
使用 GitHub Copilot
|
|
427
|
+
使用 GitHub Copilot、Ollama、LM Studio 等订阅制或本地 provider 时,将 provider 成本设为 $0:
|
|
296
428
|
|
|
297
429
|
```json
|
|
298
430
|
{
|
|
299
431
|
"providers": {
|
|
300
432
|
"github-copilot": { "input": 0, "output": 0 },
|
|
301
|
-
"cursor": { "input": 0, "output": 0 }
|
|
433
|
+
"cursor": { "input": 0, "output": 0 },
|
|
434
|
+
"ollama": { "input": 0, "output": 0 }
|
|
302
435
|
}
|
|
303
436
|
}
|
|
304
437
|
```
|
|
@@ -368,12 +501,15 @@ npm install
|
|
|
368
501
|
# 构建
|
|
369
502
|
npm run build
|
|
370
503
|
|
|
371
|
-
#
|
|
372
|
-
npm
|
|
373
|
-
|
|
374
|
-
|
|
504
|
+
# 单元与 CLI 测试
|
|
505
|
+
npm test
|
|
506
|
+
|
|
507
|
+
# 真实本机 OpenCode CLI dogfood
|
|
508
|
+
node scripts/real-opencode-cli-smoke.mjs --use-temporary-link --model deepseek/deepseek-chat
|
|
375
509
|
```
|
|
376
510
|
|
|
511
|
+
dogfood 脚本只作为仓库内开发工具,不作为 npm 包命令发布。它验证真实本机 `opencode run` 路径,包括 OpenCode 的 cache package 目录;运行结束后会恢复临时 package link。
|
|
512
|
+
|
|
377
513
|
## License
|
|
378
514
|
|
|
379
515
|
MIT © [tongsh6](https://github.com/tongsh6)
|