error handling

This commit is contained in:
AI Christianson 2024-12-11 22:38:01 -05:00
parent aa497f2ec3
commit c1bcc000a7
1 changed files with 15 additions and 4 deletions

View File

@ -28,6 +28,8 @@ from ra_aid.prompts import (
SUMMARY_PROMPT SUMMARY_PROMPT
) )
from ra_aid.exceptions import TaskCompletedException from ra_aid.exceptions import TaskCompletedException
import time
from anthropic import APIError, APITimeoutError, RateLimitError, InternalServerError
def parse_arguments(): def parse_arguments():
parser = argparse.ArgumentParser( parser = argparse.ArgumentParser(
@ -123,9 +125,12 @@ def run_agent_with_retry(agent, prompt: str, config: dict):
Raises: Raises:
TaskCompletedException: If the task is completed and should exit TaskCompletedException: If the task is completed and should exit
Other exceptions are retried RuntimeError: If max retries exceeded
""" """
while True: max_retries = 20
base_delay = 1 # Initial delay in seconds
for attempt in range(max_retries):
try: try:
for chunk in agent.stream( for chunk in agent.stream(
{"messages": [HumanMessage(content=prompt)]}, {"messages": [HumanMessage(content=prompt)]},
@ -133,8 +138,14 @@ def run_agent_with_retry(agent, prompt: str, config: dict):
): ):
print_agent_output(chunk) print_agent_output(chunk)
break break
except ChatAnthropic.InternalServerError as e: except (InternalServerError, APITimeoutError, RateLimitError, APIError) as e:
print(f"Encountered Anthropic Internal Server Error: {e}. Retrying...") if attempt == max_retries - 1:
raise RuntimeError(f"Max retries ({max_retries}) exceeded. Last error: {str(e)}")
delay = base_delay * (2 ** attempt) # Exponential backoff
error_type = e.__class__.__name__
print(f"Encountered {error_type}: {str(e)}. Retrying in {delay} seconds... (Attempt {attempt + 1}/{max_retries})")
time.sleep(delay)
continue continue
def run_implementation_stage(base_task, tasks, plan, related_files): def run_implementation_stage(base_task, tasks, plan, related_files):