π Graph with Loops - Multi-Agent Feedback Cycles¶
This example demonstrates how to create multi-agent graphs with feedback loops using the Strands Agents SDK. It showcases a write-review-improve cycle where content iterates through multiple agents until quality standards are met.
Overview¶
Feature | Description |
---|---|
Framework | Multi-Agent Graph with Loops |
Complexity | Advanced |
Agent Types | Multiple Agents + Custom Node |
Interaction | Interactive Command Line |
Key Focus | Feedback Loops & Conditional Execution |
Usage Examples¶
Basic usage:
Import in your code:
from examples.python.graph_loops_example import create_content_loop
# Create and run a content improvement loop
graph = create_content_loop()
result = graph("Write a haiku about programming")
print(result)
Graph Structure¶
The example creates a feedback loop:
graph TD
A[Writer] --> B[Quality Checker]
B --> C{Quality Check}
C -->|Needs Revision| A
C -->|Approved| D[Finalizer]
The checker requires multiple iterations before approving content, demonstrating how conditional loops work in practice.
Core Components¶
1. Writer Agent - Content Creation¶
Creates or improves content based on the task and any feedback from previous iterations.
2. Quality Checker - Custom Deterministic Node¶
A custom node that evaluates content quality without using LLMs. Demonstrates how to create deterministic business logic nodes.
3. Finalizer Agent - Content Polish¶
Takes approved content and adds final polish in a professional format.
Loop Implementation¶
Conditional Logic¶
The graph uses conditional functions to control the feedback loop:
def needs_revision(state):
# Check if content needs more work
checker_result = state.results.get("checker")
# Navigate nested results to get approval state
return not approved_status
def is_approved(state):
# Check if content is ready for finalization
return approved_status
Safety Mechanisms¶
builder.set_max_node_executions(10) # Prevent infinite loops
builder.set_execution_timeout(60) # Maximum execution time
builder.reset_on_revisit(True) # Reset state on loop back
Custom Node¶
The QualityChecker
shows how to create deterministic nodes:
class QualityChecker(MultiAgentBase):
async def invoke_async(self, task, **kwargs):
self.iteration += 1
approved = self.iteration >= self.approval_after
# Return result with state for conditions
return MultiAgentResult(...)
Sample Execution¶
Task: "Write a haiku about programming loops"
Execution Flow:
Loop Statistics: - writer node executed 2 times (looped 1 time) - checker node executed 2 times (looped 1 time)
Final Output:
# Programming Loops: A Haiku
Code circles around,
While conditions guide the pathβ
Logic finds its way.
Interactive Usage¶
The example provides an interactive command-line interface:
π Graph with Loops Example
Options:
'demo' - Run demo with haiku task
'exit' - Exit the program
Or enter any content creation task:
'Write a short story about AI'
'Create a product description for a smart watch'
> demo
Running demo task: Write a haiku about programming loops
Execution path: writer -> checker -> writer -> checker -> finalizer
Loops detected: writer (2x), checker (2x)
β¨ Final Result:
# Programming Loops: A Haiku
Code circles around,
While conditions guide the pathβ
Logic finds its way.
Real-World Applications¶
This feedback loop pattern is useful for:
- Content Workflows: Draft β Review β Revise β Approve
- Code Review: Code β Test β Fix β Merge
- Quality Control: Produce β Inspect β Fix β Re-inspect
- Iterative Optimization: Measure β Analyze β Optimize β Validate
Extending the Example¶
Ways to enhance this example:
- Multi-Criteria Checking: Add multiple quality dimensions (grammar, style, accuracy)
- Parallel Paths: Create concurrent review processes for different aspects
- Human-in-the-Loop: Integrate manual approval steps
- Dynamic Thresholds: Adjust quality standards based on context
- Performance Metrics: Add detailed timing and quality tracking
- Visual Monitoring: Create real-time loop execution visualization
This example demonstrates how to build sophisticated multi-agent workflows with feedback loops, combining AI agents with deterministic business logic for robust, iterative processes.