Skip to main content

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

from fastapi import Depends, FastAPI, Request
from rateguard import RateGuard

app = FastAPI()
rg = RateGuard(preset="standard")
app.add_middleware(rg.asgi_middleware)


@app.post("/chat")
async def chat(req: Request, _=Depends(rg.require)):
    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 / llm-heavy / strict-upstream-protection dev
hard_stop bool True
monthly_limit int preset-derived
soft_stop_at float 0.8

Docs

  • Go SDK: packages/sdk-go
  • Node SDK: packages/sdk-node

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

varbees_rateguard-0.1.0.tar.gz (22.2 kB view details)

Uploaded Source

Built Distribution

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

varbees_rateguard-0.1.0-py3-none-any.whl (28.8 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: varbees_rateguard-0.1.0.tar.gz
  • Upload date:
  • Size: 22.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.4

File hashes

Hashes for varbees_rateguard-0.1.0.tar.gz
Algorithm Hash digest
SHA256 2e1bf57c54e56aa3d2e47668831426c8bc4ea7cb542993fd06c538bf9127ed2a
MD5 e4592a8a1fb1fe999c7e96d4289e5cca
BLAKE2b-256 582df767fd04f5160db6e0b780ccb6b659e7546badb718a2ecaa8583aed4ad05

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for varbees_rateguard-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 b197ff3b634b935d67d4363357d98d8d7de71e001cc71203f158c1583db13783
MD5 d55c5bc315f3bc417e588b62f47605a4
BLAKE2b-256 23cbc0a7ba9969c1a5609fc3a05c31eafbc27746c9947e8e6200c1e0878a8370

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