Metadata-Version: 2.4
Name: gemini-usage-limits
Version: 0.1.0
Summary: CLI for Antigravity plan usage and rate limits, with Antigravity CLI fallback and warning thresholds.
Author: gemini-usage contributors
License-Expression: MIT
Project-URL: Homepage, https://github.com/javaisbetterthanpython/gemini-usage
Project-URL: Issues, https://github.com/javaisbetterthanpython/gemini-usage/issues
Keywords: gemini,google,antigravity,usage,rate-limit,weekly-usage,subscription,cli
Classifier: Development Status :: 4 - Beta
Classifier: Environment :: Console
Classifier: Intended Audience :: Developers
Classifier: Operating System :: OS Independent
Classifier: Programming Language :: Python :: 3
Classifier: Topic :: Utilities
Requires-Python: >=3.9
Description-Content-Type: text/markdown
License-File: LICENSE
Dynamic: license-file

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

<p align="center">
  <b>Your Antigravity plan limits — right from your terminal.</b>
</p>

<p align="center">
  <a href="https://pypi.org/project/gemini-usage-limits/"><img alt="PyPI" src="https://img.shields.io/pypi/v/gemini-usage-limits?color=blue&label=PyPI"></a>
  <img alt="Python" src="https://img.shields.io/badge/python-3.9%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/gemini-usage/main/docs/demo.gif" alt="gemini-usage demo" width="760">
</p>

---

Antigravity shows your plan limits with its `/usage` command.
**`gemini-usage` gives you plan usage from the shell**, for scripts, hooks, and
AI agents deciding when to back off. It uses the Antigravity login already on
your machine.

The 5-hour and weekly windows cover Gemini models and Claude/GPT models in
separate groups.

**Install `gemini-usage-limits` from PyPI for the `gemini-usage` command.**

```text
$ gemini-usage
Gemini 5-hour: 1% used, resets 21:18 (in 4h44m)
Gemini 7-day: 18% used, resets Tue 19:33 (in 146h58m)
Claude/GPT 5-hour: 0% used
Claude/GPT 7-day: 0% used
```

## ✨ Features

- **⏱️ Usage windows** with 5-hour and weekly percentages and local reset times for each group.
- **🚦 Thresholds for scripts** with `--warn [N]` for any window and `--warn-7d [N]` for the Gemini weekly window. Both exit 1 at or above N%.
- **📊 Extra detail** with model group descriptions and the plan when available through `--all`.
- **📜 Antigravity CLI fallback** through `agy -p /usage` when the direct endpoint path fails.
- **🧾 Plain, JSON, or short** output for terminals and scripts.
- **🌍 Cross-platform** with keyring access on macOS and Linux and CLI fallback on Windows.
- **🔋 Zero dependencies** beyond the Python 3.9+ standard library.
- **🔒 Local & private** with existing login credentials sent only to Google's Antigravity endpoint.

## 🚀 Quickstart

Install Antigravity and run `agy` once to sign in with Google.

```bash
curl -fsSL https://antigravity.google/cli/install.sh | bash
agy
```

After signing in, install and run the usage CLI.

```bash
pip install gemini-usage-limits
gemini-usage
```

## 🧑‍💻 Usage

| Command | What it does |
|---|---|
| `gemini-usage` | Available usage windows and their reset times |
| `gemini-usage --json` | Raw quota-only JSON (alias for `--format json`) |
| `gemini-usage --warn [N]` | Exit 1 when any usage window reaches N% (default 70) |
| `gemini-usage --warn-7d [N]` | Exit 1 at or above N% of the Gemini 7-day window (default 70) |
| `gemini-usage --all` | Add group descriptions and the plan in plain format |
| `gemini-usage --format {plain,json,short}` | Choose output format (default plain) |
| `gemini-usage -v`, `--verbose` | Print credential and usage sources and the plan, when available, to stderr |
| `gemini-usage -V`, `--version` | Print the version |
| `gemini-usage -h`, `--help` | Show help |

Flags combine as in `gemini-usage --format short --warn 70 --warn-7d 90`.
The last of `--json` and `--format` wins. `--all` adds detail in plain format.
Group descriptions come from the quota endpoint and the plan comes from a
separate lookup.

