This skill is self-contained inside the token-tracker directory. Do not assume any external config file has already been edited.
Data sources:
session_status for the current live session onlynode "{baseDir}/scripts/token-tracker.js" ... for daily, weekly, budget, and alert logicAll usage data is stored in {baseDir}/usage.json (auto-created). The script is the source of truth for:
| User input | What to do | |
|---|---|---|
| ------------ | ------------ | |
| "这次对话用了多少 token" / "cache hit rate" / "context 还剩多少" | Use session_status | |
| "今天用了多少 token" / "今天花了多少" | Run `node "{baseDir}/scripts/token-tracker.js" summary --lang | en>` | |
| "昨天用了多少" / "3月18号的账单" | Run `node "{baseDir}/scripts/token-tracker.js" summary --lang | en> --date YYYY-MM-DD` | |
| "今日详细账单" | Run `node "{baseDir}/scripts/token-tracker.js" bill --lang | en>` | |
| "本周报告" / "本周账单" | Run `node "{baseDir}/scripts/token-tracker.js" week --lang | en>` | |
| "预算还剩多少" / "budget status" | Run `node "{baseDir}/scripts/token-tracker.js" budget-status --lang | en>` | |
| "set budget 15" / "设置预算 20" / "把预算改成 5 美元" | Run `node "{baseDir}/scripts/token-tracker.js" set-budget --lang | en> --amount | |
| "生成日报" / "生成周报" / "检查预算告警" | Use the script or the templates in {baseDir}/automation/ | |
| "配置 OpenAI API Key" / "configure provider" | Run node "{baseDir}/scripts/token-tracker.js" configure-provider --name | |
| "从 API 获取用量" / "fetch usage" / "同步用量" | Run `node "{baseDir}/scripts/token-tracker.js" fetch-usage --lang | en>` | |
| "查看服务商配置" / "list providers" | Run `node "{baseDir}/scripts/token-tracker.js" list-providers --lang | en>` | |
| "查询余额" / "DeepSeek 余额" | Run `node "{baseDir}/scripts/token-tracker.js" provider-balance --name | en>` | |
Use zh when the user writes Chinese, otherwise use en.
Supported provider names: openai, anthropic, openrouter, deepseek, kimi, minimax.
All script commands accept an optional --date YYYY-MM-DD flag to query a specific date instead of today. Use this when the user asks about a past date (e.g. "昨天", "上周三", "3月18号").
Before handling any user request, check whether the session hook is installed:
node "{baseDir}/scripts/setup-hook.js" check
If installed is false, automatically install it:
node "{baseDir}/scripts/setup-hook.js" install
Before every usage-related query in a session (summary, bill, week, daily-report, budget-status, or any question about token spending), you must first run a silent data sync. This happens once per session; skip on subsequent queries.
Step A — Fetch from Provider APIs (preferred)
First, check if any providers are configured:
node "{baseDir}/scripts/token-tracker.js" list-providers --json
If any provider has enabled: true and a non-empty api_key, pull real usage data:
node "{baseDir}/scripts/token-tracker.js" fetch-usage --lang <zh|en>
This calls each configured provider's billing/usage API, merges the results, and saves them locally. All subsequent commands will automatically reflect this data.
Step B — Fallback to session_status (only if no providers configured)
If no providers are configured (all disabled or no keys), fall back to recording the current session:
session_status silently.session_status shows non-zero tokens, run record-session:node "{baseDir}/scripts/token-tracker.js" record-session --model <model_name> --tokens-in <prompt_tokens> --tokens-out <completion_tokens> --cost <session_cost>
Step C — Run the actual command
After Step A or B completes, run the command the user asked for (summary, bill, etc.).
Rules:
fetch-usage fails or returns errors for some providers, proceed anyway — partial data is better than none.list-providers, fetch-usage, or record-session output to the user. These are silent infrastructure steps.session_status shows zero tokens (brand-new session), that is genuinely zero — proceed directly.Use session_status only when the user explicitly asks about this live session — e.g. "这次对话用了多少", "cache hit rate", "context 还剩多少".
Do not use session_status for questions about "today's usage" or "how much did I spend today" — those go through Flow 1 (summary), which uses provider API data or local records.
Extract and report:
If context usage is high, mention /compact.
Run:
node "{baseDir}/scripts/token-tracker.js" summary --lang zh
or
node "{baseDir}/scripts/token-tracker.js" summary --lang en
Return the script output verbatim.
Run:
node "{baseDir}/scripts/token-tracker.js" bill --lang zh
Return the script output verbatim.
Run:
node "{baseDir}/scripts/token-tracker.js" week --lang zh
Return the script output verbatim. Even if all days show zero usage, present the output — it is a valid weekly report showing no recorded consumption.
Run:
node "{baseDir}/scripts/token-tracker.js" daily-report --lang zh
Return the script output verbatim.
Run:
node "{baseDir}/scripts/token-tracker.js" budget-status --lang zh
Return the script output verbatim.
Parse the numeric dollar amount from the user's message.
node "{baseDir}/scripts/token-tracker.js" set-budget --lang zh --amount 15
Return the script output verbatim.
Do not write budget.json manually unless the script is unavailable.
When the user wants to set up a provider API key:
node "{baseDir}/scripts/token-tracker.js" configure-provider --name openai --api-key sk-admin-xxx
Pulls real usage data from all configured (enabled + has API key) providers and saves it locally:
node "{baseDir}/scripts/token-tracker.js" fetch-usage --lang zh
This calls each provider's billing/usage API, merges the results, and writes them to the local usage store. After fetching, all other commands (summary, bill, week, etc.) automatically reflect the API data.
/v1/organization/costs.sk-ant-admin-...). Calls /v1/organizations/usage_report/messages./api/v1/activity for per-day per-model usage. Also supports balance via /api/v1/credits.node "{baseDir}/scripts/token-tracker.js" list-providers --lang zh
OpenRouter, DeepSeek, Kimi, and MiniMax support balance queries:
node "{baseDir}/scripts/token-tracker.js" provider-balance --name deepseek --lang zh
node "{baseDir}/scripts/token-tracker.js" provider-balance --name kimi --lang zh
node "{baseDir}/scripts/token-tracker.js" provider-balance --name minimax --lang zh
Daily reports, weekly reports, and budget alerts are delivered automatically via the agent:bootstrap hook. No external cron or scheduler is required.
The hook calls session-check --json --update-state at every session start and injects pending items as virtual files. Delivery state is tracked in {baseDir}/report-state.json and {baseDir}/alert-state.json to prevent duplicates.
The trigger hour is configurable via daily_report_hour in budget.json (default: 23).
A weekly report is injected on the first session of a new ISO week if last week's report hasn't been sent.
Checked on every session start. Warning and alert states both trigger a notification, de-duplicated per calendar day.
For environments that prefer external scheduling, prompt templates are still available in {baseDir}/automation/:
daily-report.prompt.txtweekly-report.prompt.txtbudget-alert.prompt.txtReplace {baseDir} with the absolute skill path before use.
{baseDir}/usage.json (auto-created by record-session / fetch-usage){baseDir}/budget.json{baseDir}/providers.json{baseDir}/alert-state.json{baseDir}/report-state.json{baseDir}/scripts/token-tracker.js{baseDir}/scripts/providers/{baseDir}/automation/*.prompt.txtusage.json exists but is invalid JSON, surface that as an error instead of pretending usage is zero.共 1 个版本