Skip to main content

Token budget monitoring and kill-switches for autonomous AI agents.

Project description

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

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

token_limiter-1.0.0.tar.gz (10.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

token_limiter-1.0.0-py3-none-any.whl (11.6 kB view details)

Uploaded Python 3

File details

Details for the file token_limiter-1.0.0.tar.gz.

File metadata

  • Download URL: token_limiter-1.0.0.tar.gz
  • Upload date:
  • Size: 10.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.5

File hashes

Hashes for token_limiter-1.0.0.tar.gz
Algorithm Hash digest
SHA256 450c1215102a4ebd14c73a943018ffbd9e819648e0a2144448736f7c6e15b4a6
MD5 4f18859c38d1b563c42b8d928b249cff
BLAKE2b-256 8b03452d5033f7f5e5bf42db0abe346fa0fe437f64b679b01dbb625f2c1d81d1

See more details on using hashes here.

File details

Details for the file token_limiter-1.0.0-py3-none-any.whl.

File metadata

  • Download URL: token_limiter-1.0.0-py3-none-any.whl
  • Upload date:
  • Size: 11.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.5

File hashes

Hashes for token_limiter-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 7fc811a81c519c9b3eca70e4cd651a455f1122a377c2fa2ad7662d034d5b8239
MD5 54ca977ae36e7cc12c4f04f8fe312cd7
BLAKE2b-256 7e644e605103c5527d2b8546ad8c155dc2fb2b81c1eb53b84cfc8c718a08df68

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page