AI Usage Check
Quick Start
CODEBLOCK0
Requirements
- - Python 3.10+ (no pip dependencies — stdlib only)
- Claude Code installed and authenticated (
claude CLI in PATH) — for Anthropic quota - OpenClaw installed — for session log token/cost stats
What It Shows
Anthropic (real quota from OAuth API):
- - Weekly utilization % with reset countdown
- 5-hour rolling window %
- Model-specific weekly (Sonnet, Opus) when available
- Extra usage spend vs monthly limit
Other Providers (from OpenClaw session logs):
- - Token counts and call counts per model
- Auto-detects any provider OpenClaw routes through (Ollama, OpenAI, etc.)
OpenClaw Anthropic breakdown:
- - Per-model token counts and equivalent API cost
How It Works
- - Anthropic quota:
GET https://api.anthropic.com/api/oauth/usage using Claude Code's OAuth token from ~/.claude/.credentials.json (requires user:profile scope) - Token auto-refresh: If the token is expired, the script automatically refreshes it by invoking
claude --print -p "ok" (Claude Code refreshes its own OAuth token on any invocation), then re-reads the updated credentials file. If Claude Code isn't installed, Anthropic quota is skipped gracefully. - Session stats: Parses
~/.openclaw/agents/main/sessions/*.jsonl for per-provider/model token and cost data
Environment Variables
| Variable | Default | Description |
|---|
| INLINECODE6 | INLINECODE7 | OpenClaw session log directory |
| INLINECODE8 |
~/.claude/.credentials.json | Claude Code credentials file |
Tips
- - Run via Haiku or a lightweight model for heartbeat/background checks — the script just needs to execute, no reasoning required
- Use
--json for programmatic consumption (cron jobs, dashboards, alerts) - Works with any Anthropic subscription tier (Pro, Max, Team, etc.)
AI 使用量检查
快速开始
bash
python3 scripts/usage_check.py # 带仪表盘的精美报告
python3 scripts/usage_check.py --json # 适用于脚本处理的JSON输出
环境要求
- - Python 3.10+(无需pip依赖——仅使用标准库)
- Claude Code 已安装并完成认证(claude CLI 在 PATH 中)——用于 Anthropic 配额
- OpenClaw 已安装——用于会话日志的令牌/成本统计
显示内容
Anthropic(来自 OAuth API 的真实配额):
- - 每周使用率百分比及重置倒计时
- 5小时滚动窗口百分比
- 特定模型的每周使用情况(Sonnet、Opus,如可用)
- 额外使用支出与月度限额对比
其他提供商(来自 OpenClaw 会话日志):
- - 每个模型的令牌数量和调用次数
- 自动检测 OpenClaw 路由的任何提供商(Ollama、OpenAI 等)
OpenClaw Anthropic 细分:
工作原理
- - Anthropic 配额: 使用 Claude Code 的 OAuth 令牌(来自 ~/.claude/.credentials.json)执行 GET https://api.anthropic.com/api/oauth/usage(需要 user:profile 作用域)
- 令牌自动刷新: 如果令牌已过期,脚本通过调用 claude --print -p ok 自动刷新(Claude Code 在任何调用中都会刷新自己的 OAuth 令牌),然后重新读取更新后的凭据文件。如果未安装 Claude Code,则优雅地跳过 Anthropic 配额。
- 会话统计: 解析 ~/.openclaw/agents/main/sessions/*.jsonl 以获取每个提供商/模型的令牌和成本数据
环境变量
| 变量 | 默认值 | 描述 |
|---|
| OPENCLAWSESSIONSDIR | ~/.openclaw/agents/main/sessions | OpenClaw 会话日志目录 |
| CLAUDECREDENTIALSPATH |
~/.claude/.credentials.json | Claude Code 凭据文件 |
提示
- - 通过 Haiku 或轻量级模型运行以进行心跳/后台检查——脚本只需执行,无需推理
- 使用 --json 进行程序化消费(cron 任务、仪表盘、告警)
- 适用于任何 Anthropic 订阅层级(Pro、Max、Team 等)