Skip to main content
If after a tool call you need to provide feedback to the model to change its behavior or exit the tool call loop, you can raise one of the following exceptions:
  • RetryAgentRun: Use this exception when you want to provide instructions to the model for how to change its behavior and have the model retry the tool call. The exception message will be passed to the model as a tool call error, allowing the model to retry or 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.
  • StopAgentRun: Use this exception when you want to exit the model execution loop and end the agent run. When raised from a tool function, the agent exits the tool call loop, and the run status is set to COMPLETED. All session state, messages, tool calls, and tool results up to that point are stored in the database.
This does not cancel the agent run. It completes the run after exiting the tool call loop.

Using RetryAgentRun

This example shows how to use the RetryAgentRun exception to provide feedback to the model, allowing it to adjust its behavior:
retry_in_tool_call.py
In this example, when add_item is called with fewer than 3 items, it raises RetryAgentRun with instructions. The model receives this as a tool call error and can call add_item again with additional items to meet the requirement.

Using StopAgentRun

This example shows how to use the StopAgentRun exception to exit the tool call loop:
stop_in_tool_call.py
In this example, when check_condition is called with a value greater than 100, it raises StopAgentRun. The tool call loop exits immediately, and the run completes with status COMPLETED. All session state, messages, and tool calls up to that point are stored in the database.
Make sure to set AGNO_DEBUG=True if you want to see the debug logs.

Developer Resources