opencode-token-tracker 1.3.2 → 1.5.0

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 CHANGED
@@ -2,17 +2,27 @@
2
2
 
3
3
  Real-time token usage and cost tracking plugin for [OpenCode](https://opencode.ai).
4
4
 
5
- ![Toast Screenshot](./docs/toast-demo.png)
5
+ [English](./README.md) | [简体中文](./README.zh-CN.md)
6
6
 
7
7
  ## Features
8
8
 
9
9
  - **Real-time Toast notifications** - See token usage and cost after each AI response
10
+ - **Budget control** - Set daily/weekly/monthly spending limits with warnings
10
11
  - **Session statistics** - Track cumulative usage across your entire session
11
12
  - **CLI statistics tool** - Query usage by day/week/month with breakdowns by model/agent
12
13
  - **Cost calculation** - Automatic cost estimation based on model pricing
13
14
  - **JSONL logging** - All usage data saved locally for analysis
14
15
  - **Multi-model support** - Claude, GPT, DeepSeek, Gemini, and more
15
16
 
17
+ ## AI Engineering Framework
18
+
19
+ This project uses the [AI Engineering Framework (AIEF)](https://github.com/tongsh6/ai-engineering-framework) to organize AI collaboration context and conventions.
20
+
21
+ - `AGENTS.md` defines repository-level collaboration rules
22
+ - `context/` stores technical snapshots, coding conventions, and business semantics
23
+
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
+
16
26
  ## Installation
17
27
 
18
28
  Add to your OpenCode config file (`~/.config/opencode/opencode.json`):
@@ -37,6 +47,13 @@ Once installed, you'll see Toast notifications after each AI response:
37
47
  $0.023 | Session: $0.156
38
48
  ```
39
49
 
50
+ When budget limits are configured, you'll see warnings:
51
+
52
+ ```
53
+ ⚠️ Budget exceeded!
54
+ Daily: $5.50/$5.00 (110%)
55
+ ```
56
+
40
57
  When a session becomes idle, you'll see a summary:
41
58
 
42
59
  ```
@@ -44,6 +61,49 @@ Session: 45.2K tokens
44
61
  $0.156 | 8 msgs | 5min
45
62
  ```
46
63
 
64
+ ### Budget Control
65
+
66
+ Set spending limits to avoid unexpected costs:
67
+
68
+ ```bash
69
+ # Check current budget status
70
+ opencode-tokens budget
71
+ ```
72
+
73
+ Example output:
74
+
75
+ ```
76
+ Budget Status
77
+ ══════════════════════════════════════════════════════════════════
78
+
79
+ 🟢 Daily
80
+ $3.50 / $10.00 [███████░░░░░░░░░░░░░] 35%
81
+ Remaining: $6.50
82
+
83
+ 🟡 Weekly
84
+ $42.00 / $50.00 [████████████████░░░░] 84%
85
+ Remaining: $8.00
86
+
87
+ 🟢 Monthly
88
+ $120.00 / $200.00 [████████████░░░░░░░░] 60%
89
+ Remaining: $80.00
90
+
91
+ Legend: 🟢 OK 🟡 Warning (>80%) 🔴 Exceeded
92
+ ```
93
+
94
+ Configure budget in `~/.config/opencode/token-tracker.json`:
95
+
96
+ ```json
97
+ {
98
+ "budget": {
99
+ "daily": 10, // $10 per day
100
+ "weekly": 50, // $50 per week
101
+ "monthly": 200, // $200 per month
102
+ "warnAt": 0.8 // Warn at 80% usage
103
+ }
104
+ }
105
+ ```
106
+
47
107
  ### CLI Statistics
48
108
 
49
109
  Query your token usage from the command line:
@@ -97,6 +157,9 @@ Breakdown options (`--by`):
97
157
  ### Pricing & Config Commands
98
158
 
99
159
  ```bash
160
+ # Check budget status
161
+ opencode-tokens budget
162
+
100
163
  # Show built-in pricing table
101
164
  opencode-tokens pricing
102
165
 
@@ -179,10 +242,30 @@ Create a config file at `~/.config/opencode/token-tracker.json`:
179
242
  "enabled": true,
180
243
  "duration": 3000,
181
244
  "showOnIdle": true
245
+ },
246
+ "budget": {
247
+ "daily": 10,
248
+ "weekly": 50,
249
+ "monthly": 200,
250
+ "warnAt": 0.8
182
251
  }
183
252
  }
