slkiser/opencode-quota
GitHub: slkiser/opencode-quota
OpenCode 配额追踪插件,支持多种AI编码助手的统一使用量监控与本地Token报告。
Stars: 101 | Forks: 12
# OpenCode 配额
`opencode-quota` 为您提供两项功能:
- 在助手回复后自动显示配额提示 (toast)
- 手动执行 `/quota`、`/pricing_refresh` 和 `/tokens_*` 命令,以进行更深入的本地报告,且不会污染上下文窗口
**配额提供商**:Anthropic (Claude)、GitHub Copilot、OpenAI (Plus/Pro)、Cursor、Qwen Code、Alibaba Coding Plan、Chutes AI、Firmware AI、Google Antigravity、Z.ai coding plan 以及 NanoGPT。
**Token 报告**:涵盖 [models.dev](https://models.dev) 中的所有模型和提供商,以及针对 Cursor Auto/Composer 和 models.dev 上未包含的 Cursor 模型别名的确定性本地定价。
`/tokens_*` 的输出基于本地 OpenCode 会话历史计算。配额行根据具体提供商,使用其现有的本地身份验证加上确定性本地状态或实时提供商配额端点。
## 快速开始
需要 OpenCode `>= 1.2.0`。将插件添加到您的 `opencode.json` 或 `opencode.jsonc` 中:
```
{
"plugin": ["@slkiser/opencode-quota"]
}
```
然后:
1. 重启或重新加载 OpenCode。
2. 运行 `/quota_status` 以确认提供商检测情况。
3. 运行 `/quota` 或 `/tokens_today`。
对于大多数安装来说,这已经足够。提供商会根据您现有的 OpenCode 设置自动检测,并且大多数提供商使用您现有的 OpenCode 身份验证即可工作。如果某个提供商需要额外配置,请参考下方的设置表。
### 提供商设置一览
| Provider | Auto setup | How it works |
| --- | --- | --- |
| **Anthropic (Claude)** | Needs [quick setup](#anthropic-quick-setup) | Local Claude CLI auth/status probe. |
| **GitHub Copilot** | Usually | OpenCode auth; PAT only for managed billing. |
| **OpenAI** | Yes | OpenCode auth. |
| **Cursor** | Needs [quick setup](#cursor-quick-setup) | Companion auth plugin + `provider.cursor`. |
| **Qwen Code** | Needs [quick setup](#qwen-code-quick-setup) | Companion auth plugin. |
| **Alibaba Coding Plan** | Yes | OpenCode auth + local request estimation. |
| **Firmware AI** | Usually | User/global OpenCode config or env. |
| **Chutes AI** | Usually | User/global OpenCode config or env. |
| **NanoGPT** | Usually | User/global OpenCode config, env, or auth.json. |
| **Google Antigravity** | Needs [quick setup](#google-antigravity-quick-setup) | Companion auth plugin. |
| **Z.ai** | Yes | OpenCode auth. |
## 命令
| Command | What it shows |
| --- | --- |
| `/quota` | Manual grouped quota report with a local call timestamp |
| `/quota_status` | Concise diagnostics for config, provider availability, account detection, and pricing snapshot health |
| `/pricing_refresh` | Pull the local runtime pricing snapshot from `models.dev` on demand |
| `/tokens_today` | Tokens used today (calendar day) |
| `/tokens_daily` | Tokens used in the last 24 hours |
| `/tokens_weekly` | Tokens used in the last 7 days |
| `/tokens_monthly` | Tokens used in the last 30 days, including pricing sections |
| `/tokens_all` | Tokens used across all local history |
| `/tokens_session` | Tokens used in the current session |
| `/tokens_between` | Tokens used between two dates: `YYYY-MM-DD YYYY-MM-DD` |
没有 `/token` 命令。报告命令属于 `/tokens_*` 系列。
## 提供商专属说明
)。凭据位于 OpenCode 运行时配置目录下。
如果检测看起来不正确,`/quota_status` 会打印为 `antigravity-accounts.json` 检查的候选路径。
## 配置参考
所有插件设置位于 `experimental.quotaToast` 下。
工作区本地配置仍然可以自定义显示/报告行为,但用户/全局配置对于影响网络的设置具有权威性,例如 `enabled`、`enabledProviders`、`minIntervalMs`、`pricingSnapshot`、`showOnIdle`、`showOnQuestion` 和 `showOnCompact`。
| Option | Default | Meaning |
| --- | --- | --- |
| `enabled` | `true` | Master switch for the plugin. When `false`, `/quota`, `/quota_status`, `/pricing_refresh`, and `/tokens_*` are no-ops. |
| `enableToast` | `true` | Show popup toasts |
| `toastStyle` | `classic` | Toast layout: `classic` or `grouped` |
| `enabledProviders` | `"auto"` | Auto-detect providers, or set an explicit provider list |
| `anthropicBinaryPath` | `"claude"` | Command/path used for local Claude CLI probing; override this for custom installs or shim locations |
| `minIntervalMs` | `300000` | Minimum fetch interval between provider updates |
| `toastDurationMs` | `9000` | Toast duration in milliseconds |
| `showOnIdle` | `true` | Show toast on idle trigger |
| `showOnQuestion` | `true` | Show toast after a question/assistant response |
| `showOnCompact` | `true` | Show toast after session compaction |
| `showOnBothFail` | `true` | Show a fallback toast when providers attempt and all fail |
| `onlyCurrentModel` | `false` | Filter to the current model when possible |
| `showSessionTokens` | `true` | Append current-session token totals to toast output |
| `layout.maxWidth` | `50` | Main formatting width target |
| `layout.narrowAt` | `42` | Compact layout breakpoint |
| `layout.tinyAt` | `32` | Tiny layout breakpoint |
| `googleModels` | `["CLAUDE"]` | Google model keys: `CLAUDE`, `G3PRO`, `G3FLASH`, `G3IMAGE` |
| `alibabaCodingPlanTier` | `"lite"` | Fallback Alibaba Coding Plan tier when auth does not include `tier` |
| `cursorPlan` | `"none"` | Cursor included API budget preset: `none`, `pro`, `pro-plus`, `ultra` |
| `cursorIncludedApiUsd` | unset | Override Cursor monthly included API budget in USD |
| `cursorBillingCycleStartDay` | unset | Local billing-cycle anchor day `1..28`; when unset, Cursor usage resets on the local calendar month |
| `pricingSnapshot.source` | `"auto"` | Token pricing snapshot selection: `auto`, `bundled`, or `runtime` |
| `pricingSnapshot.autoRefresh` | `5` | Refresh stale local pricing data after this many days |
| `debug` | `false` | Include debug context in toast output |
## Token 定价快照
`/tokens_*` 使用本地 `models.dev` 定价快照。捆绑的快照随离线使用一起提供,Cursor `auto` 和 `composer*` 定价保持捆绑状态,因为这些 id 不在 `models.dev` 上。
| `pricingSnapshot.source` | Active pricing behavior |
| --- | --- |
| `auto` | Newer runtime snapshot wins; otherwise bundled pricing stays active. |
| `bundled` | Packaged bundled snapshot stays active. |
| `runtime` | Runtime snapshot stays active when present; bundled pricing is fallback until one exists. |
- 有关选项默认值,请参阅 [配置参考](#configuration-reference)。
- `pricingSnapshot.autoRefresh` 控制运行时快照在多少天后被视为过期并触发后台刷新。
- `/pricing_refresh` 仅刷新 OpenCode 缓存目录下的本地运行时快照。它永远不会重写打包的捆绑快照。
- 如果 `pricingSnapshot.source` 为 `bundled`,`/pricing_refresh` 仍会更新运行时缓存,但活动定价仍为捆绑版本。
- 即使刷新失败,报告仍将继续工作。
- 定价选择保持本地化和确定性。没有自定义 URL 或任意定价来源。
## 故障排除
如果缺少某些内容或看起来不正确:
1. 运行 `/quota_status`。
2. 确认预期的提供商出现在检测到的提供商列表中。
3. 如果 token 报告为空,请确保 OpenCode 已经创建了 `opencode.db`。
4. 如果预期有 Copilot 托管计费,请确认 `copilot-quota-token.json` 存在且有效。
5. 如果提供商设置看起来不正确,请检查 [提供商设置一览](#provider-setup-at-a-glance) 和 [提供商专属说明](#provider-specific-notes)。对于 Google Antigravity 或 Qwen Code,请确认已安装配套身份验证插件。对于 Alibaba Coding Plan,请确认已配置 OpenCode `alibaba` 或 `alibaba-coding-plan` 身份验证;`tier` 可能是 `lite` 或 `pro`,如果缺失,插件将回退到 `experimental.quotaToast.alibabaCodingPlanTier`。
如果缺少 `opencode.db`,请启动 OpenCode 一次并让其完成本地迁移。
## 贡献
有关贡献工作流程和仓库策略,请参阅 [CONTRIBUTING.md](./CONTRIBUTING.md)。
## 许可证
MIT
## 备注
OpenCode Quota 并非由 OpenCode 团队构建,也不隶属于 OpenCode 或上述任何提供商。
Anthropic 配额检测委托给本地 Claude CLI/运行时。OpenCode Anthropic API-key 使用不受影响,但此插件仅在本地 Claude CLI 暴露配额行时显示 Anthropic 配额行。
## Star 历史
| Example of toast | Example of /tokens_weekly |
|
|
示例:关闭自动检测并手动选择提供商
``` { "experimental": { "quotaToast": { "enabledProviders": ["copilot", "openai", "google-antigravity"] } } } ```示例:使用分组提示布局代替默认的经典提示
``` { "experimental": { "quotaToast": { "toastStyle": "grouped" } } } ```快速设置:Anthropic (Claude)
Anthropic 配额支持现在检查本地 Claude CLI,而不是将 Claude 消费者 OAuth token 直接传递给 Anthropic API。 如果 Claude Code 已经安装并通过身份验证,这通常会自动生效。否则: 1. 安装 Claude Code,使 `claude` 在您的 `PATH` 中可用。 2. 运行 `claude auth login`。 3. 确认 `claude auth status` 在本地成功运行。 4. 确认 OpenCode 已配置 `anthropic` 提供商。 如果 Claude 位于自定义路径,请设置 `experimental.quotaToast.anthropicBinaryPath`。默认值为 `claude`。 如果您在 OpenCode 中通过 API key 使用 Anthropic,模型使用仍将正常工作。此插件仅在本地 Claude CLI 暴露配额窗口时显示 Anthropic 配额行。 有关行为详情和故障排除,请参阅 [Anthropic notes](#anthropic-notes)。快速设置:Cursor
Cursor 配额支持需要 `opencode-cursor-oauth` [插件](https://github.com/ephraimduncan/opencode-cursor): ``` { "$schema": "https://opencode.ai/config.json", "plugin": ["opencode-cursor-oauth", "@slkiser/opencode-quota"], "provider": { "cursor": { "name": "Cursor" } }, "experimental": { "quotaToast": { "cursorPlan": "pro", "cursorBillingCycleStartDay": 7 } } } ``` 然后进行一次身份验证: ``` opencode auth login --provider cursor ``` 有关行为详情和故障排除,请参阅 [Cursor notes](#cursor-notes)。快速设置:Google Antigravity
Google 配额支持需要 `opencode-antigravity-auth` [插件](https://github.com/NoeFabris/opencode-antigravity-auth): ``` { "plugin": ["opencode-antigravity-auth", "@slkiser/opencode-quota"] } ``` 有关行为详情和故障排除,请参阅 [Google Antigravity notes](#google-antigravity-notes)。快速设置:Qwen Code
Qwen 配额支持需要 `opencode-qwencode-auth` [插件](https://github.com/gustavodiasdev/opencode-qwencode-auth): ``` { "plugin": ["opencode-qwencode-auth", "@slkiser/opencode-quota"] } ``` 有关行为详情和故障排除,请参阅 [Qwen Code notes](#qwen-code-notes)。Anthropic (Claude)
插件使用 `anthropicBinaryPath --version` 和 `anthropicBinaryPath auth status` 探测本地 Claude CLI。默认情况下,`anthropicBinaryPath` 为 `claude`,因此标准安装无需额外配置即可工作。它不会将 Claude Free/Pro/Max OAuth token 直接传递给 Anthropic 端点。 如果 Claude CLI 在本地结构化输出中暴露 5 小时和 7 天的配额窗口,插件将显示它们。如果 CLI 仅暴露身份验证状态,Anthropic 配额行将被跳过,`/quota_status` 会解释原因。 **故障排除:** | Problem | Solution | | --- | --- | | `claude` not found | Install Claude Code and make sure `claude` is on your `PATH` | | Claude installed at a custom path | Set `experimental.quotaToast.anthropicBinaryPath` to the Claude executable path | | Not authenticated | Run `claude auth login`, then confirm `claude auth status` works | | Authenticated but no quota rows | Your local Claude CLI version did not expose quota windows; run `/quota_status` for the exact probe result | | Plugin not detected | Confirm OpenCode is configured with the `anthropic` provider |GitHub Copilot
当 OpenCode 已登录时,个人配额会自动生效。如果没有 `copilot-quota-token.json`,插件会从 `~/.local/share/opencode/auth.json` 读取 OpenCode Copilot OAuth token,并调用 `GET https://api.github.com/copilot_internal/user`。 - 托管计费使用 OpenCode 运行时配置目录中的 `copilot-quota-token.json`(`opencode debug paths`)。`business` 需要 `organization`;`enterprise` 需要 `enterprise`,也可以按 `organization` 或 `username` 进行过滤。 - `copilot-quota-token.json` 优先于 OAuth。如果 PAT 配置无效,插件会报告该错误,而不是静默回退。 - 输出标记为 `[Copilot] (personal)` 或 `[Copilot] (business)`,托管输出包括 org 或 enterprise slug。 - Enterprise 高级用量不支持细粒度 PAT 或 GitHub App token。 - 检查 `/quota_status` 中的 `copilot_quota_auth`、`billing_mode`、`billing_scope`、`quota_api`、`effective_source` 和 `billing_api_access_likely`。 示例 `copilot-quota-token.json`: ``` { "token": "github_pat_...", "tier": "business", "organization": "your-org-slug" } ``` ``` { "token": "ghp_...", "tier": "enterprise", "enterprise": "your-enterprise-slug", "organization": "optional-org-filter", "username": "optional-user-filter" } ```Cursor
有关身份验证,请参阅 [Cursor 快速设置](#cursor-quick-setup)。配额和 token 报告仅限于 OpenCode 历史记录和本地定价数据。 - 当提供商为 `cursor` 或存储/当前模型 id 为 `cursor/*` 时,检测 Cursor 使用情况。 - `/tokens_*` 将 Cursor API 池模型映射到官方定价,并为 `auto` 和 `composer*` 使用捆绑的静态定价。 - `/quota` 和 toast 仅根据本地历史估算当前计费周期的支出。不需要会话 cookie 和团队 API。 - 仅当设置了 `experimental.quotaToast.cursorPlan` 或 `experimental.quotaToast.cursorIncludedApiUsd` 时,才会显示剩余百分比。计费周期默认为本地日历月,除非设置了 `experimental.quotaToast.cursorBillingCycleStartDay`。 - 旧的 `cursor-acp/*` 历史记录仍可读取。未知的未来 Cursor 模型 id 会出现在 `/quota_status` 的 Cursor 诊断和 `unknown_pricing` 中。 示例覆盖: ``` { "experimental": { "quotaToast": { "cursorPlan": "none", "cursorIncludedApiUsd": 120 } } } ```Qwen Code
有关身份验证,请参阅 [Qwen Code 快速设置](#qwen-code-quick-setup)。用量仅为免费计划的本地估算;插件不会调用 Alibaba 配额 API。 - 免费层级限制:每个 UTC 日 `1000` 次请求,每个滚动分钟 `60` 次请求。 - 当当前模型为 `qwen-code/*` 时,计数器在成功的问题-工具完成后递增。 - 状态文件:`.../opencode/opencode-quota/qwen-local-quota.json`。 - 检查 `/quota_status` 以获取身份验证检测、`qwen_local_plan` 和本地计数器状态。Alibaba Coding Plan
使用来自 `alibaba` 或 `alibaba-coding-plan` 的原生 OpenCode 身份验证。配额是带有滚动窗口的本地请求计数估算。 - `lite`: `1200 / 5h`, `9000 / week`, `18000 / month` - `pro`: `6000 / 5h`, `45000 / week`, `90000 / month` - 如果身份验证中省略了 `tier`,插件将使用 `experimental.quotaToast.alibabaCodingPlanTier`,默认值为 `lite`。 - 当当前模型为 `alibaba/*` 或 `alibaba-cn/*` 时,计数器在成功的问题-工具完成后递增。 - 状态文件:`.../opencode/opencode-quota/alibaba-coding-plan-local-quota.json`。 - `/quota_status` 显示身份验证检测、解析的层级、状态文件路径以及当前的 5 小时/每周/每月用量。 示例回退层级: ``` { "experimental": { "quotaToast": { "alibabaCodingPlanTier": "lite" } } } ```Firmware AI
如果 OpenCode 已经配置了 Firmware,通常会自动生效。可选 API key:`provider.firmware.options.apiKey`。 出于安全考虑,提供商密钥仅从环境变量或您的用户/全局 OpenCode 配置中读取。对于 `provider.firmware.options.apiKey`,Repo 本地的 `opencode.json` / `opencode.jsonc` 会被忽略。 允许的 env 模板仅限于 `{env:FIRMWARE_AI_API_KEY}` 和 `{env:FIRMWARE_API_KEY}`。 示例用户/全局配置(Linux/macOS 上的 `~/.config/opencode/opencode.jsonc`): ``` { "provider": { "firmware": { "options": { "apiKey": "{env:FIRMWARE_API_KEY}" } } } } ```Chutes AI
如果 OpenCode 已经配置了 Chutes,通常会自动生效。可选 API key:`provider.chutes.options.apiKey`。 出于安全考虑,提供商密钥仅从环境变量或您的用户/全局 OpenCode 配置中读取。对于 `provider.chutes.options.apiKey`,Repo 本地的 `opencode.json` / `opencode.jsonc` 会被忽略。 允许的 env 模板仅限于 `{env:CHUTES_API_KEY}`。 示例用户/全局配置(Linux/macOS 上的 `~/.config/opencode/opencode.jsonc`): ``` { "provider": { "chutes": { "options": { "apiKey": "{env:CHUTES_API_KEY}" } } } } ```Google Antigravity
请参阅 [Google Antigravity 快速设置](NanoGPT
NanoGPT 使用实时 NanoGPT 订阅用量和余额端点,因此 `/quota`、分组/经典 toast 和 `/quota_status` 可以实时显示每日配额、每月配额和账户余额。 - 规范提供商 id 是 `nanogpt`。别名 `nano-gpt` 也会在 `enabledProviders` 中标准化。 - 可选 API key:`provider.nanogpt.options.apiKey` 或 `provider["nano-gpt"].options.apiKey`。 - 出于安全考虑,提供商密钥从 `NANOGPT_API_KEY`、`NANO_GPT_API_KEY`、您的用户/全局 OpenCode 配置或 `auth.json` 读取。对于 NanoGPT 密钥,Repo 本地的 `opencode.json` / `opencode.jsonc` 会被忽略。 - 允许的 env 模板仅限于 `{env:NANOGPT_API_KEY}` 和 `{env:NANO_GPT_API_KEY}`。 - `/quota_status` 打印一个 `nanogpt` 部分,包含 API-key 诊断、身份验证候选路径、实时订阅状态、每日/每月用量窗口、端点错误和余额详情。 - NanoGPT 配额反映订阅覆盖的请求和账户余额。在 `/tokens_*` 中不按 token 定价。 示例用户/全局配置(Linux/macOS 上的 `~/.config/opencode/opencode.jsonc`): ``` { "provider": { "nanogpt": { "options": { "apiKey": "{env:NANOGPT_API_KEY}" } } } } ```标签:AI 编程助手, Anthropic Claude, Cursor, GitHub Copilot, LLM 工具, OpenAI, OpenCode 插件, SaaS 订阅管理, Token 统计, VS Code 扩展, 价格计算, 使用量追踪, 依赖扫描, 内存规避, 大模型计费, 威胁情报, 开发者工具, 成本管理, 模型提供商, 自动化攻击, 自动化通知, 通义千问, 配额监控, 零上下文污染