token-limiter
Token budget monitoring and kill-switches for autonomous AI agents. Zero dependencies.
pip install token-limiter
Quick start
from token_limiter import token_limiter, from_openai
budget = token_limiter(max_tokens=500_000, max_cost=5.00)
response = openai.chat.completions.create(model="gpt-4o", messages=messages)
budget.record(from_openai(response))
if not budget.ok:
print(budget.reason)
from_openai() maps the response to { input, output, reasoning }. record() tracks it, checks the ceiling, runs the circuit breaker. ok tells you if the agent should continue.
Adapters
One adapter per provider. Each returns { input, output, reasoning }:
from token_limiter import from_openai, from_gemini, from_anthropic, from_ollama, from_raw
budget.record(from_openai(response)) # OpenAI, Groq, Together, Fireworks, LM Studio
budget.record(from_gemini(response)) # Google AI Studio, Vertex
budget.record(from_anthropic(response)) # Claude
budget.record(from_ollama(response)) # Ollama, any local model
budget.record(from_raw(3200, 800, reasoning=5400)) # raw numbers
Works with any provider that returns token counts. If yours isn't listed, use from_raw().
What token_limiter() gives you
budget.ok # should the agent continue?
budget.reason # why it stopped, or None
budget.usage # CumulativeUsage(input, output, reasoning, total)
budget.cost # CostEstimate(input, output, reasoning, total) in USD
budget.turns # number of turns recorded
budget.history # full turn history
budget.analyze() # run anomaly detection
Config
budget = token_limiter(
max_tokens=500_000, # token ceiling
max_cost=5.00, # dollar ceiling
max_duplicate_calls=3, # identical tool calls before kill
reasoning_pct_threshold=80, # reasoning % to flag
input_rate=0.00125, # $/1K input tokens
output_rate=0.01, # $/1K output tokens
reasoning_rate=0.0125, # $/1K reasoning tokens
)
Events
budget.on("warning", lambda e: print(e["message"])) # 50%, 75%, 90% thresholds
budget.on("tripped", lambda e: print(e["violations"])) # circuit breaker fired
Individual modules
If you need more control, the internals are exported too:
from token_limiter import TokenTracker, BudgetMonitor, KillSwitch, AnomalyDetector
Demo
python examples/demo.py
Simulates 20 turns across healthy → degrading → rogue phases. Kill-switch trips when cost ceiling breaches.
Related
License
MIT © Hemanth.HM
Metadata
Release files for token-limiter 1.0.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| token_limiter-1.0.0.tar.gz | 10.5 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| token_limiter-1.0.0-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 22.1 kB
Release files / token_limiter-1.0.0.tar.gz
| Download URL | token_limiter-1.0.0.tar.gz |
|---|---|
| Size | 10.5 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
450c1215102a4ebd14c73a943018ffbd9e819648e0a2144448736f7c6e15b4a6
|
|
BLAKE2b-256 checksum How to use checksums |
8b03452d5033f7f5e5bf42db0abe346fa0fe437f64b679b01dbb625f2c1d81d1
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.14.5
|
Release files / token_limiter-1.0.0-py3-none-any.whl
| Download URL | token_limiter-1.0.0-py3-none-any.whl |
|---|---|
| Size | 11.6 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
7fc811a81c519c9b3eca70e4cd651a455f1122a377c2fa2ad7662d034d5b8239
|
|
BLAKE2b-256 checksum How to use checksums |
7e644e605103c5527d2b8546ad8c155dc2fb2b81c1eb53b84cfc8c718a08df68
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.14.5
|