Skip to main content

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. cryptography is a lazy, optional import — install the attestation extra to use it.
  • Redis distributed limiter (pip install varbees-rateguard[redis]) — atomic Lua GCRA script for rate limits shared across processes/instances, RedisPyClient/AsyncRedisPyClient wrap redis-py or bring your own client shaped like RedisLimiterClient.
  • 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/ConsoleEventEmitter for 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.

Metadata

Release files for varbees-rateguard 0.5.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for varbees-rateguard 0.5.1
File Size Uploaded Provenance
varbees_rateguard-0.5.1.tar.gz 118.6 kB Without provenance Details

Built distribution (wheel)

Table of built distributions (wheels) for varbees-rateguard 0.5.1
File Interpreter ABI Platform
varbees_rateguard-0.5.1-py3-none-any.whl Python 3 none any Details

Total release size: 256.7 kB

Release files / varbees_rateguard-0.5.1.tar.gz

Download URL varbees_rateguard-0.5.1.tar.gz
Size 118.6 kB
Tags Source
SHA-256 checksum
How to use checksums
2c46de1ba7d73bfd8c290662bd0eb9e0d6035e9a7daa0dd4018c8a70ddc46b00
BLAKE2b-256 checksum
How to use checksums
149633f9a45f46c487f39cf45875d24f5828276162f2b0948ec0f401d21fce16
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.14.4

Release files / varbees_rateguard-0.5.1-py3-none-any.whl

Download URL varbees_rateguard-0.5.1-py3-none-any.whl
Size 138.2 kB
Tags Python 3
SHA-256 checksum
How to use checksums
3ee91566ee893b73211aa2fd9d231d6556b2322f77491ebfaacaba26ca8747d1
BLAKE2b-256 checksum
How to use checksums
629591d599915a277fe99bebf668d59de4f04ec671278538b299cd70b285c85f
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.14.4

Security for varbees-rateguard 0.5.1

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page