Redeo Docs
DocsLADR / How to Add Recursion

Adding Features

How to Add Recursion

How to Add Recursion

Recursion lets a step spawn a child execution of the same ladder. The child runs the full steps array with the step's output as input. When the child completes, its exit output replaces the step's output. This guide shows how to add recursion for problem decomposition.

Declaring recursion

Add a recursion: block to any step. The maxDepth field controls how deep the recursion can go.

yaml
- id: decompose
  fields:
    - { name: Problem, type: text, from: input.context }
  systemPrompt: "Break the problem into sub-problems. Solve each one."
  recursion:
    maxDepth: 3

After decompose runs and produces output, the runtime spawns a child instance. The child receives the step's output as input.context and runs the full ladder from step 1. When the child's exit step completes, its output replaces decompose's output.

Depth tracking: The child's execution has Depth: 1. If the child also has a recursive step, it spawns a grandchild at Depth: 2. When Depth reaches maxDepth, the recursion stops — the step runs normally without spawning a child.

Knob-controlled depth:

yaml
recursion:
  maxDepth: "{{knobs.depth}}"

This lets the caller control recursion depth at call time.

The decomposition pattern

Recursion is most useful for problems that decompose into smaller versions of themselves. The ladder handles the full problem; recursive children handle sub-problems.

yaml
name: Recursive Solver
allowedTargets: { strategy: universal }
exit: answer
knobs:
  depth:
    name: Depth
    type: recursion
    input: numerical
    default: 3
    min: 1
    max: 5
steps:
  - id: decompose
    fields:
      - { name: Problem, type: text, from: input.context }
    systemPrompt: |
      Break the problem into sub-problems. For each sub-problem, attempt to solve it.
      If a sub-problem is too complex, output it for recursive decomposition.
    recursion:
      maxDepth: "{{knobs.depth}}"

  - id: answer
    fields:
      - { name: Solution, type: ingest, from: { stepId: decompose, loopRef: current } }
    systemPrompt: "Format the solution for the caller."

How it executes:

  1. Parent receives the full problem.
  2. decompose runs: LLM breaks it into sub-problems and partially solves them.
  3. Runtime spawns a child with decompose's output as input.
  4. Child runs the same ladder: its decompose step handles the sub-problems.
  5. Child's exit output replaces parent's decompose output.
  6. answer formats the final result.

Each level of recursion costs one against the recursion depth ceiling (default: 5, max: 20). The hop ceiling also bounds total recursive calls.

Budget implications

Recursion multiplies cost. Each child is a full ladder execution — it runs all steps, makes all LLM calls, and consumes budget.

A ladder with 3 steps and maxDepth: 3 can spawn up to 3^3 = 27 child executions in the worst case (each level spawns 3 children). The spend ceiling is the safety net: no matter how deep the recursion goes, cumulative cost is capped.

Practical limits:

maxDepthWorst case childrenTypical use
1-21-4Simple decomposition
39-27Moderate decomposition
4-516-125Deep decomposition (watch the spend ceiling)

Always declare an executionBudget when using recursion to cap the worst case:

yaml
executionBudget:
  maxSpend: 10.0       # $10 cap
  maxRecursion: 4      # cap depth at 4 even if knob says 5

The platform max for recursion is 20, but practical ladders rarely exceed 5. The spend ceiling will usually kick in before the depth ceiling does.