Skip to main content

📊 claude-usage

Your Claude subscription limits — right from your terminal.

PyPI Python Platforms Dependencies License

claude-usage demo


Claude Code shows your subscription limits only in the interactive /usage screen. claude-usage gives you subscription usage and model limits from the shell — for scripts, hooks, and AI agents deciding when to back off. It uses the Claude Code login already on your machine. No API key to manage, nothing to configure.

The 5-hour window appears when your plan has one and the usage source reports it. Verified Ultra (formerly Max 20x) accounts have weekly limits only. Usage-based plans such as Enterprise show spend instead of percentages.

Install claude-usage-limits from PyPI; the command is claude-usage.

$ claude-usage
5-hour: 23% used, resets 17:40 (in 2h13m)
7-day: 59% used, resets Sun 03:00 (in 12h29m)

✨ Features

  • Usage windows — 5-hour usage when available, 7-day usage, populated weekly variants, and local reset times.
  • 🚦 Thresholds for scripts — --warn [N] checks the 5-hour window when available, all weekly limits, and spend percentage. --warn-7d [N] checks overall weekly usage. Both exit 1 at or above N%.
  • 📊 Extra detail — --all shows per-model limits, the active window, and the weekly surface split.
  • 📟 Status line — a compact model and usage line that also feeds the documented fallback source.
  • 🧾 Plain, JSON, short, or menubar — output for the terminal, scripts, and SwiftBar.
  • 🌍 Cross-platform — macOS, Linux, and Windows; Python 3.9+.
  • 🔋 Zero dependencies — pure Python standard library.
  • 🔒 Local & private — reads your existing login; no telemetry or third-party calls.

🚀 Quickstart

pip install claude-usage-limits
claude-usage

Sign in with claude on this machine first. Any running Claude Code session keeps its access token fresh.

🧑‍💻 Usage

Command What it does
claude-usage Available usage windows and their reset times
claude-usage --json Raw usage JSON from whichever source answered (alias for --format json)
claude-usage --warn [N] Exit 1 when the 5-hour window, any weekly limit, or spend percentage reaches N% (default 70)
claude-usage --warn-7d [N] Exit 1 at or above N% of the 7-day window (default 70)
claude-usage --all Add per-limit details and weekly surface breakdown in plain format
claude-usage --format {plain,json,short,menubar} Choose output format (default plain)
claude-usage --format menubar Print SwiftBar/xbar plugin output
claude-usage --max-age SECONDS Set the fresh cache age (default 300, clamped to 30–3600)
claude-usage --live Prefer a fresh status-line cache from an active Claude Code session (Pro/Max)
claude-usage --statusline Read Claude Code status-line JSON on stdin, cache it, print one compact line (hidden from help)
claude-usage -v, --verbose Print credential and usage sources and the plan, when available, to stderr
claude-usage -V, --version Print the version
claude-usage -h, --help Show help

Flags combine: claude-usage --format short --warn 70 --warn-7d 90. --json and --format set the same option; the last one wins. --all adds detail only in plain format, keeping JSON machine-readable and short output on one line.

📊 Example: --all
5-hour: 23% used, resets 17:40 (in 2h13m)
7-day: 60% used, resets Sun 03:00 (in 12h11m)
kind             percent  severity   active  model        reset
------------------------------------------------------------------------------
weekly_all           60%  normal     active               resets Sun 03:00 (in 12h11m)
weekly_scoped         9%  normal             Fable        resets Sun 03:00 (in 12h11m)
Claude Code 96%, Cowork 4%, Chats 0%, Other 0%

Detail fields depend on the account and endpoint response. With a status-line answer, this prints (details not available from the status-line source) instead.

🤖 Using it from scripts and agents

claude-usage --warn 70 || echo "back off"
claude-usage --warn 70 --warn-7d 90 --format short
claude-usage --json

The usual output is always printed. Exit codes: 0 for success, 1 when either threshold trips or no source answers, 2 for an argument error. The threshold is inclusive: --warn exits 1 at 70%, including exactly 70%. Missing or null windows do not trigger warnings. --warn includes session and weekly entries in limits[], and spend percentage when there is a spend limit. --warn-7d checks only seven_day. Populated seven_day_* usage windows appear in plain and short output and are checked by --warn. JSON preserves the source payload, including null fields and weekly share indices.

📟 Claude Code status line

Set statusLine in ~/.claude/settings.json (merge this field into your existing settings):

{
  "statusLine": {"type": "command", "command": "claude-usage --statusline"}
}

Claude Code pipes its documented status JSON to the command on each repaint:

Fable 5.1 · 5h 25% · 7d 59%

Missing parts are omitted. This mode never looks up credentials or makes a network call. It also writes last-statusline.json, feeding the documented fallback when the endpoint is unavailable. Invalid input produces no output and exits 0. claude-usage --format short gives the same compact format from the normal source order; endpoint answers usually have no model, so show 5h 23% · 7d 59%.

🖥️ SwiftBar menu bar

$ claude-usage --format menubar
5h 23% · 7d 59% | sfimage=gauge.with.dots.needle.50percent
---
5-hour: 23% used, resets 17:40 (in 2h13m)
7-day: 59% used, resets Sun 03:00 (in 12h29m)
weekly_scoped (Fable): 9%
Source: endpoint | size=11

The title turns orange at 70% and red at 90%. On usage-based plans it shows the spend, e.g. $12.34. If every source fails, the item shows Claude ⚠ with the reason.

