Python middleware SDK for RateGuard
Project description
RateGuard Python SDK
RateGuard is a middleware-first API protection and observability SDK for Python apps.
It runs entirely in-process by default:
- rate limiting
- token budgets
- circuit breaking
- request events
No external service is required for standalone use. Configure an event endpoint only when you want RateGuard events delivered outside the process.
Install
pip install varbees-rateguard
Quick Start
from rateguard import BudgetExceeded, RateGuard
rg = RateGuard(preset="standard")
budget = rg.budget
async def call_provider(user_id: str) -> None:
try:
async with budget.enforce(user_id=user_id, hard_stop=True):
# Call your LLM, API, worker, or provider here.
pass
budget.record(user_id=user_id, tokens=1200)
except BudgetExceeded as exc:
print(exc)
raise
For provider SDKs that return token usage, enforce before the request and record the returned usage afterward:
async with rg.budget.enforce(user_id="me", hard_stop=True):
response = await client.chat.completions.create(model="gpt-4o", messages=messages)
rg.budget.record(user_id="me", tokens=response.usage.total_tokens)
FastAPI
Two ways to wire RateGuard in — pick one per route, not both, or a single request gets admitted (and its rate limit/token budget consumed) twice.
Per-route, via Depends — needs the fastapi extra (pip install varbees-rateguard[fastapi]):
from fastapi import Depends, FastAPI
from rateguard import RateGuard
app = FastAPI()
rg = RateGuard(preset="standard")
@app.post("/chat")
async def chat(_=Depends(rg.require)):
return {"ok": True}
App-wide, via ASGI middleware:
from fastapi import FastAPI
from rateguard import RateGuard
app = FastAPI()
rg = RateGuard(preset="standard")
app.add_middleware(rg.asgi_middleware)
@app.post("/chat")
async def chat():
return {"ok": True}
Flask / WSGI
from flask import Flask
from rateguard import RateGuard, RateGuardMiddleware
app = Flask(__name__)
rg = RateGuard(preset="standard")
app.wsgi_app = RateGuardMiddleware(app.wsgi_app, guard=rg.runtime)
Configuration
| Key | Type | Default |
|---|---|---|
preset |
dev / standard / high-throughput / streaming-llm / agent-orchestrator / llm-heavy / mcp-server / strict-upstream-protection |
dev |
hard_stop |
bool |
True |
monthly_limit |
int |
preset-derived |
soft_stop_at |
float |
0.8 |
Outbound LLM Tracking
Needs the outbound extra (pip install varbees-rateguard[outbound]) — httpx is a lazy, optional import.
from openai import OpenAI, AsyncOpenAI
from rateguard import RateGuard
rg = RateGuard(preset="streaming-llm")
client = OpenAI(http_client=rg.wrap_httpx_client())
aclient = AsyncOpenAI(http_client=rg.wrap_httpx_async_client())
rg.mcp_tools() and rg.mcp_call() expose all 7 pre-flight MCP tools for agent frameworks (includes attest_budget/verify_budget). serve_mcp(rg) runs a zero-dependency stdio JSON-RPC server over the same tools — drop it straight into a Claude Code/Cursor/Claude Desktop MCP config. Guardrails, loop detection, GenAI attribute helpers (including TTFT/TPOT), and Prometheus exposition helpers are exported for app-level wiring.
Also included
- Budget attestation (
pip install varbees-rateguard[attestation]) — Ed25519-signed delegation chains (new_root_budget_token,attest,verify_presentation), byte-identical signing payload with Go and Node so a token attested in one language verifies in another.cryptographyis a lazy, optional import — install theattestationextra to use it. - Redis distributed limiter (
pip install varbees-rateguard[redis]) — atomic Lua GCRA script for rate limits shared across processes/instances,RedisPyClient/AsyncRedisPyClientwrapredis-pyor bring your own client shaped likeRedisLimiterClient. - Admin API — opt-in, unauthenticated-by-design ASGI app (
AdminApp) exposing state/policy/MCP-tool-calls; bind privately. - Adaptive rate limiting — opt-in AIMD controller that auto-tunes the effective limit from observed upstream error rate.
- Semantic response caching — bring your own
Embedder; a cosine-similarity hit skips the network call, breaker, and budget entirely. - Lock-free sharded limiter — 64-way sharding with atomic CAS, available as
ShardedLimiter. - Events/webhooks —
HTTPEventEmitter/WebSocketEventEmitter/ConsoleEventEmitterfor shipping admission decisions out of process.
Docs
- Go SDK:
packages/sdk-go - Node SDK:
packages/sdk-node - Full feature parity as of v0.2.0 — see the root README for the complete capability table.
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
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file varbees_rateguard-0.5.1.tar.gz.
File metadata
- Download URL: varbees_rateguard-0.5.1.tar.gz
- Upload date:
- Size: 118.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.14.4
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
2c46de1ba7d73bfd8c290662bd0eb9e0d6035e9a7daa0dd4018c8a70ddc46b00
|
|
| MD5 |
e1990825163f07a9e3fd151dbb35b024
|
|
| BLAKE2b-256 |
149633f9a45f46c487f39cf45875d24f5828276162f2b0948ec0f401d21fce16
|
File details
Details for the file varbees_rateguard-0.5.1-py3-none-any.whl.
File metadata
- Download URL: varbees_rateguard-0.5.1-py3-none-any.whl
- Upload date:
- Size: 138.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.14.4
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
3ee91566ee893b73211aa2fd9d231d6556b2322f77491ebfaacaba26ca8747d1
|
|
| MD5 |
4f0750b5a95d9a114b67aa7813998f56
|
|
| BLAKE2b-256 |
629591d599915a277fe99bebf668d59de4f04ec671278538b299cd70b285c85f
|