速率限制与 API 配额管理
通过限制请求速率、采用退避策略重试以及管理每位用户的配额,保护生产环境中的代理免受服务提供商速率限制和成本失控的影响。
速率限制与 API 配额管理 是 CoddyKit 上的免费 AI Agents with LangChain & Autonomous Workflows 课时。 这是第 4 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 AI Agents with LangChain & Autonomous Workflows 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 AI Agents with LangChain & Autonomous Workflows 课程共包含 4 节课。
本课时的部分内容尚未翻译,以英文显示。
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.
常见问题解答
「速率限制与 API 配额管理」课时是免费的吗?
是的 — 「速率限制与 API 配额管理」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 AI Agents with LangChain & Autonomous Workflows 课程的其余内容,请升级到 CoddyKit PRO。 AI Agents with LangChain & Autonomous Workflows 课程共包含 4 节课。
「速率限制与 API 配额管理」这节课中我会学到什么?
通过限制请求速率、采用退避策略重试以及管理每位用户的配额,保护生产环境中的代理免受服务提供商速率限制和成本失控的影响。 你通过在浏览器中直接运行的动手代码来练习 AI Agents with LangChain & Autonomous Workflows,全天候 AI 导师会在你学习这节课的过程中回答你的问题。
学习 AI Agents with LangChain & Autonomous Workflows 需要有经验吗?
无需任何先前经验。CoddyKit 上的 AI Agents with LangChain & Autonomous Workflows 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 4 节课,共 4 节。
「速率限制与 API 配额管理」课时需要多长时间?
大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。
我能在这节 AI Agents with LangChain & Autonomous Workflows 课中编写并运行代码吗?
能。每节 AI Agents with LangChain & Autonomous Workflows 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。
此课程中的所有课时
- 将智能体部署到云平台
- 管理智能体状态与会话
- 扩展智能体架构
- 速率限制与 API 配额管理