advancedcorecompoundnestedcascadingadvanced

Compound Loop Engineering

Nested, cascading, and recursive loop patterns for complex multi-dimensional tasks.

Simple loops handle single-dimensional tasks: fix this bug, refactor this file, generate this test. But real-world engineering challenges are often multi-dimensional -- they require loops composed of other loops, loops that cascade across systems, and loops that adapt their behavior based on outcomes. This is compound loop engineering.

What is a Compound Loop?

A compound loop is a loop engineering system where multiple loop patterns are composed together to handle tasks that exceed the capability of any single loop. The composition can take several forms:

+-------------------------------------+
|         COMPOUND LOOP               |
|                                     |
|  +---------+    +--------------+   |
|  | Outer   |    |  Inner Loop  |   |
|  | Loop    |--->|  (per item)  |   |
|  |         |<---|              |   |
|  +----+----+    +--------------+   |
|       |                             |
|  +----v----+                        |
|  | Verify  |  <- Runs after all    |
|  | Loop    |    inner loops complete |
|  +---------+                        |
+-------------------------------------+

Real tools already implement this pattern. MetaGPT (github.com/FoundationAgents/MetaGPT, 45K+ stars), published at ICLR 2024, composes outer loops (the SOP pipeline) with inner loops (per-role agent iteration). It takes a one-line requirement and runs it through a Product Manager loop, Architect loop, and Engineer loop in sequence -- each with its own internal iteration. CrewAI (github.com/crewAIInc/crewAI) formalizes this further with explicit Sequential, Parallel, and Hierarchical process types that compose agent loops into compound workflows.

Compound Loop Patterns

Pattern 1: Sequential Compound Loop

Loops execute in sequence, where the output of one feeds into the next:

Discover Issues --> Fix Each Issue --> Verify All Fixes --> Deploy
    [Loop 1]        [Loop 2]         [Loop 3]      [Loop 4]

Use when: Tasks have a natural pipeline structure where each stage depends on the previous one.

Real-world example -- OpenHands autonomous PR workflow: OpenHands (github.com/All-Hands-AI/OpenHands) implements sequential compound loops in its Software Agent SDK. When an @openhands mention triggers on a GitHub issue, the agent runs a discovery loop (reads issue, scans repo), a planning loop (generates approach), a coding loop (edits files iteratively), and a verification loop (runs tests, checks lint). Each loop feeds its output into the next. The SDK expresses this as composable workflow YAML with explicit sequential and branching steps.

Pattern 2: Nested Compound Loop

An outer loop iterates over a collection, and an inner loop handles each item:

For each file in codebase:           <-- Outer Loop
  For each issue in file:           <-- Inner Loop
    Fix issue
    Run test
    Verify
  Run integration test              <-- Verification Loop (after inner)

Use when: Processing a collection of items where each item requires its own iteration.

Real-world example -- Aider batch refactoring: Aider (github.com/paul-gauthier/aider, 30K+ stars) uses a Git-first approach where each edit becomes a commit. For a batch migration of 50 files from JavaScript to TypeScript, you can script Aider in a nested compound pattern:

# Outer loop: iterate over files
for file in $(find src -name "*.js"); do
  # Inner loop: convert and fix type errors
  aider --model claude-3.5-sonnet \
    "Convert $file to TypeScript. Fix all type errors. Run tests."
done
# Verification loop: full build check
npm run build && npm run test

Each inner invocation runs Aider's own internal loop (edit, commit, test, retry) until the file passes, while the outer shell loop advances to the next file. The verification loop at the end catches cross-file regressions.

