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.
- id: decompose
fields:
- { name: Problem, type: text, from: input.context }
systemPrompt: "Break the problem into sub-problems. Solve each one."
recursion:
maxDepth: 3After 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:
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.
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:
- Parent receives the full problem.
decomposeruns: LLM breaks it into sub-problems and partially solves them.- Runtime spawns a child with
decompose's output as input. - Child runs the same ladder: its
decomposestep handles the sub-problems. - Child's exit output replaces parent's
decomposeoutput. answerformats 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:
| maxDepth | Worst case children | Typical use |
|---|---|---|
| 1-2 | 1-4 | Simple decomposition |
| 3 | 9-27 | Moderate decomposition |
| 4-5 | 16-125 | Deep decomposition (watch the spend ceiling) |
Always declare an executionBudget when using recursion to cap the worst case:
executionBudget:
maxSpend: 10.0 # $10 cap
maxRecursion: 4 # cap depth at 4 even if knob says 5The platform max for recursion is 20, but practical ladders rarely exceed 5. The spend ceiling will usually kick in before the depth ceiling does.