Loop
An iterative optimization pattern where an agent autonomously loops through testing and refinement cycles using sub-agent delegation.
Overview
The loop pattern is ideal when you need iterative refinement with feedback loops. Unlike linear pipelines, this pattern allows agents to cycle back, test, and improve their output based on execution results. The optimizer agent makes autonomous decisions about when to iterate and when to stop.
Demo Scenario: Code Optimizer with Feedback Loop
This example demonstrates an autonomous optimization loop using sub-agent delegation:
Optimizer agent – proposes code improvements and decides when to iterate
Executor agent – runs and benchmarks the code, providing feedback
The optimizer performs an internal loop (up to 3 iterations): it proposes code, delegates execution to the executor agent, analyzes the benchmark results, and refines the approach. This cyclic workflow is why we use sub-agent delegation instead of gllm-pipeline.
Diagram
Implementation Steps
Create executor agent with code execution tool
Create optimizer agent with executor as sub-agent
Run the optimizer
Full implementation: See
loop/main.pyfor complete code with detailed instructions.Why sub-agents? This pattern uses sub-agent delegation (not gllm-pipeline) because the optimizer needs to make autonomous decisions about looping back based on execution feedback. gllm-pipeline is designed for linear workflows without cyclic control flow.
How to Run
From the gl-aip/examples/multi-agent-system-patterns directory in the GL SDK Cookbook:
Ensure your .env contains:
Note: You'll need an E2B API key for the code sandbox functionality.
Output
Notes
This pattern uses sub-agent delegation (not gllm-pipeline) because of the cyclic nature of the workflow.
The optimizer agent autonomously decides when to loop back and when to stop based on the executor's feedback.
Use this pattern when you need iterative refinement with feedback loops - testing, optimization, validation cycles.
The maximum iteration count is controlled via the optimizer's instructions, preventing infinite loops.
For non-cyclic workflows, prefer patterns that use gllm-pipeline (Sequential, Parallel, Router, Aggregator) which leverage the AgentComponent wrapper.
Related Documentation
Agents guide — Configure nested agents and delegation patterns.
Tools guide — Manage execution tools like e2b_sandbox_tool.
Hierarchical pattern — Another pattern using sub-agent delegation for decision-based workflows.
Security & privacy — Apply sandbox and execution policies for code running agents.
Last updated
Was this helpful?