anthropic-usage
You are helping the user query their Anthropic token usage via the Admin API.
Before running anything
Check whether the API key is available:
CODEBLOCK0
- - If the output is
KEY_MISSING: stop and guide the user through setup. Do NOT proceed until the variable is set. - If the output is
KEY_EXISTS: proceed.
Setup guidance (show this when KEY_MISSING)
Tell the user they have two options:
Option 1 (recommended) — OpenClaw UI:
Open the OpenClaw UI, go to Skills → anthropic-usage, enter the Admin API key in the API key field, and click Save key.
Option 2 — Edit ~/.openclaw/openclaw.json directly (for CLI users):
Add the key under the skill entry:
CODEBLOCK1
The gateway picks up the change automatically — no restart needed.
They can get an Admin API key from the Anthropic Console under Settings → API Keys → Admin keys. Their account must be on an Organization plan to access usage reports.
Running the usage script
Once the key exists, run scripts/usage.sh with the appropriate flags based on what the user asked for:
| User intent | Command |
|---|
| Today's usage | INLINECODE4 |
| This week |
bash scripts/usage.sh --weekly |
| This month |
bash scripts/usage.sh --monthly |
| Breakdown by model |
bash scripts/usage.sh --breakdown |
| Weekly + model breakdown |
bash scripts/usage.sh --weekly --breakdown |
| Monthly + model breakdown |
bash scripts/usage.sh --monthly --breakdown |
Run the command from the skill's root directory (where scripts/ lives), or use the full path to scripts/usage.sh.
Formatting the output
After the script runs:
- 1. Present the data as a friendly chat message, not a raw dump.
- Summarize the key numbers at the top (total input tokens, total output tokens).
- If
--breakdown was used, render the per-model table in a readable way. - Estimate the cost (agent-side step): The script returns token counts only — pricing is not included. After presenting the token data, use your
web_fetch tool to fetch https://www.anthropic.com/pricing and extract the current rates for each model that appeared in the results (input, cache write, cache read, and output token rates). Calculate the estimated cost per model and the total. Present this as a cost summary after the token table. If pricing for a model is not found on the page, note it as unknown and skip it in the total. This fetch is intentional and scoped to anthropic.com. - If the script exits with an error (non-zero exit code), show the error message and suggest fixes:
- Exit 1 / "key file not found" → re-show setup guidance
- "401 Unauthorized" → key is invalid or expired
- "403 Forbidden" → key lacks Admin permissions or account is not on an Organization plan
- Network error → ask the user to check their internet connection
Example friendly output
CODEBLOCK2
Keep the tone helpful and concise.
anthropic-usage
您正在帮助用户通过管理 API 查询其 Anthropic 令牌使用情况。
运行前检查
检查 API 密钥是否可用:
bash
[[ -n ${ANTHROPICADMINAPIKEY:-} ]] && echo KEYEXISTS || echo KEY_MISSING
- - 如果输出为 KEYMISSING:停止操作并引导用户完成设置。在变量设置完成前不要继续。
- 如果输出为 KEYEXISTS:继续操作。
设置指南(当 KEY_MISSING 时显示)
告知用户有两个选项:
选项 1(推荐)— OpenClaw 界面:
打开 OpenClaw 界面,进入 技能 → anthropic-usage,在 API 密钥 字段中输入管理 API 密钥,然后点击 保存密钥。
选项 2 — 直接编辑 ~/.openclaw/openclaw.json(适用于 CLI 用户):
在技能条目下添加密钥:
json
{
skills: {
entries: {
anthropic-usage: {
enabled: true,
apiKey: sk-ant-admin-...
}
}
}
}
网关会自动检测更改 — 无需重启。
用户可以从 Anthropic 控制台的 设置 → API 密钥 → 管理密钥 获取管理 API 密钥。其账户必须处于 组织计划 才能访问使用报告。
运行使用脚本
密钥就绪后,根据用户的需求使用相应标志运行 scripts/usage.sh:
| 用户意图 | 命令 |
|---|
| 今日使用情况 | bash scripts/usage.sh --daily |
| 本周使用情况 |
bash scripts/usage.sh --weekly |
| 本月使用情况 | bash scripts/usage.sh --monthly |
| 按模型细分 | bash scripts/usage.sh --breakdown |
| 每周 + 模型细分 | bash scripts/usage.sh --weekly --breakdown |
| 每月 + 模型细分 | bash scripts/usage.sh --monthly --breakdown |
从技能根目录(scripts/ 所在位置)运行命令,或使用 scripts/usage.sh 的完整路径。
格式化输出
脚本运行后:
- 1. 将数据呈现为 友好的聊天消息,而非原始数据转储。
- 在顶部汇总关键数字(总输入令牌数、总输出令牌数)。
- 如果使用了 --breakdown,以可读方式呈现按模型划分的表格。
- 估算成本(代理端步骤): 脚本仅返回令牌计数 — 不包含定价。呈现令牌数据后,使用您的 web_fetch 工具获取 https://www.anthropic.com/pricing,并提取结果中出现的每个模型的当前费率(输入、缓存写入、缓存读取和输出令牌费率)。计算每个模型的估算成本和总成本。在令牌表格后以成本摘要形式呈现。如果页面上未找到某个模型的定价,则标记为未知并在总计中跳过。此获取操作是有意为之,且范围限定在 anthropic.com。
- 如果脚本退出时出现错误(非零退出码),显示错误消息并建议修复:
- 退出码 1 / 密钥文件未找到 → 重新显示设置指南
- 401 未授权 → 密钥无效或已过期
- 403 禁止 → 密钥缺少管理权限或账户未处于组织计划
- 网络错误 → 请用户检查网络连接
友好输出示例
以下是您过去 7 天的 Anthropic 使用情况:
📥 输入令牌: 12,450,000
📤 输出令牌: 1,830,000
🔢 总令牌数: 14,280,000
模型细分:
| 模型 | 输入令牌 | 输出令牌 |
|---|
| claude-opus-4-6 | 8,200,000 | 1,100,000 |
| claude-sonnet-4-6 |
3,900,000 | 680,000 |
| claude-haiku-4-5-20251001 | 350,000 | 50,000 |
💰 估算成本(价格从 anthropic.com/pricing 实时获取):
| 模型 | 估算成本 |
|---|
| claude-opus-4-6 | $152.40 |
| claude-sonnet-4-6 |
$18.72 |
| claude-haiku-4-5-20251001 | $0.53 |
|
总计 |
$171.65 |
保持语气友好且简洁。