diff --git a/backend/agentpress/response_processor.py b/backend/agentpress/response_processor.py index 40eb6675..470aaf44 100644 --- a/backend/agentpress/response_processor.py +++ b/backend/agentpress/response_processor.py @@ -798,16 +798,20 @@ class ResponseProcessor: logger.error(f"Error processing stream: {str(e)}", exc_info=True) self.trace.event(name="error_processing_stream", level="ERROR", status_message=(f"Error processing stream: {str(e)}")) # Save and yield error status message - err_content = {"role": "system", "status_type": "error", "message": str(e)} - err_msg_obj = await self.add_message( - thread_id=thread_id, type="status", content=err_content, - is_llm_message=False, metadata={"thread_run_id": thread_run_id if 'thread_run_id' in locals() else None} - ) - if err_msg_obj: yield format_for_yield(err_msg_obj) # Yield the saved error message - # Re-raise the same exception (not a new one) to ensure proper error propagation - logger.critical(f"Re-raising error to stop further processing: {str(e)}") - self.trace.event(name="re_raising_error_to_stop_further_processing", level="ERROR", status_message=(f"Re-raising error to stop further processing: {str(e)}")) + err_content = {"role": "system", "status_type": "error", "message": str(e)} + if (not "AnthropicException - Overloaded" in str(e)): + err_msg_obj = await self.add_message( + thread_id=thread_id, type="status", content=err_content, + is_llm_message=False, metadata={"thread_run_id": thread_run_id if 'thread_run_id' in locals() else None} + ) + if err_msg_obj: yield format_for_yield(err_msg_obj) # Yield the saved error message + # Re-raise the same exception (not a new one) to ensure proper error propagation + logger.critical(f"Re-raising error to stop further processing: {str(e)}") + self.trace.event(name="re_raising_error_to_stop_further_processing", level="ERROR", status_message=(f"Re-raising error to stop further processing: {str(e)}")) + else: + logger.error(f"AnthropicException - Overloaded detected - Falling back to OpenRouter: {str(e)}", exc_info=True) + self.trace.event(name="anthropic_exception_overloaded_detected", level="ERROR", status_message=(f"AnthropicException - Overloaded detected - Falling back to OpenRouter: {str(e)}")) raise # Use bare 'raise' to preserve the original exception with its traceback finally: diff --git a/backend/agentpress/thread_manager.py b/backend/agentpress/thread_manager.py index ec46710e..a4d1d974 100644 --- a/backend/agentpress/thread_manager.py +++ b/backend/agentpress/thread_manager.py @@ -477,13 +477,20 @@ Here are the XML tools available with examples: if not auto_continue: break except Exception as e: - # If there's an exception, log it, yield an error status, and stop execution - logger.error(f"Error in auto_continue_wrapper generator: {str(e)}", exc_info=True) - yield { - "type": "status", - "status": "error", - "message": f"Error in thread processing: {str(e)}" - } + if ("AnthropicException - Overloaded" in str(e)): + logger.error(f"AnthropicException - Overloaded detected - Falling back to OpenRouter: {str(e)}", exc_info=True) + nonlocal llm_model + llm_model = f"openrouter/{llm_model}" + auto_continue = True + continue # Continue the loop + else: + # If there's any other exception, log it, yield an error status, and stop execution + logger.error(f"Error in auto_continue_wrapper generator: {str(e)}", exc_info=True) + yield { + "type": "status", + "status": "error", + "message": f"Error in thread processing: {str(e)}" + } return # Exit the generator on any error except Exception as outer_e: # Catch exceptions from _run_once itself diff --git a/backend/services/llm.py b/backend/services/llm.py index 1b5c61c5..f63f4539 100644 --- a/backend/services/llm.py +++ b/backend/services/llm.py @@ -172,7 +172,7 @@ def prepare_params( # Apply cache control to the first 4 text blocks across all messages cache_control_count = 0 - max_cache_control_blocks = 4 + max_cache_control_blocks = 3 for message in messages: if cache_control_count >= max_cache_control_blocks: diff --git a/backend/utils/logger.py b/backend/utils/logger.py index 4c9d805f..2d7ecf14 100644 --- a/backend/utils/logger.py +++ b/backend/utils/logger.py @@ -6,8 +6,8 @@ LOGGING_LEVEL = logging.getLevelNamesMapping().get( ) renderer = [structlog.processors.JSONRenderer()] -if ENV_MODE.lower() == "local".lower() or ENV_MODE.lower() == "staging".lower(): - renderer = [structlog.dev.ConsoleRenderer()] +# if ENV_MODE.lower() == "local".lower() or ENV_MODE.lower() == "staging".lower(): +# renderer = [structlog.dev.ConsoleRenderer()] structlog.configure( processors=[