From 22f45c66668cc39809f05d8908605ded474ede1b Mon Sep 17 00:00:00 2001 From: Ishaan Jaffer Date: Sat, 11 Apr 2026 18:16:56 -0700 Subject: [PATCH] fix(advisor): restore AdvisorMaxIterationsError, raise on cap, fix max_uses=0 falsy --- .../messages/interceptors/advisor.py | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/litellm/llms/anthropic/experimental_pass_through/messages/interceptors/advisor.py b/litellm/llms/anthropic/experimental_pass_through/messages/interceptors/advisor.py index 797fd535f47..02437c9b63f 100644 --- a/litellm/llms/anthropic/experimental_pass_through/messages/interceptors/advisor.py +++ b/litellm/llms/anthropic/experimental_pass_through/messages/interceptors/advisor.py @@ -31,6 +31,10 @@ ADVISOR_TOOL_DESCRIPTION: str = _c.ADVISOR_TOOL_DESCRIPTION from .base import MessagesInterceptor +class AdvisorMaxIterationsError(Exception): + """Raised when the advisor loop exceeds max_uses.""" + + class AdvisorOrchestrationHandler(MessagesInterceptor): """Orchestrates the advisor tool loop for non-native providers.""" @@ -74,7 +78,8 @@ class AdvisorOrchestrationHandler(MessagesInterceptor): raise ValueError( "advisor tool definition must include a 'model' field specifying the advisor model" ) - max_uses: int = advisor_tool.get("max_uses") or ADVISOR_MAX_USES + _raw_max_uses = advisor_tool.get("max_uses") + max_uses: int = ADVISOR_MAX_USES if _raw_max_uses is None else int(_raw_max_uses) # Optional routing overrides for the advisor sub-call (e.g. proxy routing). # If not set in the tool definition, litellm resolves from env vars. advisor_api_key: Optional[str] = advisor_tool.get("api_key") @@ -131,12 +136,10 @@ class AdvisorOrchestrationHandler(MessagesInterceptor): iteration += 1 if iteration > max_uses: - # Per Anthropic spec: inject max_uses_exceeded error result so the - # executor sees the cap and continues without further advice. - current_messages = _inject_max_uses_error( - current_messages, executor_response, advisor_use_block + raise AdvisorMaxIterationsError( + f"Advisor orchestration loop exceeded max_uses={max_uses}. " + "Increase max_uses in the advisor tool definition or cap the request." ) - continue # --- Build advisor context --- advisor_messages = _build_advisor_context(