Limitazione della frequenza e gestione delle quote API
Proteggete gli agenti in produzione dai limiti di frequenza dei provider e dai costi incontrollati limitando le richieste, ritentando con backoff e gestendo le quote per utente.
Limitazione della frequenza e gestione delle quote API è una lezione AI Agents with LangChain & Autonomous Workflows gratuita su CoddyKit. Questa è la lezione 4 di 4. Puoi leggere la lezione completa qui gratuitamente — poi esercitati direttamente nel browser con un editor di codice integrato e un tutor IA disponibile 24/7. Fa parte del percorso di apprendimento AI Agents with LangChain & Autonomous Workflows, e i tuoi progressi si sincronizzano tra il web e l'app CoddyKit. Il corso AI Agents with LangChain & Autonomous Workflows include 4 lezioni in totale.
Parti di questa lezione non sono ancora state tradotte e vengono mostrate in inglese.
The Limits You Face
LLM providers cap usage in two ways:
- Requests per minute (RPM)
- Tokens per minute (TPM)
Exceed them and calls return 429 Too Many Requests, breaking your agents under load.
Why Throttle Proactively
Waiting for 429s and retrying is wasteful. Proactive rate limiting spaces out requests so you stay under the cap, smoothing traffic and avoiding errors entirely.
Client-Side Rate Limiter
LangChain can throttle model calls with a built-in rate limiter that releases a fixed number of requests per second.
from langchain_core.rate_limiters import InMemoryRateLimiter
limiter = InMemoryRateLimiter(
requests_per_second=2,
max_bucket_size=5
)Attaching It to the Model
Pass the limiter to the chat model. Every call now waits its turn automatically.
llm = ChatOpenAI(
model='gpt-4o-mini',
rate_limiter=limiter
)Retry with Exponential Backoff
Some 429s and transient errors are unavoidable. Retry with growing delays so you do not hammer the provider.
llm_with_retry = llm.with_retry(
stop_after_attempt=5
)Respecting Retry-After
Providers often return a Retry-After header telling you how long to wait. Honoring it is more polite and effective than a fixed delay.
wait = int(response.headers.get('Retry-After', '1'))Per-User Quotas
Beyond provider limits, you set your own per-user quotas to control cost and fairness. Track usage in a store like Redis and reject or queue once a user exceeds their allowance.
used = redis.incr(f'quota:{user_id}')
if used > DAILY_LIMIT:
raise QuotaExceeded()Token Bucket Algorithm
The common pattern is a token bucket: tokens refill at a steady rate, each request consumes one, and an empty bucket means wait. It allows short bursts while enforcing an average rate.
Queuing Under Load
When demand spikes past your limits, queue requests instead of dropping them. A background worker drains the queue at a safe rate, keeping the system stable.
Spreading Across Keys
For high throughput you can rotate across multiple API keys or providers, distributing load so no single key hits its cap. Track each key's usage independently.
Monitoring Limits
Track 429 rates and how close you run to caps. Rising 429s signal you need a higher tier, better throttling, or more keys before users notice failures.
Quick Check
Test your rate-limiting knowledge.
Recap
You learned to manage limits and quotas in production:
- Providers cap RPM and TPM; 429s break agents
- Use an
InMemoryRateLimiterto throttle proactively - Add retry with backoff and honor
Retry-After - Enforce per-user quotas with a token bucket
- Queue, rotate keys, and monitor 429 rates
Good limit management keeps scaled agents reliable and affordable.
Impara AI Agents with LangChain & Autonomous Workflows con un tutor IA — gratis
Scrivi ed esegui vero codice nel tuo browser, ricevi aiuto istantaneo da un tutor IA disponibile 24/7, e riprendi da dove hai lasciato sul web o nell'app.
- Corsi
- 12
- Lezioni
- 50
Domande Frequenti
La lezione «Limitazione della frequenza e gestione delle quote API» è gratuita?
Sì — il testo completo di «Limitazione della frequenza e gestione delle quote API» è gratuito qui sul web. Per esercitarvi in modo interattivo (un editor di codice integrato e un tutor IA 24/7) e sbloccare il resto del corso AI Agents with LangChain & Autonomous Workflows, passa a CoddyKit PRO. Il corso AI Agents with LangChain & Autonomous Workflows include 4 lezioni in totale.
Cosa imparerò in «Limitazione della frequenza e gestione delle quote API»?
Proteggete gli agenti in produzione dai limiti di frequenza dei provider e dai costi incontrollati limitando le richieste, ritentando con backoff e gestendo le quote per utente. Eserciti AI Agents with LangChain & Autonomous Workflows con codice pratico che esegui direttamente nel browser, e un tutor IA 24/7 risponde alle tue domande mentre lavori sulla lezione.
Ho bisogno di esperienza per iniziare AI Agents with LangChain & Autonomous Workflows?
Non è richiesta alcuna esperienza precedente. AI Agents with LangChain & Autonomous Workflows su CoddyKit è strutturato per principianti e studenti avanzati, quindi puoi iniziare da qui o dall'inizio e procedere al tuo ritmo. Questa è la lezione 4 di 4.
Quanto tempo richiede la lezione «Limitazione della frequenza e gestione delle quote API»?
La maggior parte delle lezioni CoddyKit richiede circa 5–10 minuti. Ogni lezione è breve e interattiva, quindi fai progressi costanti e riprendi esattamente da dove hai lasciato su web e app.
Posso scrivere ed eseguire codice in questa lezione AI Agents with LangChain & Autonomous Workflows?
Sì. Ogni lezione AI Agents with LangChain & Autonomous Workflows include un editor di codice integrato, quindi scrivi ed esegui codice reale direttamente nel tuo browser e ricevi feedback istantaneo dall'IA — nessuna configurazione locale necessaria.
Tutte le lezioni di questo corso
- Distribuire gli agenti su piattaforme cloud
- Gestire lo stato e le sessioni degli agenti
- Scalare le architetture degli agenti
- Limitazione della frequenza e gestione delle quote API