Real-world example -- SWE-Agent: SWE-Agent (github.com/princeton-nlp/SWE-Agent, 15K+ stars) implements nested loops in research-oriented software engineering. Its outer loop iterates over GitHub issues, while the inner loop (the agent's action-observation cycle) works on resolving each issue independently before moving to the next.

Pattern 3: Parallel Compound Loop

Multiple independent loops run simultaneously, and a coordination loop merges their results:

+----------+                +----------+
| Fix Loop |                | Fix Loop |
| (files   |                | (files   |
|  1-25)   |                |  26-50)  |
+----+-----+                +-----+----+
     |                            |
     +------------+  +-------------+
                  |  |
             +----v--v-----+
             | Coordination | <-- Merge results, resolve conflicts
             | Loop         |
             +--------------+

Use when: Tasks can be parallelized across independent subsets of work.

Real-world example -- CrewAI parallel process: CrewAI's Parallel process type runs multiple agent loops concurrently. Each agent has its own task loop (plan, act, observe, iterate), and the crew-level process coordinates shared state and merged results:

from crewai import Crew, Process, Task, Agent

research_agent = Agent(role="Researcher", goal="Find issues in module A")
coding_agent = Agent(role="Coder", goal="Fix issues in module B")

# Each agent runs its own loop -- parallel compound
crew = Crew(
    agents=[research_agent, coding_agent],
    tasks=[research_task, coding_task],
    process=Process.parallel  # Runs loops concurrently
)

Pattern 4: Adaptive Compound Loop

A loop that dynamically adjusts its behavior based on outcomes:

Start with Strategy A
  If convergence rate > threshold --> Continue Strategy A
  If convergence rate < threshold --> Switch to Strategy B
  If stuck for N iterations --> Escalate to human

Use when: The best approach isn't known in advance and the system needs to adapt.

Real-world example -- Windsurf Cascade agent: Windsurf (windsurf.ai) implements its Cascade agent with multi-step execution and adaptive behavior. The agent evaluates intermediate results and adjusts its approach -- if a code change introduces new errors, it backtracks and tries an alternative strategy rather than blindly retrying the same approach.

Pattern 5: Recursive Compound Loop

A loop that spawns sub-loops for sub-problems:

Main Loop: Refactor the authentication system
  |-- Sub-loop: Migrate session management
  |    |-- Sub-sub-loop: Update middleware
  |    +-- Sub-sub-loop: Update tests
  |-- Sub-loop: Migrate password handling
  |    |-- Sub-sub-loop: Update hashing
  |    +-- Sub-sub-loop: Update validation
  +-- Integration loop: Verify all changes work together

Use when: A complex task decomposes into sub-tasks that each require their own iteration.

Real-world example -- MetaGPT's recursive SOP: MetaGPT implements recursive compound loops through its Standard Operating Procedure. The top-level SOP loop decomposes a requirement into roles (Product Manager, Architect, Engineer, QA). Each role then runs its own sub-loop: the Product Manager generates user stories iteratively, the Architect designs data structures iteratively, and the Engineer writes code iteratively. The outer coordination loop verifies coherence across all sub-loop outputs. This recursive decomposition is what makes MetaGPT's output more coherent than simple chat-based multi-agent systems, as demonstrated in the ICLR 2024 paper.

Real-world example -- LangGraph recursive agents: LangGraph (github.com/langchain-ai/langgraph) supports recursive agent orchestration where a supervisor agent spawns sub-agents for sub-problems, each running their own loop. The graph structure naturally expresses recursive compound loops -- nodes are loop iterations, edges carry state, and conditional edges implement the adaptive logic that decides whether to recurse deeper or converge.

Designing Compound Loops

Step 1: Decompose the Task

Break the overall goal into independent sub-goals:

Goal: "Modernize the API layer"
|-- Sub-goal 1: Add type safety (TypeScript migration)
|-- Sub-goal 2: Add error handling middleware
|-- Sub-goal 3: Add rate limiting
|-- Sub-goal 4: Update documentation
+-- Sub-goal 5: Verify all changes together

Step 2: Identify Dependencies

Map which sub-goals depend on others:

Sub-goal 1 (types) <-- No dependencies
Sub-goal 2 (errors) <-- Depends on Sub-goal 1
Sub-goal 3 (rate limiting) <-- Independent
Sub-goal 4 (docs) <-- Depends on all others
Sub-goal 5 (verify) <-- Depends on all others

Step 3: Choose the Composition Pattern

Based on dependencies:

  • Sub-goals 1 and 3 can run in parallel
  • Sub-goal 2 depends on 1, so it runs sequentially after 1
  • Sub-goals 4 and 5 run sequentially after all others

For implementation, use CrewAI if you want a multi-agent framework with explicit process control. Use LangGraph if you need fine-grained conditional branching and recursive agent spawning. Use the OpenHands SDK if you want composable YAML-based workflows for GitHub-integrated tasks.

Step 4: Define Cross-Loop State

What information needs to flow between loops?

{
  "shared_state": {
    "files_modified": [],
    "new_types_defined": [],
    "middleware_chain": [],
    "test_results": {},
    "token_budget_used": 0,
    "iteration_counts": {
      "discovery": 0,
      "fix": 0,
      "verify": 0
    }
  }
}

Cross-loop state management is a known challenge in multi-agent frameworks. The CrewAI community discussion (github.com/crewAIInc/crewAI/discussions/4111) documents common pitfalls: when parallel agent loops share state without proper synchronization, results can overwrite each other or create inconsistent intermediate states.

Step 5: Set Global Boundaries

Compound loops need boundaries at multiple levels. This is especially critical given that data waste reports show traditional prompt stuffing sends 20,000+ tokens but 95% is irrelevant, and without compound loop budgets, costs escalate rapidly.

LevelBoundaryExample
Inner loopMax iterations per item5 attempts per file
Outer loopMax items to process50 files maximum
GlobalMax total time30 minutes
GlobalMax total tokens500,000
GlobalMax total cost$50 USD

Risks and Mitigations

RiskMitigation
Cascade failure: One loop's failure breaks subsequent loopsAdd verification gates between loops -- each loop should produce validated output before the next loop consumes it
State explosion: Cross-loop state grows unboundedUse structured state with size limits; compress context using the 7 principles from InfoQ (compression, replacement, retention, anchoring, merging, sharing, dynamic context)
Deadlock: Loops wait on each otherDesign clear dependency graphs with no circular dependencies; CrewAI's sequential process avoids this by definition
Resource exhaustion: Parallel loops consume too muchSet global resource budgets; Cursor 2.0's branch-per-agent model isolates parallel work to prevent interference
Infinite loops: A loop gets stuck repeating the same actionImplement adaptive strategy switching (Pattern 4); add iteration counters with hard limits
Debugging difficulty: Hard to trace issues across loopsComprehensive logging at each loop level with unique run IDs per compound loop execution

Key Takeaways

  1. Compound loops compose multiple loop patterns to handle multi-dimensional tasks that exceed any single loop's capability
  2. Choose the right composition based on task dependencies -- sequential for pipelines, parallel for independent work, nested for collection processing, adaptive for uncertain approaches, recursive for hierarchical decomposition
  3. Real frameworks implement these patterns today: MetaGPT uses recursive SOP loops, CrewAI provides sequential/parallel/hierarchical processes, OpenHands SDK composes YAML-based workflows, and Cursor 2.0 runs up to 8 parallel agent loops
  4. Decompose before composing -- break the goal into sub-goals and map dependencies before choosing loop composition
  5. Manage state across loops with shared, structured state and explicit synchronization points
  6. Set boundaries at multiple levels -- inner loops, outer loops, and global limits on iterations, time, tokens, and cost
  7. Add verification gates between loops to prevent cascade failures -- the "four major failure scenarios" documented in production systems show why this matters
  8. Implement adaptive strategies to prevent infinite loops -- when a loop stalls, switch approach or escalate rather than retry blindly