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.
Metadata
Release files for tokengate 0.1.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 | |
|---|---|---|---|
| tokengate-0.1.0.tar.gz | 13.5 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| tokengate-0.1.0-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 29.1 kB
Release files / tokengate-0.1.0.tar.gz
| Download URL | tokengate-0.1.0.tar.gz |
|---|---|
| Size | 13.5 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
73f433edbde377bca25c46f253d9df39f5478d52ee48c9267c0c14623ddc3aa8
|
|
BLAKE2b-256 checksum How to use checksums |
f35c77834f2e8dd453d4a5659cbbb55081f19ba7fda58aa5317000d917c75e9d
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.14.6
|
Release files / tokengate-0.1.0-py3-none-any.whl
| Download URL | tokengate-0.1.0-py3-none-any.whl |
|---|---|
| Size | 15.5 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
629f906b91e9ceccd2020d56f087e6e6193180cf64731e21b36be0af46dc4dd4
|
|
BLAKE2b-256 checksum How to use checksums |
df78a8e79916faa675b6499b3c4b638a81a754b9c793c4aec982b9ea92647f2b
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.14.6
|