🔄 Graph with Loops - Multi-Agent Feedback Cycles
Section titled “🔄 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
Section titled “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
Section titled “Usage Examples”Basic usage:
python graph_loops_example.pyImport in your code:
from examples.python.graph_loops_example import create_content_loop
# Create and run a content improvement loopgraph = create_content_loop()result = graph("Write a haiku about programming")print(result)Graph Structure
Section titled “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
Section titled “Core Components”1. Writer Agent - Content Creation
Section titled “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
Section titled “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
Section titled “3. Finalizer Agent - Content Polish”Takes approved content and adds final polish in a professional format.
Loop Implementation
Section titled “Loop Implementation”Conditional Logic
Section titled “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_statusSafety Mechanisms
Section titled “Safety Mechanisms”builder.set_max_node_executions(10) # Prevent infinite loopsbuilder.set_execution_timeout(60) # Maximum execution timebuilder.reset_on_revisit(True) # Reset state on loop backCustom Node
Section titled “Custom Node”The QualityChecker shows how to create deterministic nodes:
class QualityChecker(MultiAgentBase): async def invoke_async(self, task, invocation_state, **kwargs): self.iteration += 1 approved = self.iteration >= self.approval_after
# Return result with state for conditions return MultiAgentResult(...)Sample Execution
Section titled “Sample Execution”Task: “Write a haiku about programming loops”
Execution Flow:
writer -> checker -> writer -> checker -> finalizerLoop 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
Section titled “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'
> demoRunning demo task: Write a haiku about programming loops
Execution path: writer -> checker -> writer -> checker -> finalizerLoops 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
Section titled “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
Section titled “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.