> ## Documentation Index
> Fetch the complete documentation index at: https://agno-v2-service-account.mintlify.site/llms.txt
> Use this file to discover all available pages before exploring further.

# Nested Workflow with Loop

> Demonstrates using a workflow (containing a Loop step) as a step in an outer workflow.

Demonstrates using a workflow (containing a Loop step) as a step in an outer workflow. The inner workflow iteratively refines research until it meets a quality threshold, then the outer workflow writes.

```python nested_workflow_with_loop.py theme={null}
"""
Nested Workflow with Loop

Demonstrates using a workflow (containing a Loop step) as a step
in an outer workflow. The inner workflow iteratively refines research
until it meets a quality threshold, then the outer workflow writes.
"""

from typing import List

from agno.agent import Agent
from agno.models.openai import OpenAIResponses
from agno.workflow import Loop
from agno.workflow.step import Step
from agno.workflow.types import StepOutput
from agno.workflow.workflow import Workflow


def is_detailed_enough(outputs: List[StepOutput]) -> bool:
    """End the loop when the output is sufficiently detailed (> 200 chars)."""
    if not outputs:
        return False
    last = outputs[-1]
    return last.content is not None and len(str(last.content)) > 200


# --- Inner workflow: iterative research ---
researcher = Agent(
    name="Iterative Researcher",
    model=OpenAIResponses(id="gpt-5.4"),
    instructions=(
        "You are a researcher. Each iteration, expand on the previous research "
        "with more detail and specifics. Build on what was already written."
    ),
)

inner_workflow = Workflow(
    name="Iterative Research",
    description="Researches a topic in iterative passes until sufficiently detailed",
    steps=[
        Loop(
            name="research_loop",
            steps=[Step(name="research_pass", agent=researcher)],
            end_condition=is_detailed_enough,
            max_iterations=3,
        ),
    ],
)

# --- Outer workflow ---
writer = Agent(
    name="Writer",
    model=OpenAIResponses(id="gpt-5.4"),
    instructions="Write a polished summary from the detailed research provided.",
)

outer_workflow = Workflow(
    name="Iterative Research and Write",
    description="Iteratively researches until detailed, then writes a summary",
    steps=[
        Step(name="research_phase", workflow=inner_workflow),
        Step(name="writing_phase", agent=writer),
    ],
)


if __name__ == "__main__":
    outer_workflow.print_response(
        input="Explain how neural networks learn",
        stream=True,
    )
```

## Run the Example

<Steps>
  <Snippet file="create-venv-step.mdx" />

  <Step title="Install dependencies">
    ```bash theme={null}
    uv pip install -U agno cel-python fastapi openai
    ```
  </Step>

  <Step title="Export your OpenAI API key">
    <CodeGroup>
      ```bash Mac/Linux theme={null}
      export OPENAI_API_KEY="your_openai_api_key_here"
      ```

      ```bash Windows theme={null}
      $Env:OPENAI_API_KEY="your_openai_api_key_here"
      ```
    </CodeGroup>
  </Step>

  <Step title="Run the example">
    Save the code above as `nested_workflow_with_loop.py`, then run:

    ```bash theme={null}
    python nested_workflow_with_loop.py
    ```
  </Step>
</Steps>

Full source: [cookbook/04\_workflows/06\_advanced\_concepts/workflow\_as\_a\_step/nested\_workflow\_with\_loop.py](https://github.com/agno-agi/agno/blob/main/cookbook/04_workflows/06_advanced_concepts/workflow_as_a_step/nested_workflow_with_loop.py)
