AI APIを運用する上で避けて通れない課題、それがリクエスト失敗時のリトライ戦略です。私は複数の本番環境で両アプローチを実装し、実際のレイテンシ・コスト・成功率の違いを検証してきました。本稿では、理論解説と実務的なコード例を通じて、最適なリトライ戦略の選択方法を解説します。
リトライ戦略の基礎:なぜ必要か
AI API(GPT-4.1、Claude Sonnet 4.5、Gemini 2.5 Flashなど)は、輻輳・サーバー過負荷・一時的なネットワーク問題により429 Too Many Requestsや503 Service Unavailableを返します。これらのエラーに対する適切なリトライ戦略は、可用性とコスト効率の両面で重要です。
2026年主要AI API価格比較(トークンコスト)
リトライ戦略のコストへの影響を計算するため、まず主要APIの2026年最新価格を確認しましょう。HolySheep AIは登録することで¥1=$1の為替レートでこれらのAPIを利用できます(公式サイト比85%節約)。
| モデル | Output価格 ($/MTok) | Input価格 ($/MTok) | 月間1000万トークン時の概算コスト |
|---|---|---|---|
| GPT-4.1 | $8.00 | $2.40 | $80(出力のみ) |
| Claude Sonnet 4.5 | $15.00 | $7.50 | $150(出力のみ) |
| Gemini 2.5 Flash | $2.50 | $0.30 | $25(出力のみ) |
| DeepSeek V3.2 | $0.42 | $0.27 | $4.2(出力のみ) |
※ HolySheep AIでは¥1=$1のレートが適用され、日本円での請求价格为上記ドル額を7.3で除した金額になります。
Exponential Backoff(指数関数的バックオフ)
仕組み
指数関数的バックオフは、失敗後に待機時間を指数関数的に増加させます。基本的な計算式は:
待機時間 = base_delay × (exponent ^ attempt) + jitter
このアプローチは、サーバー負荷が高い情况进行適切に考慮し、指数関数的に増加する待機時間で段階的に負荷を分散させます。
実装例(Python + HolySheep AI)
import asyncio
import aiohttp
import random
import time
BASE_URL = "https://api.holysheep.ai/v1"
API_KEY = "YOUR_HOLYSHEEP_API_KEY"
MAX_RETRIES = 5
BASE_DELAY = 1.0 # 基本遅延(秒)
MAX_DELAY = 32.0 # 最大遅延(秒)
async def exponential_backoff_request(
session: aiohttp.ClientSession,
prompt: str,
model: str = "gpt-4.1"
):
"""
Exponential Backoff を適用したAPIリクエスト
待機時間 = 1秒 × 2^attempt + ランダムジャダー
"""
headers = {
"Authorization": f"Bearer {API_KEY}",
"Content-Type": "application/json"
}
payload = {
"model": model,
"messages": [{"role": "user", "content": prompt}],
"max_tokens": 1000
}
for attempt in range(MAX_RETRIES):
try:
async with session.post(
f"{BASE_URL}/chat/completions",
headers=headers,
json=payload,
timeout=aiohttp.ClientTimeout(total=60)
) as response:
if response.status == 200:
data = await response.json()
return data["choices"][0]["message"]["content"]
elif response.status == 429:
# Rate Limit - Exponential Backoff
delay = min(BASE_DELAY * (2 ** attempt) + random.uniform(0, 1), MAX_DELAY)
print(f"Attempt {attempt + 1}: Rate limited. Waiting {delay:.2f}s...")
await asyncio.sleep(delay)
elif response.status >= 500:
# Server Error - Retry
delay = min(BASE_DELAY * (2 ** attempt) + random.uniform(0, 1), MAX_DELAY)
print(f"Attempt {attempt + 1}: Server error. Waiting {delay:.2f}s...")
await asyncio.sleep(delay)
else:
# Client Error (4xx) - スキップ
raise Exception(f"API Error: {response.status}")
except aiohttp.ClientError as e:
if attempt == MAX_RETRIES - 1:
raise
delay = min(BASE_DELAY * (2 ** attempt), MAX_DELAY)
print(f"Attempt {attempt + 1}: Connection error. Waiting {delay:.2f}s...")
await asyncio.sleep(delay)
raise Exception("Max retries exceeded")
使用例
async def main():
async with aiohttp.ClientSession() as session:
result = await exponential_backoff_request(session, "Hello, world!")
print(result)
asyncio.run(main())
Linear Backoff(線形バックオフ)
仕組み
線形バックオフは、失敗後に待機時間を一定の量ずつ増加させます:
待機時間 = base_delay + (increment × attempt)
このアプローチは、予測可能性が高く、負荷が緩やかに変動する環境に向いています。
実装例(Python + HolySheep AI)
import asyncio
import aiohttp
import random
import time
BASE_URL = "https://api.holysheep.ai/v1"
API_KEY = "YOUR_HOLYSHEEP_API_KEY"
MAX_RETRIES = 5
BASE_DELAY = 1.0 # 基本遅延(秒)
INCREMENT = 2.0 # 増分(秒)
async def linear_backoff_request(
session: aiohttp.ClientSession,
prompt: str,
model: str = "gpt-4.1"
):
"""
Linear Backoff を適用したAPIリクエスト
待機時間 = 1秒 + 2秒 × attempt + ランダムジャダー
"""
headers = {
"Authorization": f"Bearer {API_KEY}",
"Content-Type": "application/json"
}
payload = {
"model": model,
"messages": [{"role": "user", "content": prompt}],
"max_tokens": 1000
}
for attempt in range(MAX_RETRIES):
try:
async with session.post(
f"{BASE_URL}/chat/completions",
headers=headers,
json=payload,
timeout=aiohttp.Client