Metadata-Version: 2.4
Name: cursor-usage
Version: 0.3.0
Summary: CLI for Cursor usage, spend and cycle limits, with per-day breakdowns, CSV export and warning thresholds.
Author: cursor-usage contributors
License: MIT
Project-URL: Homepage, https://github.com/javaisbetterthanpython/cursor-usage
Project-URL: Issues, https://github.com/javaisbetterthanpython/cursor-usage/issues
Keywords: cursor,usage,spend,cli,ai,tokens,llm,rate-limit,subscription
Classifier: Development Status :: 4 - Beta
Classifier: Environment :: Console
Classifier: Intended Audience :: Developers
Classifier: License :: OSI Approved :: MIT License
Classifier: Operating System :: OS Independent
Classifier: Programming Language :: Python :: 3
Classifier: Topic :: Utilities
Requires-Python: >=3.8
Description-Content-Type: text/markdown
License-File: LICENSE
Provides-Extra: keyring
Requires-Dist: keyring>=24; extra == "keyring"
Dynamic: license-file

<h1 align="center">📊 cursor-usage</h1>

<p align="center">
  <b>Your Cursor usage, spend, and cycle limits — right from your terminal.</b>
</p>

<p align="center">
  <a href="https://pypi.org/project/cursor-usage/"><img alt="PyPI" src="https://img.shields.io/pypi/v/cursor-usage?color=blue&label=PyPI"></a>
  <img alt="Python" src="https://img.shields.io/badge/python-3.8%2B-blue">
  <img alt="Platforms" src="https://img.shields.io/badge/platform-macOS%20%7C%20Linux%20%7C%20Windows-lightgrey">
  <img alt="Dependencies" src="https://img.shields.io/badge/dependencies-zero-brightgreen">
  <img alt="License" src="https://img.shields.io/badge/license-MIT-green">
</p>

<p align="center">
  <img src="https://raw.githubusercontent.com/javaisbetterthanpython/cursor-usage/main/docs/demo.gif" alt="cursor-usage demo" width="760">
</p>

---

Cursor shows your usage only on its web dashboard. **`cursor-usage` gives you
usage, spend, and cycle limits from the shell** — for scripts, hooks, and AI agents
deciding when to back off, plus per-day reports and CSV exports. It uses the Cursor
session already on your machine.

```text
$ cursor-usage --format short
composer 37% · other 12%
```

## ✨ Features

- **📊 Usage and spend** — per-model tokens and compute value for the current billing month or a custom time window.
- **📈 Cycle limits** — percentages used and remaining for Composer / Auto and Other models, plus included spend and the reset date when reported.
- **🚦 Thresholds for scripts** — `--warn [N]` exits 1 when either cycle bucket reaches N%.
- **📅 Per-day breakdown** — `--by-day` groups usage events by local calendar day.
- **💾 CSV export** — `--csv` saves individual events with timestamps, models, tokens, and cost.
- **🧾 Plain, JSON, or short** — readable reports, raw data, and a one-line status.
- **🌍 Cross-platform** — macOS, Linux, and Windows; Python 3.8+.
- **🔋 Zero dependencies** — pure Python standard library.
- **🔒 Local & private** — reads your existing Cursor session and sends it only to Cursor.

## 🚀 Quickstart

```bash
pip install cursor-usage
cursor-usage
```

Run `cursor-agent login`, sign in at `cursor.com` in Firefox, or use an existing
Cursor app session on this machine.

## 🧑‍💻 Usage

