Loop Pattern

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:

  1. Optimizer agent – proposes code improvements and decides when to iterate

  2. 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

spinner

Implementation Steps

  1. Create executor agent with code execution tool

    from glaip_sdk import Agent
    from glaip_sdk.tools import Tool
    
    e2b_sandbox_tool = Tool.from_native("e2b_sandbox_tool")
    
    executor_agent = Agent(
        name="executor_agent",
        instruction="Run code, measure runtime, and return benchmark report...",
        tools=[e2b_sandbox_tool],
        model="openai/gpt-5-mini"
    )
  2. Create optimizer agent with executor as sub-agent

    optimizer_agent = Agent(
        name="optimizer_agent",
        instruction="""Perform internal loop (up to 3 iterations):
        propose code, delegate to 'executor_agent', analyze results, refine...""",
        agents=[executor_agent],  # Sub-agent delegation
        model="openai/gpt-5-mini"
    )
  3. Run the optimizer

    result = optimizer_agent.run(prompt, verbose=False)

Full implementation: See loop/main.py for 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 glaip/examples/multi-agent-system-patterns directory in the GL SDK Cookbookarrow-up-right:

Ensure your .env contains:

Note: You'll need an E2B API keyarrow-up-right 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.

Last updated