claude-cost-cli
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 fully open source under the MIT license: https://github.com/cyberash-dev/claude-cost-cli
CODEBLOCK0
The npm package is published with provenance attestation, linking each release to its source commit via GitHub Actions. You can verify the published contents before installing:
CODEBLOCK1
Install from source (if you prefer to audit the code before running):
CODEBLOCK2
After installation the claude-cost command is available globally.
Quick Start
CODEBLOCK3
API Key Management
Store API key (interactive masked prompt, validates sk-ant-admin prefix):
CODEBLOCK4
Show stored key (masked):
CODEBLOCK5
Remove key from Keychain:
CODEBLOCK6
Usage Reports
CODEBLOCK7
JSON output (for scripting):
CODEBLOCK8
Output columns: Date, Model, Input Tokens, Cached Tokens, Output Tokens, Web Searches.
Cost Reports
CODEBLOCK9
JSON output (for scripting):
CODEBLOCK10
Output columns: Date, Description, Model, Amount (USD), Token Type, Tier.
Flag Reference
usage
| Flag | Description | Default |
|---|
| INLINECODE4 | Start date (YYYY-MM-DD or ISO) | 7 days ago |
| INLINECODE5 |
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 |
|---|
| INLINECODE13 | Start date (YYYY-MM-DD or ISO) | 7 days ago |
| INLINECODE14 |
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
The following properties are by design and can be verified in the source code:
- - Admin API key: stored exclusively in macOS Keychain (service:
claude-cost-cli). By design, never written to disk in plaintext. See src/infrastructure/keychain-credential-store.ts for the implementation. - No config files: all settings are passed via CLI flags. Nothing is stored on disk besides the Keychain entry.
- Network: by design, the API key is only sent to
api.anthropic.com over HTTPS. No other outbound connections are made. See src/infrastructure/anthropic-usage-repository.ts and src/infrastructure/anthropic-cost-repository.ts. - 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. This is a property of the Anthropic Admin API, not just this CLI.
- No caching: query results are not cached or persisted to disk. The CLI writes output to stdout only.
API Reference
This CLI wraps the Anthropic Admin API:
- - Usage: INLINECODE24
- Cost: INLINECODE25
Documentation: https://platform.claude.com/docs/en/build-with-claude/usage-cost-api
claude-cost-cli
一个用于查询Anthropic管理API使用量和成本数据的命令行工具。需要从Claude控制台→设置→管理密钥中获取管理API密钥(sk-ant-admin...)。凭证存储在macOS钥匙串中。
安装
需要Node.js >= 18和macOS。该包在MIT许可下完全开源:https://github.com/cyberash-dev/claude-cost-cli
bash
npm install -g claude-cost-cli
npm包发布时带有出处证明,通过GitHub Actions将每个版本链接到其源代码提交。您可以在安装前验证发布的内容:
bash
npm pack claude-cost-cli --dry-run
从源代码安装(如果您希望在运行前审计代码):
bash
git clone https://github.com/cyberash-dev/claude-cost-cli.git
cd claude-cost-cli
npm install && npm run build && npm link
安装后,claude-cost命令即可全局使用。
快速开始
bash
claude-cost config set-key # 交互式提示:输入管理API密钥(掩码显示)
claude-cost usage # 最近7天的令牌使用量
claude-cost cost # 最近7天的成本明细
claude-cost cost --sum # 最近7天的总花费
API密钥管理
存储API密钥(交互式掩码提示,验证sk-ant-admin前缀):
bash
claude-cost config set-key
显示已存储的密钥(掩码显示):
bash
claude-cost config show
从钥匙串中移除密钥:
bash
claude-cost config remove-key
使用量报告
bash
claude-cost usage # 最近7天,按天统计,按模型分组
claude-cost usage --period 30d # 最近30天
claude-cost usage --from 2026-01-01 --to 2026-01-31 # 自定义日期范围
claude-cost usage --model claude-sonnet-4 # 按模型筛选
claude-cost usage --api-keys apikey01Rj,apikey02Xz # 按API密钥ID筛选
claude-cost usage --group-by model,apikeyid # 按多个维度分组
claude-cost usage --bucket 1h # 按小时粒度(1d、1h、1m)
JSON输出(用于脚本):
bash
claude-cost usage --json
claude-cost usage --period 30d --json
输出列:日期、模型、输入令牌、缓存令牌、输出令牌、网络搜索次数。
成本报告
bash
claude-cost cost # 最近7天,按描述分组
claude-cost cost --period 30d # 最近30天
claude-cost cost --from 2026-01-01 --to 2026-01-31 # 自定义日期范围
claude-cost cost --group-by workspace_id,description # 按工作区和描述分组
claude-cost cost --sum # 仅显示总成本
JSON输出(用于脚本):
bash
claude-cost cost --json
claude-cost 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参考
此CLI封装了Anthropic管理API:
- - 使用量:GET /v1/organizations/usagereport/messages
- 成本:GET /v1/organizations/costreport
文档:https://platform.claude.com/docs/en/build-with-claude/usage-cost-api