Skip to main content

tokengate

Python SDK for TokenGate — quota management as a service.

pip install tokengate

Requires Python 3.11+. Sync and async clients, automatic idempotency keys, a reserve/commit metering context manager, and typed exceptions for every denial. The full integration contract lives at your deployment's /llms.txt (and is served over MCP at /mcp/).

from tokengate import TokenGate

client = TokenGate("https://tokengate.example.com", api_key="tg_...")

# Atomic check-and-consume (idempotency key auto-generated)
result = client.consume("user_42", {"llm.input_tokens": 1200, "llm.output_tokens": 350})
print(result.allowed, [(r.resource, r.remaining) for r in result.results])

# LLM-style metering: reserve an estimate, commit actual usage
with client.meter("user_42", {"llm.tokens": 4000}) as m:
    completion = run_llm(...)
    m.record("llm.tokens", completion.usage.total_tokens)
# exit: commit(actuals) on success, release() on exception

# Denials raise typed errors carrying retry hints
from tokengate import QuotaExceeded, RateLimited

try:
    client.consume("user_42", {"llm.tokens": 10_000_000})
except QuotaExceeded as exc:
    print(exc.retry_after, exc.reset_at, exc.blocking_policy)

AsyncTokenGate mirrors the same surface with async/await and async with client.meter(...).

Denials are exceptions, never return values. consume() refusals arrive as RFC 7807 429s and raise QuotaExceeded/RateLimited; a success status therefore always means permission. The client enforces that invariant rather than assuming it: if a 2xx body ever carried allowed: false, consume() raises ProtocolViolation instead of returning — a denial must never be readable as a grant. The advisory check() is the exception by design: it always answers 200 and you inspect response.allowed yourself.

Download files

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

Source Distribution

tokengate-0.1.0.tar.gz (13.5 kB view details)

Uploaded Source

Built Distribution

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

tokengate-0.1.0-py3-none-any.whl (15.5 kB view details)

Uploaded Python 3

File details

Details for the file tokengate-0.1.0.tar.gz.

File metadata

  • Download URL: tokengate-0.1.0.tar.gz
  • Upload date:
  • Size: 13.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/7.0.0 CPython/3.14.6

File hashes

Hashes for tokengate-0.1.0.tar.gz
Algorithm Hash digest
SHA256 73f433edbde377bca25c46f253d9df39f5478d52ee48c9267c0c14623ddc3aa8
MD5 52c69e9e2181f524015b28827f047865
BLAKE2b-256 f35c77834f2e8dd453d4a5659cbbb55081f19ba7fda58aa5317000d917c75e9d

See more details on using hashes here.

File details

Details for the file tokengate-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: tokengate-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 15.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/7.0.0 CPython/3.14.6

File hashes

Hashes for tokengate-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 629f906b91e9ceccd2020d56f087e6e6193180cf64731e21b36be0af46dc4dd4
MD5 1df276091364a0599907d84d61a15749
BLAKE2b-256 df78a8e79916faa675b6499b3c4b638a81a754b9c793c4aec982b9ea92647f2b

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 Sentry Error logging StatusPage Status page