AI Agents with LangChain & Autonomous Workflows · レッスン

レート制限とAPIクォータの管理

リクエストのスロットリング、バックオフ付きの再試行、ユーザーごとのクォータ管理によって、プロバイダーのレート制限や予想外のコスト増から本番エージェントを守ります。

レッスン 4/413 ステップ

「レート制限とAPIクォータの管理」はCoddyKit上の無料AI Agents with LangChain & Autonomous Workflowsレッスンです。 これはレッスン4/4です。 下記で完全なレッスンを無料で読むことができます。その後、ブラウザ内の組み込みコードエディタと24時間対応の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 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 チューターと学ぶ AI Agents with LangChain & Autonomous Workflows — 無料

ブラウザでリアルコードを書いて実行し、24/7 の AI チューターから瞬時にサポートを受け、ウェブまたはアプリで続きから学習できます。

コース
12
レッスン
50

よくある質問

「レート制限とAPIクォータの管理」レッスンは無料ですか?

はい。「レート制限とAPIクォータの管理」の完全なテキストはこのウェブで無料で読めます。インタラクティブに演習し(組み込みコードエディタと24時間対応のAIチューター)、AI Agents with LangChain & Autonomous Workflowsコースの残りをアンロックするには、CoddyKit PROにアップグレードしてください。 AI Agents with LangChain & Autonomous Workflowsコースには全4レッスンが含まれています。

「レート制限とAPIクォータの管理」で何を学びますか?

リクエストのスロットリング、バックオフ付きの再試行、ユーザーごとのクォータ管理によって、プロバイダーのレート制限や予想外のコスト増から本番エージェントを守ります。 ブラウザで直接実行するハンズオンコードでAI Agents with LangChain & Autonomous Workflowsを演習し、24時間対応の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フィードバックを取得できます。ローカル設定は不要です。

このコースのすべてのレッスン

  1. エージェントのクラウドプラットフォームへのデプロイ
  2. エージェントの状態とセッションの管理
  3. エージェントアーキテクチャのスケーリング
  4. レート制限とAPIクォータの管理
← AI Agents with LangChain & Autonomous Workflowsに戻る