| Command | What it does |
|---|---|
| `cursor-usage` | Usage summary for the current billing month and cycle limits when available |
| `cursor-usage --by-day` | Add a per-day breakdown in plain format |
| `cursor-usage --csv FILE` | Export usage events in the selected window to CSV in plain format |
| `cursor-usage --days N` | Show usage from the last N days |
| `cursor-usage --month YYYY-MM` | Show usage from a specific UTC calendar month, up to the current time |
| `cursor-usage --start YYYY-MM-DD` | Set the inclusive start date in UTC |
| `cursor-usage --end YYYY-MM-DD` | Set the inclusive end date in UTC |
| `cursor-usage --page-size N` | Set events per API page for `--by-day` and `--csv` (default 1000) |
| `cursor-usage --warn [N]` | Exit 1 when either cycle bucket reaches N% (default 70) |
| `cursor-usage --format {plain,json,short}` | Choose output format (default plain) |
| `cursor-usage --json` | Print raw aggregated-usage JSON (alias for `--format json`) |
| `cursor-usage --limits-json` | Print raw current cycle-limits JSON, overriding `--format` |
| `cursor-usage -v`, `--verbose` | Log the credential source, expiry, rejections, and optional limits failures to stderr |
| `cursor-usage -V`, `--version` | Print the version |
| `cursor-usage -h`, `--help` | Show help |

Combine flags with a command such as `cursor-usage --by-day --csv june.csv --month 2026-06`.
When both `--json` and `--format` appear, the last one wins. The usage start
comes from `--start`, then `--days` (counting back from now), then `--month`,
then the current billing month. `--end` overrides the end boundary. After
validating the session, short format and `--limits-json` fetch cycle limits.
Cycle limits and warnings always use the current billing cycle.

<details>
<summary><b>📊 Example: <code>cursor-usage</code></b></summary>

```text
==============================================================================
CURSOR USAGE  |  you@example.com  |  2026-06-04 -> 2026-06-09
==============================================================================
Included value used : $875.81   (compute consumed; included in plan)
Tokens   in=126,033,860  out=18,989,068
         cacheRead=2,492,781,450  cacheWrite=8,322,520
------------------------------------------------------------------------------
model                                  $ value        in tok     out tok
------------------------------------------------------------------------------
composer-2.5                            460.14    95,638,983  13,315,470
claude-4.6-opus-high                    233.71       786,434   1,059,451
gemini-3.5-flash                         45.77    16,097,065   1,075,904
gpt-5.4-high                             43.65     4,069,043   1,041,967
...

==============================================================================
CURSOR CYCLE LIMITS  |  resets 2026-07-04
==============================================================================
Composer / Auto [###########...................]   36.7% used,   63.3% left
Other models    [####..........................]   12.4% used,   87.6% left
Included usage  $146.80 of $400.00 used  ($253.20 left)
```
</details>

<details>
<summary><b>📅 Example: <code>--by-day</code></b></summary>

```text
==============================================================================
CURSOR USAGE BY DAY  |  you@example.com  |  2026-06-04 -> 2026-06-09
==============================================================================
date           events     $ value         in tok      out tok
------------------------------------------------------------------------------
2026-06-04        433      324.46     38,602,275    6,080,617
2026-06-05        368      252.58     40,616,543    6,469,309
2026-06-06        416      121.06     29,686,247    4,497,010
2026-06-07        444      177.71     17,128,795    1,942,132
------------------------------------------------------------------------------
TOTAL           1,661      875.81    126,033,860   18,989,068
```
</details>

<details>
<summary><b>💾 Example: <code>--csv usage.csv</code></b></summary>

The CSV has one row per usage event, sorted by timestamp.

```text
datetime_local,timestamp_ms,date,model,kind,input_tokens,output_tokens,cache_read_tokens,cache_write_tokens,value_cents,charged_cents,requests_costs,is_headless,owning_user
```
</details>

## 🤖 Using it from scripts and agents

```bash
cursor-usage --warn 70 || echo "back off"
cursor-usage --format short
cursor-usage --json
```

The threshold check uses the reported percentages before rounding. An exact
match triggers the warning. One reported percentage is enough to check the
threshold. If both endpoints lack usable percentages, `--warn` reports an
error unless the plan reports unlimited usage. Short format rounds to whole
percentages and displays `unlimited` for an unlimited plan.

The command exits **0** on success, **1** when the threshold is reached or an
API or data error occurs, and **2** for invalid arguments or when no session
can be authenticated. A threshold match leaves the
requested output on stdout. Diagnostics go to stderr.

