Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: retry on anthropic overloaded #356

Merged
merged 2 commits into from
Dec 21, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions gptme/llm/llm_anthropic.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import base64
import logging
import time
from collections.abc import Generator
from functools import wraps
from pathlib import Path
from typing import (
TYPE_CHECKING,
Expand All @@ -19,13 +21,41 @@
import anthropic.types # fmt: skip
from anthropic import Anthropic # fmt: skip


logger = logging.getLogger(__name__)

_anthropic: "Anthropic | None" = None

ALLOWED_FILE_EXTS = ["jpg", "jpeg", "png", "gif"]


def retry_generator_on_overloaded(max_retries: int = 5, base_delay: float = 1.0):
"""Decorator to retry generator functions on Anthropic API overloaded errors with exponential backoff."""

def decorator(func):
@wraps(func)
def wrapper(*args, **kwargs):
from anthropic import APIStatusError # fmt: skip

for attempt in range(max_retries):
try:
yield from func(*args, **kwargs)
break # If generator completes successfully, exit retry loop
except APIStatusError as e:
if e.status_code != 503 or attempt == max_retries - 1:
raise

delay = base_delay * (2**attempt)
logger.warning(
f"Anthropic API overloaded, retrying in {delay}s (attempt {attempt + 1}/{max_retries})"
)
time.sleep(delay)

return wrapper

return decorator


def init(config):
global _anthropic
api_key = config.get_env_required("ANTHROPIC_API_KEY")
Expand Down Expand Up @@ -69,6 +99,7 @@ def chat(messages: list[Message], model: str, tools: list[ToolSpec] | None) -> s
return content[0].text # type: ignore


@retry_generator_on_overloaded()
def stream(
messages: list[Message], model: str, tools: list[ToolSpec] | None
) -> Generator[str, None, None]:
Expand Down
Loading