File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ """Rate-limit dependency for the cost-incurring run endpoint (per user).
2+
3+ The per-IP auth limiter lives in ``auth.py`` instead — putting it here
4+ would form an import cycle (this module needs ``authenticate_user`` from
5+ ``auth``). Both share the process-local ``limiter`` and raise
6+ ``TooManyRequests`` (429 + Retry-After) when a window is exceeded.
7+ """
8+
9+ from __future__ import annotations
10+
11+ from fastapi import Depends
12+
13+ from ..controllers .errors import TooManyRequests
14+ from ..infra .config import get_settings
15+ from ..infra .ratelimit import limiter
16+ from ..models import User
17+ from .auth import authenticate_user
18+
19+
20+ def limit_runs (user : User = Depends (authenticate_user )) -> None :
21+ """Per-user limit on run submissions (each spends LLM tokens)."""
22+ s = get_settings ()
23+ allowed , retry_after = limiter .check (
24+ f"runs:{ user .id } " , s .rate_limit_runs , s .rate_limit_window_s
25+ )
26+ if not allowed :
27+ secs = int (retry_after ) + 1
28+ raise TooManyRequests (f"rate limit exceeded; retry in { secs } s" , retry_after = secs )
You can’t perform that action at this time.
0 commit comments