184
253
  ```
185
254
 
255
+ ### Budget Settings
256
+
257
+ | Option | Type | Default | Description |
258
+ |--------|------|---------|-------------|
259
+ | `daily` | number | - | Maximum daily spend in USD |
260
+ | `weekly` | number | - | Maximum weekly spend in USD |
261
+ | `monthly` | number | - | Maximum monthly spend in USD |
262
+ | `warnAt` | number | `0.8` | Warning threshold (0-1), e.g., 0.8 = warn at 80% |
263
+
264
+ When you exceed a budget limit:
265
+ - Toast notifications change to warning/error style
266
+ - Use `opencode-tokens budget` to check detailed status
267
+ - Budgets reset at midnight (daily), Monday (weekly), or 1st of month (monthly)
268
+
186
269
  ### Pricing Fields Explained
187
270
 
188
271
  All prices are in **USD per 1 million tokens**:
@@ -0,0 +1,367 @@
1
+ # opencode-token-tracker
2
+
3
+ 面向 [OpenCode](https://opencode.ai) 的实时 token 用量与成本追踪插件。
4
+
5
+ [English](./README.md) | [简体中文](./README.zh-CN.md)
6
+
7
+ ## 功能特性
8
+
9
+ - **实时 Toast 提示**:每次 AI 响应后展示 token 用量和成本
10
+ - **预算控制**:支持日/周/月预算与阈值预警
11
+ - **会话统计**:跟踪整个 session 的累计消耗
12
+ - **CLI 统计工具**:按 day/week/month 查看统计,支持按 model/agent/provider 分组
13
+ - **成本估算**:基于模型定价自动计算估算成本
14
+ - **JSONL 日志**:本地持久化所有用量记录,便于分析
15
+ - **多模型支持**:Claude、GPT、DeepSeek、Gemini 等
16
+
17
+ ## AI Engineering Framework
18
+
19
+ 本项目使用 [AI Engineering Framework (AIEF)](https://github.com/tongsh6/ai-engineering-framework) 组织 AI 协作上下文与规范。
20
+
21
+ - `AGENTS.md`:仓库级 AI 协作规则
22
+ - `context/`:技术快照、编码约定、业务语义文档
23
+
24
+ 如果你在构建 AI-assisted engineering 工作流,推荐在你的仓库中采用 AIEF,以获得更清晰的上下文管理与更稳定的 agent 输出。
25
+
26
+ ## 安装
27
+
28
+ 在 OpenCode 配置文件 `~/.config/opencode/opencode.json` 中添加插件:
29
+
30
+ ```json
31
+ {
32
+ "$schema": "https://opencode.ai/config.json",
33
+ "plugin": ["opencode-token-tracker"]
34
+ }
35
+ ```
36
+
37
+ 重启 OpenCode 后会自动安装插件。
38
+
39
+ ## 使用
40
+
41
+ ### Toast 提示
42
+
43
+ 安装后,每次 AI 响应会看到类似提示:
44
+
45
+ ```
46
+ 12.5K tokens
47
+ $0.023 | Session: $0.156
48
+ ```
49
+
50
+ 配置预算后,超阈值会显示预警:
51
+
52
+ ```
53
+ ⚠️ Budget exceeded!
54
+ Daily: $5.50/$5.00 (110%)
55
+ ```
56
+
57
+ 当会话 idle 时,会显示会话摘要:
58
+
59
+ ```
60
+ Session: 45.2K tokens
61
+ $0.156 | 8 msgs | 5min
62
+ ```
63
+
64
+ ### 预算控制
65
+
66
+ 设置预算限额,避免意外超支:
67
+
68
+ ```bash
69
+ # 查看预算状态
70
+ opencode-tokens budget
71
+ ```
72
+
73
+ 示例输出:
74
+
75
+ ```
76
+ Budget Status
77
+ ══════════════════════════════════════════════════════════════════
78
+
79
+ 🟢 Daily
80
+ $3.50 / $10.00 [███████░░░░░░░░░░░░░] 35%
81
+ Remaining: $6.50
82
+
83
+ 🟡 Weekly
84
+ $42.00 / $50.00 [████████████████░░░░] 84%
85
+ Remaining: $8.00
86
+
87
+ 🟢 Monthly
88
+ $120.00 / $200.00 [████████████░░░░░░░░] 60%
89
+ Remaining: $80.00
90
+
91
+ Legend: 🟢 OK 🟡 Warning (>80%) 🔴 Exceeded
92
+ ```
93
+
94
+ 预算配置文件:`~/.config/opencode/token-tracker.json`
95
+
96
+ ```json
97
+ {
98
+ "budget": {
99
+ "daily": 10,
100
+ "weekly": 50,
101
+ "monthly": 200,
102
+ "warnAt": 0.8
103
+ }
104
+ }
105
+ ```
106
+
107
+ ### CLI 统计
108
+
109
+ ```bash
110
+ # 全量汇总
111
+ opencode-tokens
112
+
113
+ # 今日统计
114
+ opencode-tokens today
115
+
116
+ # 本周统计(按模型分组)
117
+ opencode-tokens week --by model
118
+
119
+ # 本月统计(展示全部分组)
120
+ opencode-tokens month --by all
121
+
122
+ # 按天拆分
123
+ opencode-tokens --by daily
124
+ ```
125
+
126
+ `--by` 可选:
127
+ - `model`:按模型分组
128
+ - `agent`:按 agent 分组
129
+ - `provider`:按 provider 分组
130
+ - `daily`:按天分组
131
+ - `all`:显示全部
132
+
133
+ 示例输出:
134
+
135
+ ```
136
+ Today's Usage
137
+ ──────────────────────────────────────────────────
138
+ Total Tokens: 2.81M
139
+ Input: 2.74M
140
+ Output: 72.9K
141
+ Reasoning: 7.1K
142
+ Cache Read: 12.62M
143
+ Total Cost: $32.93
144
+ Messages: 230
145
+
146
+ By Model
147
+ ─────────────────────────────────────────────────────
148
+ Model Tokens Cost Msgs
149
+ --------------- ---------- ---------- ------
150
+ claude-opus-4.5 2.70M $32.93 206
151
+ deepseek-chat 23.4K $0.0025 6
152
+ gpt-5.2 86.9K $0.0000 18
153
+ ```
154
+
155
+ ### 定价与配置命令
156
+
157
+ ```bash
158
+ # 查看预算状态
159
+ opencode-tokens budget
160
+
161
+ # 查看内置定价表
162
+ opencode-tokens pricing
163
+
164
+ # 查看你实际使用的模型与定价来源
165
+ opencode-tokens models
166
+
167
+ # 查看当前配置
168
+ opencode-tokens config
169
+
170
+ # 基于当前使用情况生成示例配置
171
+ opencode-tokens config init
172
+ ```
173
+
174
+ `models` 示例输出:
175
+
176
+ ```
177
+ Model Provider Msgs Pricing
178
+ ------------------------ ---------------- -------- ------------
179
+ claude-opus-4.5 github-copilot 379 provider cfg
180
+ deepseek-chat deepseek 6 built-in
181
+ gpt-5.2 openai 18 built-in
182
+ ```
183
+
184
+ 可以帮助你了解:
185
+ - 当前使用了哪些模型和 provider
186
+ - 定价来源是内置表、用户配置还是默认回退
187
+ - 需要在配置文件中补充哪些模型定价
188
+
189
+ ## 日志文件
190
+
191
+ token 记录保存在:
192
+
193
+ ```
194
+ ~/.config/opencode/logs/token-tracker/tokens.jsonl
195
+ ```
196
+
197
+ 单行示例:
198
+
199
+ ```json
200
+ {
201
+ "type": "tokens",
202
+ "sessionId": "ses_xxx",
203
+ "messageId": "msg_xxx",
204
+ "agent": "build",
205
+ "model": "claude-opus-4.5",
206
+ "provider": "github-copilot",
207
+ "input": 1500,
208
+ "output": 350,
209
+ "reasoning": 0,
210
+ "cacheRead": 5000,
211
+ "cacheWrite": 0,
212
+ "cost": 0.0234,
213
+ "_ts": 1234567890123
214
+ }
215
+ ```
216
+
217
+ ## 支持模型
218
+
219
+ | Provider | Models |
220
+ | --- | --- |
221
+ | Anthropic | Claude Opus 4.5, Sonnet 4/4.5, Haiku 4/4.5 |
222
+ | OpenAI | GPT-5.x, GPT-4.x, o1, o3 |
223
+ | DeepSeek | deepseek-chat, deepseek-reasoner |
224
+ | Google | Gemini 2.x, 3.x |
225
+
226
+ 未知模型会使用默认定价估算。
227
+
228
+ ## 配置说明
229
+
230
+ 在 `~/.config/opencode/token-tracker.json` 创建配置:
231
+
232
+ ```json
233
+ {
234
+ "providers": {
235
+ "github-copilot": { "input": 0, "output": 0 }
236
+ },
237
+ "models": {
238
+ "my-custom-model": { "input": 1, "output": 2 }
239
+ },
240
+ "toast": {
241
+ "enabled": true,
242
+ "duration": 3000,
243
+ "showOnIdle": true
244
+ },
245
+ "budget": {
246
+ "daily": 10,
247
+ "weekly": 50,
248
+ "monthly": 200,
249
+ "warnAt": 0.8
250
+ }
251
+ }
252
+ ```
253
+
254
+ ### 定价字段
255
+
256
+ 定价单位均为 **USD / 1M tokens**:
257
+
258
+ | 字段 | 说明 | 示例 |
259
+ | --- | --- | --- |
260
+ | `input` | 输入 token 单价 | `15` = $15 / 1M tokens |
261
+ | `output` | 输出 token 单价 | `75` = $75 / 1M tokens |
262
+ | `cacheRead` | 缓存读取 token 单价(可选) | `1.5` = $1.5 / 1M tokens |
263
+ | `cacheWrite` | 缓存写入 token 单价(可选) | `18.75` = $18.75 / 1M tokens |
264
+
265
+ **如何查找模型定价:**
266
+
267
+ 1. 查看各厂商官方定价页:
268
+ - [Anthropic Claude](https://www.anthropic.com/pricing)
269
+ - [OpenAI](https://openai.com/pricing)
270
+ - [DeepSeek](https://platform.deepseek.com/api-docs/pricing)
271
+ - [Google Gemini](https://ai.google.dev/pricing)
272
+
273
+ 2. 或执行 `opencode-tokens pricing` 查看内置定价表
274
+
275
+ **常见场景:**
276
+
277
+ | 场景 | 配置 |
278
+ | --- | --- |
279
+ | 订阅制服务(GitHub Copilot、Cursor) | `{ "input": 0, "output": 0 }` |
280
+ | 免费/本地模型 | `{ "input": 0, "output": 0 }` |
281
+ | 自定义 API(已知定价) | 查看 provider 官方定价页 |
282
+
283
+ ### 定价优先级
284
+
285
+ 定价解析顺序(命中即止):
286
+
287
+ 1. **Provider 覆盖** — 为某个 provider 的所有模型统一设置
288
+ 2. **用户 model 配置** — 为特定模型自定义定价
289
+ 3. **内置定价** — 默认定价表
290
+ 4. **默认回退** — $1/M input,$4/M output
291
+
292
+ #### 示例:免费 provider
293
+
294
+ 使用 GitHub Copilot 等订阅制服务时,将成本设为 $0:
295
+
296
+ ```json
297
+ {
298
+ "providers": {
299
+ "github-copilot": { "input": 0, "output": 0 },
300
+ "cursor": { "input": 0, "output": 0 }
301
+ }
302
+ }
303
+ ```
304
+
305
+ #### 示例:自定义模型定价
306
+
307
+ 为特定模型覆盖或新增定价(单位 USD / 1M tokens):
308
+
309
+ ```json
310
+ {
311
+ "models": {
312
+ "claude-opus-4.5": { "input": 12, "output": 60, "cacheRead": 1.2 },
313
+ "my-local-model": { "input": 0, "output": 0 }
314
+ }
315
+ }
316
+ ```
317
+
318
+ ### 预算设置
319
+
320
+ | 选项 | 类型 | 默认值 | 说明 |
321
+ | --- | --- | --- | --- |
322
+ | `daily` | number | - | 每日预算上限(USD) |
323
+ | `weekly` | number | - | 每周预算上限(USD) |
324
+ | `monthly` | number | - | 每月预算上限(USD) |
325
+ | `warnAt` | number | `0.8` | 预警阈值(0-1),如 0.8 = 达到 80% 时预警 |
326
+
327
+ 超出预算时:
328
+ - Toast 提示切换为预警/错误样式
329
+ - 使用 `opencode-tokens budget` 查看详细状态
330
+ - 预算在每日零点(daily)、每周一(weekly)、每月 1 日(monthly)自动重置
331
+
332
+ ### Toast 设置
333
+
334
+ | 选项 | 类型 | 默认值 | 说明 |
335
+ | --- | --- | --- | --- |
336
+ | `enabled` | boolean | `true` | 是否显示 Toast 提示 |
337
+ | `duration` | number | `3000` | Toast 显示时长(毫秒) |
338
+ | `showOnIdle` | boolean | `true` | 会话 idle 时是否显示会话摘要 |
339
+
340
+ ## 开发
341
+
342
+ ```bash
343
+ # 克隆仓库
344
+ git clone https://github.com/tongsh6/opencode-token-tracker.git
345
+ cd opencode-token-tracker
346
+
347
+ # 安装依赖
348
+ npm install
349
+
350
+ # 构建
351
+ npm run build
352
+
353
+ # 本地联调
354
+ npm link
355
+ cd ~/.config/opencode
356
+ npm link opencode-token-tracker
357
+ ```
358
+
359
+ ## License
360
+
361
+ MIT © [tongsh6](https://github.com/tongsh6)
362
+
363
+ ## Related
364
+
365
+ - [OpenCode](https://opencode.ai) - AI coding assistant
366
+ - [OpenCode Plugins](https://opencode.ai/docs/plugins) - 插件文档
367
+ - [oh-my-opencode](https://github.com/code-yeongyu/oh-my-opencode) - OpenCode 增强插件