惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

J
Java Code Geeks
月光博客
月光博客
D
DataBreaches.Net
云风的 BLOG
云风的 BLOG
F
Fortinet All Blogs
T
The Blog of Author Tim Ferriss
Stack Overflow Blog
Stack Overflow Blog
Blog — PlanetScale
Blog — PlanetScale
aimingoo的专栏
aimingoo的专栏
U
Unit 42
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
MyScale Blog
MyScale Blog
T
Tailwind CSS Blog
N
Netflix TechBlog - Medium
B
Blog
博客园_首页
G
Google Developers Blog
Recent Announcements
Recent Announcements
博客园 - 【当耐特】
P
Proofpoint News Feed
博客园 - 司徒正美
Hugging Face - Blog
Hugging Face - Blog
MongoDB | Blog
MongoDB | Blog
Last Week in AI
Last Week in AI

DEV Community

Authentication Security Deep Dive: From Brute Force to Salted Hashing (With Java Examples) Why AI Systems Don’t Fail — They Drift Spilling beans for how i learn for exam😁"Reinforcement Learning Cheat Sheet" I Replaced Chrome with Safari for AI Browser Automation. Here's What Broke (and What Finally Worked) How Python Borrows Other People's Work The $40 Architecture: Processing 1 Billion API Requests with 99.99% Uptime Vibe Coding: A Workflow Guide (From Zero to SaaS) Most webhook security guides protect the wrong side. The scary part is delivery. Headless CMS for TanStack Start: Build a Blog with Cosmic EU Age Verification App "Hacked in 2 Minutes" — What Actually Happened Comfy Cloud’s delete function does not actually remove files Running AI Models on GPU Cloud Servers: A Beginner Guide Event-driven media intelligence with AWS Step Functions and Bedrock I scored 500 AI prompts across 8 quality dimensions — here's what broke How to Call Google Gemini API from Next.js (Free Tier, No Backend Needed) The Portal Protocol: Reclaiming Human Connection in the Age of AI How to Fix Your Team's Scattered Knowledge Problem With a Self-Hosted Forum Intro to tc Cloud Functors: A Graph-First Mental Model for the Modern Cloud Designing Multi-Tenant Backends With Both Ownership and Team Access I Built a Neumorphic CSS Library with 77+ Components — Here's What I Learned PostgreSQL Performance Optimization: Why Connection Pooling Is Critical at Scale Cómo construí un SaaS multi-rubro para gestionar expensas en Argentina con FastAPI + Vue 3 🚀 I Built an Ethical Hacking Scanner Tool – Open Source Project I Replaced /usage and /context in Claude Code With a Single Statusline A Pythonic Way to Handle Emails (IMAP/SMTP) with Auto-Discovery and AI-Ready Design I Collected 8.9 Million Polymarket Price Points — Here's What I Found About How Markets Really Move EcoTrack AI — Carbon Footprint Tracker & Dashboard Everyone's Using AI. No One Agrees How. 5 self-hosted ebook managers worth trying in 2026 Building Your First AI Agent with LangChain: From Chatbot to Autonomous Assistant
Per-Key Rate Limiting for Agent Tool Calls: Stop One User...
Mukunda Rao · 2026-05-26 · via DEV Community

Multi-tenant agents share infrastructure. When one user's agent calls the web search tool 200 times in a minute, every other user's agent slows down or gets errors.

Global rate limits protect the provider. Per-key rate limits protect your users from each other. agent-rate-fence is a sliding-window rate limiter where the key is whatever you want: user ID, session ID, tool name, or any combination.


The Shape of the Fix

from agent_rate_fence import RateFence, RateLimitExceeded

fence = RateFence(
    max_calls=10,
    window_seconds=60.0,
)

def rate_limited_search(user_id: str, query: str) -> list:
    try:
        with fence.allow(key=user_id):
            return web_search(query)
    except RateLimitExceeded as e:
        return {"error": f"Rate limit exceeded. Retry after {e.retry_after_seconds:.0f}s"}

Enter fullscreen mode Exit fullscreen mode

Ten calls per user per 60-second window. The eleventh call raises RateLimitExceeded with a retry_after_seconds hint.


What It Does NOT Do

agent-rate-fence does not rate limit across processes or machines. It is in-process only. For distributed rate limiting across a fleet of workers, you need Redis or a similar shared store.

