Copy
Ask AI
"""
Workflow With Loop
==================
Demonstrates workflow with loop.
"""
from typing import List
from agno.agent.agent import Agent
from agno.db.sqlite import SqliteDb
from agno.models.openai.chat import OpenAIChat
# ---------------------------------------------------------------------------
# Create Example
# ---------------------------------------------------------------------------
# Import the workflows
from agno.os import AgentOS
from agno.tools.hackernews import HackerNewsTools
from agno.tools.websearch import WebSearchTools
from agno.workflow.loop import Loop
from agno.workflow.step import Step
from agno.workflow.types import StepOutput
from agno.workflow.workflow import Workflow
research_agent = Agent(
name="Research Agent",
role="Research specialist",
model=OpenAIChat(id="gpt-4o-mini"),
tools=[HackerNewsTools(), WebSearchTools()],
instructions="You are a research specialist. Research the given topic thoroughly.",
markdown=True,
)
content_agent = Agent(
name="Content Agent",
model=OpenAIChat(id="gpt-4o-mini"),
role="Content creator",
instructions="You are a content creator. Create engaging content based on research.",
markdown=True,
)
# Create research steps
research_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 function
def research_evaluator(outputs: List[StepOutput]) -> bool:
"""
Evaluate if research results are sufficient
Returns True to break the loop, False to continue
"""
# Check if we have good research results
if not outputs:
return False
# Simple check - if any output contains substantial content, we're good
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 loop
workflow = 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,
],
db=SqliteDb(
session_table="workflow_session",
db_file="tmp/workflow.db",
),
)
# Initialize the AgentOS with the workflows
agent_os = AgentOS(
description="Example OS setup",
workflows=[workflow],
)
app = agent_os.get_app()
# ---------------------------------------------------------------------------
# Run Example
# ---------------------------------------------------------------------------
if __name__ == "__main__":
agent_os.serve(app="workflow_with_loop:app", reload=True)
Run the Example
Copy
Ask AI
# Clone and setup repo
git clone https://github.com/agno-agi/agno.git
cd agno/cookbook/05_agent_os/workflow
# Create and activate virtual environment
./scripts/demo_setup.sh
source .venvs/demo/bin/activate
python workflow_with_loop.py