- 失敗、逾時的請求不扣積分;一次請求裡只成功了一部分,只扣成功的那幾張。
- 重試時帶上
Idempotency-Key 請求頭:同一個鍵的請求不會被執行兩次,也不會扣兩次。 - 遇到 429 就等一會兒再試,間隔逐次加倍。
- 結果用
response_format: "url" 拿連結,連結保留 72 小時,請及時下載到自己的儲存。
import asyncio
from openai import AsyncOpenAI, RateLimitError
client = AsyncOpenAI(base_url="https://api.cheapestimagen.com/v1", api_key="YOUR_KEY")
gate = asyncio.Semaphore(50) # the account limit on requests in progress
async def make(prompt: str, key: str):
async with gate:
for attempt in range(5):
try:
r = await client.images.generate(
model="nano-banana-2.1", prompt=prompt, size="1024x1024", response_format="url",
extra_headers={"Idempotency-Key": key}, # a retry never charges twice
)
return r.data[0].url
except RateLimitError:
await asyncio.sleep(2 ** attempt)
async def main(prompts):
return await asyncio.gather(*(make(p, f"job-{i}") for i, p in enumerate(prompts)))
限制的完整說明見呼叫限制和批次生成,所有錯誤碼見錯誤說明。在 n8n 裡跑見 n8n 接入。