<details>
<summary><b>📊 Example · <code>--all</code></b></summary>

```text
Gemini 5-hour: 1% used, resets 21:18 (in 4h44m)
Gemini 7-day: 18% used, resets Tue 19:33 (in 146h58m)
Claude/GPT 5-hour: 0% used
Claude/GPT 7-day: 0% used
Gemini: Models within this group: Gemini 4 Neon, Gemini 4 Carbon, Gemini 4 Argon, Gemini 4 Hydrogen, Gemini 3.8 Flash
Claude/GPT: Models within this group: Claude Opus 5.5, Claude Sonnet 5.5, GPT-OSS 120B
plan Google AI Pro
```

</details>

## 🤖 Using it from scripts and agents

```bash
gemini-usage --warn 70 || echo "back off"
gemini-usage --warn 70 --warn-7d 90 --format short
gemini-usage --json
```

The usual output is printed even when a threshold trips. Exit code **0** means
success, **1** means a threshold tripped or no source answered, and **2** means
an argument error. Both warning thresholds are inclusive and compare the
source percentage before rounding for display. `--warn` includes all reported
windows, and `--warn-7d` checks the Gemini weekly window when present.

## 📜 Antigravity CLI fallback

The direct request needs a current access token. When it is expired or the
endpoint path fails, the CLI runs `agy -p /usage --print-timeout 60s` once in a
temporary directory. Antigravity refreshes its own credential as needed. The
fallback uses whole percentages from its tab-separated output.

```text
$ gemini-usage
Gemini 5-hour: 1% used, resets 21:18 (in 4h44m)
Gemini 7-day: 18% used, resets Tue 19:33 (in 146h58m)
Claude/GPT 5-hour: 0% used
Claude/GPT 7-day: 0% used
(from the agy CLI)
```

With `--format short`, each group and window fits on one line.

```text
Gemini 5h 1% · Gemini 7d 18% · Claude/GPT 5h 0% · Claude/GPT 7d 0%
```

## 🤔 How it works

Sources are tried in this order.

1. `last-usage.json` if less than **5 minutes** old.
2. One live `POST https://daily-cloudcode-pa.googleapis.com/v1internal:retrieveUserQuotaSummary`, saved on success.
3. One Antigravity `/usage` run.
4. `last-usage.json` if less than **60 minutes** old.

The quota cache lives in `~/.gemini-usage`, or `GEMINI_USAGE_HOME` when set.
Cache writes are atomic. Cached answers at least a minute old show their source
and age, for example `(from the endpoint, 3 min ago)`. If all sources fail, the
distinct reasons appear on stderr with a sign-in hint.

The direct quota request has a 15-second timeout. `-v` and plain `--all` can
make one additional request for the plan using the available Antigravity login.

<details>
<summary><b>🔍 Where credentials live (per OS)</b></summary>

| OS | Credential source |
|---|---|
| macOS | Antigravity's system Keychain entry |
| Linux | Antigravity's Secret Service entry through `secret-tool`, when available |
| Windows | The signed-in Antigravity CLI via the fallback |

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

## 🔒 Privacy & security

- Reads the existing Antigravity login. Antigravity handles token refresh.
- Sends the access token only to **`daily-cloudcode-pa.googleapis.com`** in direct requests.
- Keeps only quota fields in the cache and JSON output. The plan lookup extracts the tier name for display.
- Keeps tokens, request headers, and email addresses out of errors and verbose output.
- **No telemetry or third-party calls from this CLI.**

## ⚠️ Good to know

- The quota endpoint is **undocumented** and its format may change.
- Both sources need a signed-in Antigravity CLI and an eligible account.
- The direct request uses Antigravity's versioned User-Agent to receive the Gemini group.
- An unused window can have no reset time. The CLI omits the reset clause for it.

## 🛠️ Install options

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

Requires Python 3.9+. **The PyPI distribution is `gemini-usage-limits`. The command
and GitHub repo are `gemini-usage`, and the import package is `gemini_usage`.**

## 🤝 Contributing

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

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

Tests use temporary directories and mocked HTTP and subprocesses. They never
contact the endpoint or read 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 uses illustrative quota data with HTTP and credential access
disabled. The README embeds the resulting `docs/demo.gif`.

## 📄 License

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