微算力 · API 导航与自营网关
API 接入 EN
未登录登录
📊

Claude 用量成本一键查

claude-usage-cli · v0.1.0
效率 安全 A 级 作者 cyberash-dev 公开目录收录
下载 2k 安装 0 收藏 5.8k 浏览 12
原始页面

简介

命令行查询 Claude 企业级用量与成本数据,支持表格/JSON 输出,Admin API 密钥安全存储于 macOS 钥匙串。

原始描述

Query Claude API usage and cost reports from the command line. Secure macOS Keychain storage for Admin API key. Table/JSON output.

中文使用说明 来自收录源

核心用法

claude-usage-cli 是面向 Anthropic Claude Console 管理员的命令行工具,用于查询组织级 API 用量与成本报告。用户需先在 Claude Console 生成 Admin API 密钥(sk-ant-admin...),通过 claude-usage config set-key 交互式录入后,即可执行 claude-usage usage 或 claude-usage cost 获取数据。

支持灵活的查询维度:按模型、API Key ID、工作空间分组;按日/小时/分钟粒度聚合;自定义时间范围;输出格式可在易读的 ASCII 表格与 JSON 之间切换,便于对接自动化脚本或 BI 系统。

显著优点

  1. 安全设计:Admin API 密钥仅存储于 macOS Keychain,不落盘、不写配置文件,降低密钥泄露风险。
  2. 权限最小化:Admin Key 为只读权限,无法修改账单、创建密钥或读取对话内容,遵循最小权限原则。
  3. 开源可审计:代码托管于 GitHub,支持从源码构建,满足安全审计需求。
  4. 企业级查询能力:支持多维度分组(model/api_key_id/workspace_id/service_tier)和细粒度时间桶(1h/1m),满足复杂成本分摊场景。

潜在缺点与局限性

  • 平台限制:仅支持 macOS,Windows/Linux 用户无法使用。
  • Node.js 依赖:要求 Node >= 18,环境配置可能增加运维成本。
  • 功能单一:专注于只读报表,不支持预算告警、实时推送或预测分析。
  • 网络直连:无代理配置选项说明,受限网络环境可能需要额外配置。

适合人群

  • Claude Enterprise/Team 计划的管理员与财务运维人员
  • 需要自动化成本分摊与用量审计的 DevOps/SRE 团队
  • 偏好命令行工作流、注重密钥安全存储的技术团队

常规风险

  • 密钥管理:尽管使用 Keychain,若 macOS 账户被攻破,密钥仍存在暴露风险;建议定期轮换 Admin Key。
  • 数据隐私:查询数据流经本地终端,但依赖 Anthropic 云端 API,敏感组织需确认合规要求。
  • 误操作风险:--json 输出若未加筛选,可能在大组织场景下产生巨量数据,导致终端卡顿或内存问题。
  • 供应链风险:npm 全局安装需信任 registry 及构建产物,建议锁定版本并校验 checksum。

技能正文


name: claude-usage-cli description: Query Claude API usage and cost reports from the command line. Secure macOS Keychain storage for Admin API key. Table/JSON output. metadata: {"clawdbot":{"emoji":"📊","os":["macos"],"requires":{"bins":["claude-usage","node"]},"install":[{"id":"npm","kind":"shell","command":"npm install -g claude-usage-cli","bins":["claude-usage"],"label":"Install claude-usage-cli via npm"}],"source":"https://github.com/cyberash-dev/claude-usage-cli"}}

claude-usage-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 open source: https://github.com/cyberash-dev/claude-usage-cli

npm install -g claude-usage-cli

Install from source (if you prefer to audit the code before running):

git clone https://github.com/cyberash-dev/claude-usage-cli.git
cd claude-usage-cli
npm install && npm run build && npm link

After installation the claude-usage command is available globally.

Quick Start

claude-usage config set-key     # Interactive prompt: enter Admin API key (masked)
claude-usage usage              # Token usage for the last 7 days
claude-usage cost               # Cost breakdown for the last 7 days
claude-usage cost --sum         # Total spend for the last 7 days

API Key Management

Store API key (interactive masked prompt, validates sk-ant-admin prefix):

claude-usage config set-key

Show stored key (masked):

claude-usage config show

Remove key from Keychain:

claude-usage config remove-key

Usage Reports

claude-usage usage                                    # Last 7 days, daily, grouped by model
claude-usage usage --period 30d                       # Last 30 days
claude-usage usage --from 2026-01-01 --to 2026-01-31 # Custom date range
claude-usage usage --model claude-sonnet-4            # Filter by model
claude-usage usage --api-keys apikey_01Rj,apikey_02Xz # Filter by API key IDs
claude-usage usage --group-by model,api_key_id        # Group by multiple dimensions
claude-usage usage --bucket 1h                        # Hourly granularity (1d, 1h, 1m)

JSON output (for scripting):

claude-usage usage --json
claude-usage usage --period 30d --json

Output columns: Date, Model, Input Tokens, Cached Tokens, Output Tokens, Web Searches.

Cost Reports

claude-usage cost                                           # Last 7 days, grouped by description
claude-usage cost --period 30d                              # Last 30 days
claude-usage cost --from 2026-01-01 --to 2026-01-31        # Custom date range
claude-usage cost --group-by workspace_id,description       # Group by workspace and description
claude-usage cost --sum                                     # Total cost only

JSON output (for scripting):

claude-usage cost --json
claude-usage cost --sum --json

Output columns: Date, Description, Model, Amount (USD), Token Type, Tier.

Flag Reference

usage

Flag Description Default
--from <date> Start date (YYYY-MM-DD or ISO) 7 days ago
--to <date> 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_key_id, workspace_id, service_tier model
--bucket <width> Bucket width: 1d, 1h, 1m 1d
--json Output as JSON false

cost

Flag Description Default
--from <date> Start date (YYYY-MM-DD or ISO) 7 days ago
--to <date> 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: GET /v1/organizations/usage_report/messages
  • Cost: GET /v1/organizations/cost_report

Documentation: https://platform.claude.com/docs/en/build-with-claude/usage-cost-api

正文来自原始发布页, 版权归作者所有,许可未标注。 权利人认为有不妥,可申请下架。

其他来源收录

同一技能在不同目录的原始页面:

标签

clicost-managementapi-monitoringanthropicmacoskeychainreporting

同类 Skill