Save claude-usage.1m.sh in your SwiftBar plugin folder and make it executable.

#!/bin/sh
claude-usage --format menubar --live --max-age 60

SwiftBar uses a minimal PATH, so replace claude-usage in the script with its absolute path. Set up the Claude Code status line above to get live updates.

🤔 How it works

Sources are tried in this order:

  1. last-usage.json if less than 5 minutes old by default.
  2. One live GET https://api.anthropic.com/api/oauth/usage, saved on success.
  3. last-usage.json if less than 60 minutes old.
  4. last-statusline.json if less than 60 minutes old and it has rate_limits.

--max-age sets the fresh cache age for step 1 and --live. With --live, a fresh status-line cache is tried first if the endpoint cache is older or missing.

Both caches live in ~/.claude, or CLAUDE_CONFIG_DIR when set. Cache writes are atomic. Cached answers at least a minute old show their source and age, e.g. (from the endpoint, 3 min ago); status-line answers always show their age. If every source fails, the distinct reasons appear once each on stderr.

The endpoint returns HTTP 429 on back-to-back calls, sometimes blocking for minutes. Five-minute reuse avoids repeating requests; there are no retries or polling, and the single request times out after 15 seconds.

🔍 Where credentials live (per OS)

Resolution is local-only, in this order:

  1. CLAUDE_CODE_OAUTH_TOKEN environment override, used as-is.
  2. macOS Keychain service Claude Code-credentials, read with security find-generic-password -s "Claude Code-credentials" -w.
  3. The credentials file, including on macOS when the keychain is unavailable (for example, a locked keychain over SSH).
OS Credentials file
macOS / Linux ~/.claude/.credentials.json
Windows %USERPROFILE%\.claude\.credentials.json

CLAUDE_CONFIG_DIR changes the credentials file directory and both cache paths. The token is claudeAiOauth.accessToken; expiresAt is milliseconds since epoch. An expired token is refused: open claude so it refreshes.

For the protocol and a recipe to rebuild the tool, see docs/HOW_THIS_WAS_BUILT.md.

🔒 Privacy & security

  • Reads existing credentials only; never refreshes or writes credentials.
  • Sends the token only to api.anthropic.com, with the usage request.
  • Writes only the two local usage caches; never modifies Claude Code settings.
  • Never prints secrets or headers, including with -v.
  • No telemetry. No third-party calls.

⚠️ Good to know

  • The OAuth usage endpoint is undocumented and rate limited; it may change or disappear. The documented status-line path is the fallback.
  • Usage-based plans report spend. Subscription plans report usage percentages.
  • Status-line rate_limits, which powers --live, is Pro/Max only. It appears after the first API response, drops individual windows after they reset, and updates while a Claude Code session is active and repainting.
  • The observed endpoint responses came from Max and Enterprise accounts. Fields on other plans may differ; an API key is not a Claude Code OAuth login.
  • Any running claude keeps the access token fresh. This tool deliberately does not implement the undocumented refresh flow.
  • Use /feedback in Claude Code to ask Anthropic for an official claude usage command.

🛠️ Install options

pip install claude-usage-limits       # from PyPI
pipx install claude-usage-limits      # isolated command installation
pip install .                  # from a local clone

Requires Python 3.9+. The PyPI distribution is claude-usage-limits; the command and GitHub repo are claude-usage, and the import package is claude_usage. The PyPI names claude-usage, claude-usage-cli, and claude-code-usage belong to unrelated projects.

🤝 Contributing

Issues and PRs are welcome. After installing the local project with pip install -e ., run the tests with:

pip install pytest && pytest -q

Tests use temporary config directories and mocked HTTP; they never contact the endpoint or read your real credentials.

Regenerate the demo with python3 docs/record_demo.py after installing agg. The recorder runs the CLI against illustrative local caches and supplies status-line JSON on stdin; it needs no credentials or network access. The README embeds the resulting docs/demo.gif.

📄 License

MIT — do whatever you like.

Metadata

Release files for claude-usage-limits 0.3.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for claude-usage-limits 0.3.1
File Size Uploaded Provenance Actions
claude_usage_limits-0.3.1.tar.gz 26.1 kB Without provenance Details for claude_usage_limits-0.3.1.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for claude-usage-limits 0.3.1
File Interpreter ABI Platform Actions
claude_usage_limits-0.3.1-py3-none-any.whl Python 3 none any Details for claude_usage_limits-0.3.1-py3-none-any.whl

Total release size: 41.9 kB

Release files / claude_usage_limits-0.3.1.tar.gz

Download URL claude_usage_limits-0.3.1.tar.gz
Size 26.1 kB
Tags Source
SHA-256 checksum
How to use checksums
a9592a7b33fec3b180ff637bdb62c60e9db43f6375a3bf682b4866210fa32060
BLAKE2b-256 checksum
How to use checksums
0bfc0c71b2ac3a00b6111033ca8e809a10618521d311787e1f55802d5fcf8873
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.12.3

Release files / claude_usage_limits-0.3.1-py3-none-any.whl

Download URL claude_usage_limits-0.3.1-py3-none-any.whl
Size 15.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
1d6b6d7a58d6a6f0313fde482a1747bc0c4419ff83cfceb5339ee3dbe781e996
BLAKE2b-256 checksum
How to use checksums
52ad77c77f6f08916b5a9330fc336105748b24d4c7bbe11aec70d382b1ec0851
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.12.3

Security for claude-usage-limits 0.3.1

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page