From c1bcc000a7441ae12a96a2746715466649b5f8ca Mon Sep 17 00:00:00 2001 From: AI Christianson Date: Wed, 11 Dec 2024 22:38:01 -0500 Subject: [PATCH] error handling --- ra_aid/__main__.py | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/ra_aid/__main__.py b/ra_aid/__main__.py index d022ae9..105ec0e 100644 --- a/ra_aid/__main__.py +++ b/ra_aid/__main__.py @@ -28,6 +28,8 @@ from ra_aid.prompts import ( SUMMARY_PROMPT ) from ra_aid.exceptions import TaskCompletedException +import time +from anthropic import APIError, APITimeoutError, RateLimitError, InternalServerError def parse_arguments(): parser = argparse.ArgumentParser( @@ -123,9 +125,12 @@ def run_agent_with_retry(agent, prompt: str, config: dict): Raises: 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: for chunk in agent.stream( {"messages": [HumanMessage(content=prompt)]}, @@ -133,8 +138,14 @@ def run_agent_with_retry(agent, prompt: str, config: dict): ): print_agent_output(chunk) break - except ChatAnthropic.InternalServerError as e: - print(f"Encountered Anthropic Internal Server Error: {e}. Retrying...") + except (InternalServerError, APITimeoutError, RateLimitError, APIError) as e: + 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 def run_implementation_stage(base_task, tasks, plan, related_files):