claude-usage-cli
⚠️ DEPRECATED — This skill is no longer maintained. Please use claude-cost-cli instead, which provides the same functionality with active support.
A CLI for querying Anthropic Admin API usage and cost data. Requires an Admin API key (
sk-ant-admin...) from Claude Console → Settings → Admin Keys. Credentials are stored in macOS Keychain.
Installation
Requires Node.js >= 18 and macOS. The package is open source: https://github.com/cyberash-dev/claude-usage-cli
CODEBLOCK0
Install from source (if you prefer to audit the code before running):
CODEBLOCK1
After installation the claude-usage command is available globally.
Quick Start
CODEBLOCK2
API Key Management
Store API key (interactive masked prompt, validates sk-ant-admin prefix):
CODEBLOCK3
Show stored key (masked):
CODEBLOCK4
Remove key from Keychain:
CODEBLOCK5
Usage Reports
CODEBLOCK6
JSON output (for scripting):
CODEBLOCK7
Output columns: Date, Model, Input Tokens, Cached Tokens, Output Tokens, Web Searches.
Cost Reports
CODEBLOCK8
JSON output (for scripting):
CODEBLOCK9
Output columns: Date, Description, Model, Amount (USD), Token Type, Tier.
Flag Reference
usage
| Flag | Description | Default |
|---|
| INLINECODE5 | Start date (YYYY-MM-DD or ISO) | 7 days ago |
| INLINECODE6 |
End date (YYYY-MM-DD or ISO) | now |
|
--period <days> | Shorthand period (7d, 30d, 90d) | 7d |
|
--model <models> | Filter by model(s), comma-separated | all |
|
--api-keys <ids> | Filter by API key ID(s), comma-separated | all |
|
--group-by <fields> | Group by model, api
keyid, workspace
id, servicetier | model |
|
--bucket <width> | Bucket width: 1d, 1h, 1m | 1d |
|
--json | Output as JSON | false |
cost
| Flag | Description | Default |
|---|
| INLINECODE14 | Start date (YYYY-MM-DD or ISO) | 7 days ago |
| INLINECODE15 |
End date (YYYY-MM-DD or ISO) | now |
|
--period <days> | Shorthand period (7d, 30d, 90d) | 7d |
|
--group-by <fields> | Group by workspace_id, description | description |
|
--sum | Output total cost only | false |
|
--json | Output as JSON | false |
Security and Data Storage
- - Admin API key: stored exclusively in macOS Keychain (service:
claude-usage-cli). Never written to disk in plaintext. - No config files: all settings are passed via CLI flags. Nothing is stored on disk besides the Keychain entry.
- Network: the API key is only sent to
api.anthropic.com over HTTPS. No other outbound connections are made. - Scope: the Admin API key grants read-only access to organization usage and cost data. It cannot modify billing, create API keys, or access conversation content.
- No caching: query results are not cached or persisted to disk.
API Reference
This CLI wraps the Anthropic Admin API:
- - Usage: INLINECODE22
- Cost: INLINECODE23
Documentation: https://platform.claude.com/docs/en/build-with-claude/usage-cost-api
claude-usage-cli
⚠️ 已弃用 — 此技能不再维护。请改用 claude-cost-cli,它提供相同的功能并持续获得支持。
用于查询 Anthropic Admin API 使用量和成本数据的命令行工具。需要从 Claude 控制台 → 设置 → 管理员密钥获取管理员 API 密钥(sk-ant-admin...)。凭据存储在 macOS 钥匙串中。
安装
需要 Node.js >= 18 和 macOS。该包是开源的:https://github.com/cyberash-dev/claude-usage-cli
bash
npm install -g claude-usage-cli
从源码安装(如果你希望在运行前审查代码):
bash
git clone https://github.com/cyberash-dev/claude-usage-cli.git
cd claude-usage-cli
npm install && npm run build && npm link
安装后,claude-usage 命令即可全局使用。
快速开始
bash
claude-usage config set-key # 交互式提示:输入管理员 API 密钥(掩码显示)
claude-usage usage # 最近 7 天的令牌使用量
claude-usage cost # 最近 7 天的成本明细
claude-usage cost --sum # 最近 7 天的总支出
API 密钥管理
存储 API 密钥(交互式掩码提示,验证 sk-ant-admin 前缀):
bash
claude-usage config set-key
显示已存储的密钥(掩码显示):
bash
claude-usage config show
从钥匙串中移除密钥:
bash
claude-usage config remove-key
使用量报告
bash
claude-usage usage # 最近 7 天,按天统计,按模型分组
claude-usage usage --period 30d # 最近 30 天
claude-usage usage --from 2026-01-01 --to 2026-01-31 # 自定义日期范围
claude-usage usage --model claude-sonnet-4 # 按模型筛选
claude-usage usage --api-keys apikey01Rj,apikey02Xz # 按 API 密钥 ID 筛选
claude-usage usage --group-by model,apikeyid # 按多个维度分组
claude-usage usage --bucket 1h # 按小时粒度统计(1d、1h、1m)
JSON 输出(用于脚本处理):
bash
claude-usage usage --json
claude-usage usage --period 30d --json
输出列:日期、模型、输入令牌、缓存令牌、输出令牌、网络搜索次数。
成本报告
bash
claude-usage cost # 最近 7 天,按描述分组
claude-usage cost --period 30d # 最近 30 天
claude-usage cost --from 2026-01-01 --to 2026-01-31 # 自定义日期范围
claude-usage cost --group-by workspace_id,description # 按工作区和描述分组
claude-usage cost --sum # 仅显示总成本
JSON 输出(用于脚本处理):
bash
claude-usage cost --json
claude-usage cost --sum --json
输出列:日期、描述、模型、金额(美元)、令牌类型、层级。
参数参考
usage
| 参数 | 描述 | 默认值 |
|---|
| --from <date> | 开始日期(YYYY-MM-DD 或 ISO 格式) | 7 天前 |
| --to <date> |
结束日期(YYYY-MM-DD 或 ISO 格式) | 现在 |
| --period
| 快捷时间段(7d、30d、90d) | 7d |
| --model | 按模型筛选,逗号分隔 | 全部 |
| --api-keys | 按 API 密钥 ID 筛选,逗号分隔 | 全部 |
| --group-by | 按 model、apikeyid、workspaceid、servicetier 分组 | model |
| --bucket | 时间桶宽度:1d、1h、1m | 1d |
| --json | 以 JSON 格式输出 | false |
cost
| 参数 | 描述 | 默认值 |
|---|
| --from <date> | 开始日期(YYYY-MM-DD 或 ISO 格式) | 7 天前 |
| --to <date> |
结束日期(YYYY-MM-DD 或 ISO 格式) | 现在 |
| --period | 快捷时间段(7d、30d、90d) | 7d |
| --group-by | 按 workspace_id、description 分组 | description |
| --sum | 仅输出总成本 | false |
| --json | 以 JSON 格式输出 | false |
安全与数据存储
- - 管理员 API 密钥:仅存储在 macOS 钥匙串中(服务名称:claude-usage-cli)。绝不会以明文形式写入磁盘。
- 无配置文件:所有设置均通过 CLI 参数传递。除钥匙串条目外,磁盘上不存储任何内容。
- 网络:API 密钥仅通过 HTTPS 发送至 api.anthropic.com。不会建立其他出站连接。
- 范围:管理员 API 密钥仅授予对组织使用量和成本数据的只读访问权限。无法修改账单、创建 API 密钥或访问对话内容。
- 无缓存:查询结果不会被缓存或持久化到磁盘。
API 参考
此 CLI 封装了 Anthropic Admin API:
- - 使用量:GET /v1/organizations/usagereport/messages
- 成本:GET /v1/organizations/costreport
文档:https://platform.claude.com/docs/en/build-with-claude/usage-cost-api