0Pricing
AI Agents with LangChain & Autonomous Workflows · บทเรียน

การจำกัดอัตราและการจัดการโควตาเอพีไอ

ปกป้องเอเจนต์ในระบบจริงจากข้อจำกัดอัตราของผู้ให้บริการและค่าใช้จ่ายที่พุ่งสูง ด้วยการจำกัดความถี่คำขอ การลองใหม่โดยเว้นระยะเพิ่มขึ้น และการจัดการโควตารายผู้ใช้

การจำกัดอัตราและการจัดการโควตาเอพีไอ เป็นบทเรียน AI Agents with LangChain & Autonomous Workflows ฟรีบน CoddyKit นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 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 InMemoryRateLimiter to 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.

คำถามที่พบบ่อย

บทเรียน “การจำกัดอัตราและการจัดการโควตาเอพีไอ” ฟรีหรือไม่

ใช่ — ข้อความเต็มของ “การจำกัดอัตราและการจัดการโควตาเอพีไอ” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส AI Agents with LangChain & Autonomous Workflows ให้อัปเกรดเป็น CoddyKit PRO คอร์ส AI Agents with LangChain & Autonomous Workflows มีบทเรียนทั้งหมด 4 บทเรียน

คุณจะเรียนรู้อะไรในบทเรียน “การจำกัดอัตราและการจัดการโควตาเอพีไอ”

ปกป้องเอเจนต์ในระบบจริงจากข้อจำกัดอัตราของผู้ให้บริการและค่าใช้จ่ายที่พุ่งสูง ด้วยการจำกัดความถี่คำขอ การลองใหม่โดยเว้นระยะเพิ่มขึ้น และการจัดการโควตารายผู้ใช้ คุณปฏิบัติ AI Agents with LangChain & Autonomous Workflows ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน

คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน AI Agents with LangChain & Autonomous Workflows หรือไม่

ไม่จำเป็นต้องมีประสบการณ์มาก่อน AI Agents with LangChain & Autonomous Workflows บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน

บทเรียน “การจำกัดอัตราและการจัดการโควตาเอพีไอ” ใช้เวลานานแค่ไหน

บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย

ฉันเขียนและรันโค้ดในบทเรียน AI Agents with LangChain & Autonomous Workflows นี้ได้ไหม

ได้ บทเรียน AI Agents with LangChain & Autonomous Workflows ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ

บทเรียนทั้งหมดในหลักสูตรนี้

  1. การนำเอเจนต์ไปใช้งานบนแพลตฟอร์มคลาวด์
  2. การจัดการสถานะและเซสชันของเอเจนต์
  3. การปรับขนาดสถาปัตยกรรมเอเจนต์
  4. การจำกัดอัตราและการจัดการโควตาเอพีไอ
← กลับไปที่ AI Agents with LangChain & Autonomous Workflows