## 🤔 How it works

The CLI finds your local Cursor web session and asks Cursor's dashboard API for
usage. The aggregated endpoint provides per-model totals. Cycle limits come
from the current-period endpoint, with the usage-summary endpoint as a
fallback. Per-day and CSV reports fetch the per-event log in pages.

The CLI collects sessions from all available sources and tries them in this
priority order. Within each group, it tries the latest expiry first.

1. **Overrides** from `CURSOR_SESSION_TOKEN` and the session file. Set
   `CURSOR_USAGE_SESSION_FILE` to choose the file path. The default is
   `~/.config/cursor-usage/session` on macOS and Linux or
   `%APPDATA%/cursor-usage/session` on Windows. On macOS and Linux, use
   `chmod 600` on the file. The CLI skips files readable by group or others.
2. **Saved sign-ins** from macOS Keychain, the `cursor-agent login` file, and
   the optional OS keyring. On Linux, the login file is
   `$XDG_CONFIG_HOME/cursor/auth.json` or `~/.config/cursor/auth.json` by
   default. On Windows, the CLI tries `%APPDATA%/cursor/auth.json` when
   available. OS keyring lookup waits up to five seconds.
3. **Firefox** dashboard cookies across profiles, including Snap on Linux.
4. **Cursor or Cursor Nightly** sessions from the app's SQLite state database.

The CLI skips Agent API tokens, encrypted or invalid credentials, and sessions
that expire within 60 seconds. It checks each distinct candidate once against
Cursor's identity endpoint and falls through if Cursor rejects it. With `-v`,
the CLI reports rejections and the source and UTC expiry of the accepted
session. If no session works, it lists each source's status and suggests
`cursor-agent login`.

For manual setup, open the Cursor dashboard in your browser and look under
Application or Storage in developer tools for the `WorkosCursorSessionToken`
cookie. The environment override and session file accept the cookie value
(`id::jwt`, including its URL-encoded form) or a raw session JWT. Put just the
value in the file. In Bash, you can set the environment override without
displaying the value or adding it to shell history.

```bash
read -rs CURSOR_SESSION_TOKEN
export CURSOR_SESSION_TOKEN
cursor-usage
unset CURSOR_SESSION_TOKEN
```

For the protocol and a recipe to rebuild the tool, see
**[docs/HOW_THIS_WAS_BUILT.md](docs/HOW_THIS_WAS_BUILT.md)**.

## 🔒 Privacy & security

- Reads your existing local session. The Firefox lookup uses a private temporary
  copy of the cookie database and its write-ahead log, then removes the copy.
- Sends the session cookie only to **`cursor.com`**. HTTP redirects are blocked.
- Keeps the session out of output and errors, including with `-v`.
- CSV exports contain your usage data. Git ignores `*.csv` in this repo.
- **No telemetry. No third-party calls.**

## ⚠️ Good to know

- **`$ value` measures compute consumed.** With usage-based pricing off, the flat subscription covers those figures.
- The dashboard API is **undocumented** and Cursor may change it.
- Personal Cursor API keys authenticate the Agent API. Usage requires a Cursor web session.
- If your session expires, sign back in at `cursor.com`. Refresh any session
  value you supplied manually before running the command again.

## 🛠️ Install options

```bash
pip install cursor-usage            # from PyPI
pipx install cursor-usage           # isolated command installation
pip install .                       # from a local clone
pip install "cursor-usage[keyring]" # optional OS keyring lookup
```

Requires Python 3.8+.

## 🤝 Contributing

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

```bash
pip install pytest && pytest -q
```

Tests use temporary files 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](https://github.com/asciinema/agg/releases) and the DejaVu Sans Mono font.
The recorder runs the CLI against illustrative responses with HTTP disabled.
Set `CURSOR_USAGE_DEMO_FONT_DIR` if the font is outside agg's font search path.
The README embeds the resulting `docs/demo.gif`.

## 📄 License

[MIT](LICENSE) — do whatever you like.
