Stopping Tasks Early
Take fine-grained control over task execution.
When working with complex AI workflows, it’s often necessary to repeatedly invoke an agent until a task is completed. This pattern allows for flexible, iterative approaches to task completion, especially useful when the number of steps required is unknown or variable.
Here’s how you can implement this pattern in ControlFlow:
This pattern is particularly useful when:
- The task may require multiple attempts or refinements
- You want to implement custom logic between iterations
- You need to dynamically adjust the workflow based on other results
By using a while loop with task.is_incomplete()
, you ensure that the agent continues working until the task is either marked as complete or fails. The run_once()
method allows for step-by-step execution, giving you control over each iteration of the agentic loop.