تحديد معدل الطلبات وإدارة حصص API
احمِ وكلاء الإنتاج من حدود معدل الطلبات لدى مزوّدي الخدمة ومن التكاليف المتصاعدة بلا ضابط، عبر خنق الطلبات وإعادة المحاولة مع التراجع التدريجي وإدارة حصص كل مستخدم.
تحديد معدل الطلبات وإدارة حصص API درس مجاني في AI Agents with LangChain & Autonomous Workflows على CoddyKit. هذا هو الدرس 4 من أصل 4. يمكنك قراءة الدرس كاملاً أدناه مجاناً — ثم تمرن عليه مباشرة في المتصفح باستخدام محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7. هذا الدرس جزء من مسار التعلم في 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» كامل متاح مجاناً هنا على الويب. لتمرينه بشكل تفاعلي (محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7) وفتح باقي دورة AI Agents with LangChain & Autonomous Workflows، انتقل إلى CoddyKit PRO. تتضمن دورة AI Agents with LangChain & Autonomous Workflows 4 دروس في المجموع.
ماذا ستتعلم في «تحديد معدل الطلبات وإدارة حصص API»؟
احمِ وكلاء الإنتاج من حدود معدل الطلبات لدى مزوّدي الخدمة ومن التكاليف المتصاعدة بلا ضابط، عبر خنق الطلبات وإعادة المحاولة مع التراجع التدريجي وإدارة حصص كل مستخدم. تتمرن على AI Agents with LangChain & Autonomous Workflows مع أكواد عملية تشغلها مباشرة في المتصفح، ومدرس ذكاء اصطناعي متاح 24/7 يجيب على أسئلتك أثناء عملك.
هل أحتاج إلى خبرة سابقة لأبدأ AI Agents with LangChain & Autonomous Workflows؟
لا تُشترط خبرة سابقة. AI Agents with LangChain & Autonomous Workflows على CoddyKit منظم للمبتدئين حتى المتقدمين، لذا يمكنك البدء من هنا أو من البداية والتقدم بسرعتك الخاصة. هذا هو الدرس 4 من أصل 4.
كم من الوقت يستغرق درس «تحديد معدل الطلبات وإدارة حصص API»؟
معظم دروس CoddyKit تستغرق حوالي 5–10 دقائق. كل منها موجز وتفاعلي، لذا تحرز تقدماً مستمراً وتستأنف من حيث توقفت عبر الويب والتطبيق.
هل يمكنني كتابة وتشغيل أكواد في درس AI Agents with LangChain & Autonomous Workflows هذا؟
نعم. كل درس في AI Agents with LangChain & Autonomous Workflows يتضمن محرر أكواد مدمج، لذا تكتب وتشغل أكواداً حقيقية مباشرة في متصفحك وتحصل على تعليقات فورية من الذكاء الاصطناعي — بدون إعداد محلي.
جميع الدروس في هذه الدورة
- نشر الوكلاء على المنصات السحابية
- إدارة حالة الوكيل والجلسات
- توسيع نطاق بنيات الوكلاء
- تحديد معدل الطلبات وإدارة حصص API