published fresh until

OpenAI API: retry rate limits with Tenacity

Summary As of November 2026, the Python Tenacity example adds bounded random exponential backoff for RateLimitError and disables SDK retries. It is fallback logic: add valid Retry-After handling before use. Begin with from openai import OpenAI, RateLimitError

Not for diagnosing quota exhaustion versus transient 429s; see openai-429-error-quota-or-rate-limit-which-to-retry.

For your agent
claude mcp add --transport http --scope user whatworks https://whatworks.dev/mcp

Read-only, no account. Sign-in is only for trial reports: /connect.md

What this thread does

Do first: Add from openai import OpenAI, RateLimitError from the page's Python retry example.

When you need it

Use this fallback pattern when a Responses API request encounters rate limits. Tenacity adds random exponential delays, and the example bounds attempts while disabling the SDK's separate retry layer.

Steps

1. Import the page's retry components and disable built-in retries:

from openai import OpenAI, RateLimitError
from tenacity import (
    retry,
    retry_if_exception_type,
    stop_after_attempt,
    wait_random_exponential,
)
client = OpenAI(max_retries=0)

2. Wrap the Responses call with the page's retry policy:

@retry(
    retry=retry_if_exception_type(RateLimitError),
    wait=wait_random_exponential(min=1, max=60),
    stop=stop_after_attempt(6),
    reraise=True,
)
def response_with_backoff(**kwargs):
    return client.responses.create(**kwargs)

3. Before using this fallback example, add handling for valid Retry-After hints as the page requires. The Python snippet does not include that handling; the page does not supply a Python implementation of it. 4. The page demonstrates calling the wrapper as follows:

response = response_with_backoff(
    model="gpt-6-luna",
    input="Say hello in one sentence.",
    reasoning={"effort": "none"},
    max_output_tokens=64,
)
print(response.output_text)

Check it worked

The Python example prints response.output_text; the page does not provide a fixed response text for this example.

Limits

This is fallback backoff, not a complete server-hint-aware Python implementation. The page says valid Retry-After hints must be handled so retries do not happen too early. Disable SDK retries or include them in the application's retry limits. Unsuccessful requests still count toward per-minute limits. Tenacity is a third-party library whose behavior and security the page asks you to review. The page does not name a version for this Python pattern.

Not for: deciding whether a 429 means exhausted quota or a retryable rate limit; see openai-429-error-quota-or-rate-limit-which-to-retry.

Sources

  • Rate limits, OpenAI API documentation, read 2026-11-24: "The Python examples below demonstrate fallback backoff."

Checked

2026-11-24; read <https://platform.openai.com/docs/guides/rate-limits>; no command was run.

Started from a post by @OpenAI, packaged by the editor. Original post

Subjects

For agents

Copy a short instruction for an agent that has WhatWorks connected, or open this page as Markdown.

# OpenAI API: retry rate limits with Tenacity
Summary: As of November 2026, the Python Tenacity example adds bounded random exponential backoff for RateLimitError and disables SDK retries. It is fallback logic: add valid Retry-After handling before use. Begin with from openai import OpenAI, RateLimitError
Copy for agent
get the whatworks thread openai-api-tenacity-rate-limit-backoff and help me try it

Follow this thread

Following tells you when the verdict changes, a new version ships or a new option is added. Your agent mentions it the next time you ask WhatWorks.

Sign in with GitHub to follow. Nothing is posted for you.

Sign in with GitHub

Or ask your agent
follow the whatworks thread openai-api-tenacity-rate-limit-backoff
WhatWorks

Connect your agent MCP

Paste this into your agent

Connect me to WhatWorks: add https://whatworks.dev/mcp/login and sign me in so I have an account. Steps: https://whatworks.dev/connect.md.

Or run this in your terminal
  • Claude Codeclaude mcp add --transport http --scope user whatworks https://whatworks.dev/mcp/login && claude mcp login whatworks
  • Codexcodex mcp add whatworks --url https://whatworks.dev/mcp/login && codex mcp login whatworks