Skip to main content
The RetryAgentRun exception allows you to provide instructions to the model for how to change its behavior and have the model retry within the current agent run. The exception message is passed to the model as a tool call error, allowing the model to adjust its approach in the next iteration of the LLM loop.
This does not retry the full agent run—it only provides feedback to the model within the current run’s tool call loop.

Constructor

RetryAgentRun(
    exc: str,
    user_message: Optional[Union[str, Message]] = None,
    agent_message: Optional[Union[str, Message]] = None,
    messages: Optional[List[Union[dict, Message]]] = None,
)

Parameters

exc
str
required
The error message to pass to the model. This message provides instructions or feedback to help the model adjust its behavior in the next iteration.
user_message
Union[str, Message]
An optional message to display to the user about the retry.
agent_message
Union[str, Message]
An optional message from the agent’s perspective about the retry.
messages
List[Union[dict, Message]]
An optional list of messages to add to the conversation history.

When to Use

Use RetryAgentRun when:
  • Validation fails: Input doesn’t meet requirements, and you want the model to try again with corrected input
  • State requirements: The current state doesn’t meet prerequisites, and you want the model to perform additional actions
  • Business logic: A condition isn’t met, and you want to guide the model on how to proceed
  • Iterative refinement: You want the model to improve its approach based on feedback

Behavior

When RetryAgentRun is raised:
  1. The exception message is added to the tool call result as an error
  2. The model receives this error in the next LLM call
  3. The model can adjust its approach and retry the tool call or try a different approach
  4. The agent run continues (does not stop or exit)
  5. All messages and tool calls are preserved in the session

See Also