It does not differentiate between tool types. All calls through the same fence share the same counter for the same key. If you want different limits for different tools, create a fence per tool.

It does not queue requests that exceed the limit. Excess requests fail immediately. For a queueing approach, you need a task queue.


Inside the Library

Sliding window implementation using a deque of timestamps:

from collections import deque
import time
import threading

class RateFence:
    def __init__(self, max_calls: int, window_seconds: float):
        self._max = max_calls
        self._window = window_seconds
        self._calls: dict[str, deque] = {}
        self._lock = threading.Lock()

    @contextmanager
    def allow(self, key: str):
        with self._lock:
            now = time.monotonic()
            if key not in self._calls:
                self._calls[key] = deque()

            # Remove expired entries
            dq = self._calls[key]
            while dq and dq[0] < now - self._window:
                dq.popleft()

            if len(dq) >= self._max:
                oldest = dq[0]
                retry_after = self._window - (now - oldest)
                raise RateLimitExceeded(retry_after_seconds=retry_after)

            dq.append(now)

        yield

Enter fullscreen mode Exit fullscreen mode

The sliding window is more accurate than a fixed window (which can allow 2x the limit at window boundaries). A deque per key is efficient: popleft() is O(1), and the deque only holds timestamps within the current window.

Thread safety: the entire check-and-append is under one lock. This prevents the TOCTOU race where two concurrent callers both see len(dq) < max and both proceed.

Cleanup: keys that have been idle for more than window_seconds can accumulate in self._calls. A background cleanup thread is optional. By default, the deque for an idle key stays in memory but empty, which is cheap.


When to Use It

Use it for multi-tenant agents where users share tool infrastructure. Web search, database queries, external API calls — any tool where one user consuming excessive capacity affects others.

Use it by tool category, not just by user. A user making 10 web searches per minute might be fine. A user making 10 DELETE operations per minute might not be. Create a fence per tool category with different limits.

Use it for cost attribution and control. Pairing per-user rate limiting with per-user cost tracking gives you a complete picture of resource consumption per tenant.

Skip it for single-user agents or closed environments where all agent instances belong to the same user. The overhead is not worth it.


Install

pip install git+https://github.com/MukundaKatta/agent-rate-fence

Enter fullscreen mode Exit fullscreen mode

from agent_rate_fence import RateFence, RateLimitExceeded

# Different limits for different tool categories
search_fence = RateFence(max_calls=20, window_seconds=60.0)
write_fence = RateFence(max_calls=5, window_seconds=60.0)
delete_fence = RateFence(max_calls=2, window_seconds=300.0)

FENCES = {
    "search_web": search_fence,
    "create_record": write_fence,
    "update_record": write_fence,
    "delete_record": delete_fence,
}

def execute_tool(tool_name: str, user_id: str, args: dict):
    fence = FENCES.get(tool_name)
    if fence:
        try:
            with fence.allow(key=user_id):
                return call_tool(tool_name, args)
        except RateLimitExceeded as e:
            return {
                "error": "rate_limit_exceeded",
                "retry_after": e.retry_after_seconds,
            }
    return call_tool(tool_name, args)

Enter fullscreen mode Exit fullscreen mode


Sibling Libraries

Library What it solves
llm-rate-limit-bucket Token-bucket rate limiter for LLM API calls
token-budget-pool Shared USD/token budget across concurrent agents
llm-cost-cap Per-call cost gate
llm-batch-coalesce Collapse duplicate calls from concurrent callers
agent-deadline Time-bound agent execution

The multi-tenant stack: agent-rate-fence per user per tool, token-budget-pool for shared fleet budget, llm-cost-cap for individual call bounds.


What's Next

Redis backend for distributed rate limiting is the most requested missing feature. The interface would stay the same — RateFence(max_calls=10, window=60, backend=RedisBackend(url="redis://...")) — but the deque would live in Redis with atomic Lua scripts for check-and-append.

Burst allowance: RateFence(max_calls=10, window=60, burst=20) would allow up to 20 calls in a short burst before rate limiting kicks in, while still enforcing the 10-per-60s average. This matches how most API rate limits actually work.

Per-key limit overrides: some users legitimately need higher limits (enterprise tiers). A fence.set_limit(key="enterprise-user-1", max_calls=100) would allow per-key overrides without a separate fence.


Built as part of the agent-stack family: composable Python primitives for production LLM agents.