This example demonstrates Workflows 2.0 loop execution for quality-driven iterative processes.
This example demonstrates Workflows 2.0 to repeatedly execute steps until specific conditions are met,
ensuring adequate research depth before proceeding to content creation.When to use: When you need iterative refinement, quality assurance, or when the
required output quality can’t be guaranteed in a single execution. Ideal for research
gathering, data collection, or any process where “good enough” is determined by content
analysis rather than a fixed number of iterations.
loop_steps_workflow.py
Copy
Ask AI
from typing import Listfrom agno.agent import Agentfrom agno.tools.duckduckgo import DuckDuckGoToolsfrom agno.tools.hackernews import HackerNewsToolsfrom agno.workflow.v2 import Loop, Step, Workflowfrom agno.workflow.v2.types import StepOutput# Create agents for researchresearch_agent = Agent( name="Research Agent", role="Research specialist", tools=[HackerNewsTools(), DuckDuckGoTools()], instructions="You are a research specialist. Research the given topic thoroughly.", markdown=True,)content_agent = Agent( name="Content Agent", role="Content creator", instructions="You are a content creator. Create engaging content based on research.", markdown=True,)# Create research stepsresearch_hackernews_step = Step( name="Research HackerNews", agent=research_agent, description="Research trending topics on HackerNews",)research_web_step = Step( name="Research Web", agent=research_agent, description="Research additional information from web sources",)content_step = Step( name="Create Content", agent=content_agent, description="Create content based on research findings",)# End condition functiondef research_evaluator(outputs: List[StepOutput]) -> bool: """ Evaluate if research results are sufficient Returns True to break the loop, False to continue """ # Check if any outputs are present if not outputs: return False # Check if any output contains substantial content for output in outputs: if output.content and len(output.content) > 200: print( f"✅ Research evaluation passed - found substantial content ({len(output.content)} chars)" ) return True print("❌ Research evaluation failed - need more substantial research") return False# Create workflow with loopworkflow = Workflow( name="Research and Content Workflow", description="Research topics in a loop until conditions are met, then create content", steps=[ Loop( name="Research Loop", steps=[research_hackernews_step, research_web_step], end_condition=research_evaluator, max_iterations=3, # Maximum 3 iterations ), content_step, ],)if __name__ == "__main__": # Test the workflow workflow.print_response( message="Research the latest trends in AI and machine learning, then create a summary", )
This was a synchronous non-streaming example of this pattern. To checkout async and streaming versions, see the cookbooks-