Skip to content

Plan-first and delegating algorithms

Three ways to spend the model's attention on planning and then run the plan with as little model involvement as possible, plus a review option for PlanAndExecute. As for every algorithm, state is a fold of the run's log, parallel completions cannot change the result, a recorded run replays to the same outcome, and each algorithm tells you its most model calls (max_calls) up front.

"""Plan first, then run: ReWOO, LLMCompiler, a manager with specialist workers, and PlanAndExecute with a progress review. Scripted model, offline."""

import asyncio
import json

from aaa.algos import HierarchicalAgent, LLMCompiler, PlanAndExecute, ReWOO, ToolLoop
from aaa.llm import FakeLLMClient, LLMHandler
from aaa.llm.fake import text
from aaa.runtime import Engine
from aaa.tools import ToolHandler, ToolRegistry, tool


def system_of(request) -> str:
    return next((m.content or "" for m in request.messages if m.role == "system"), "")


def user_of(request) -> str:
    return next((m.content or "" for m in reversed(request.messages) if m.role == "user"), "")


@tool
def lookup(query: str) -> str:
    """Look a fact up."""
    return {"capital of France": "Paris", "population of Paris": "2.1 million"}.get(
        query, "unknown"
    )


@tool
def add(a: int, b: int) -> int:
    """Add two numbers."""
    return a + b


REGISTRY = ToolRegistry([lookup, add])
PLAN = {
    "steps": [
        {"id": "E1", "tool": "lookup", "arguments": {"query": "capital of France"}},
        {"id": "E2", "tool": "lookup", "arguments": {"query": "population of #E1"}},
        {"id": "E3", "tool": "add", "arguments": {"a": 2, "b": 2}},
    ]
}


async def run(algorithm, model, task, registry=REGISTRY):
    async with Engine() as engine:
        LLMHandler(FakeLLMClient(model)).attach(engine)
        ToolHandler(registry).attach(engine)
        return await engine.run(algorithm, {"task": task})


async def rewoo() -> None:
    def model(request):
        if "Plan the tool calls" in system_of(request):
            return text(json.dumps(PLAN))
        return text("Paris has 2.1 million people.")

    out = await run(ReWOO(REGISTRY), model, "How many people live in the capital of France?")
    print("rewoo:", out.value["text"], "| evidence", out.value["evidence"], "| model calls: 2")


async def llm_compiler() -> None:
    answers = iter(
        [
            {"action": "replan", "feedback": "also add the numbers"},
            {"action": "finish", "answer": "Paris has 2.1 million people; 2 + 2 = 4."},
        ]
    )
    plans = iter([{"steps": PLAN["steps"][:2]}, {"steps": [{**PLAN["steps"][2], "id": "E3"}]}])

    def model(request):
        if "Plan the tool calls" in system_of(request):
            return text(json.dumps(next(plans)))
        return text(json.dumps(next(answers)))

    out = await run(LLMCompiler(REGISTRY), model, "Population of the capital, and 2 + 2?")
    print("llm-compiler:", out.value["text"], "| rounds", out.value["rounds"])


async def manager() -> None:
    def model(request):
        s = system_of(request)
        if "Split the task" in s:
            goals = [
                {"id": "research", "worker": "researcher", "goal": "find the facts"},
                {
                    "id": "write",
                    "worker": "writer",
                    "goal": "write them up",
                    "depends_on": ["research"],
                },
            ]
            return text(json.dumps({"subgoals": goals}))
        if "Combine the sub-goal results" in s:
            return text("Report: Paris is the capital of France.")
        if "Your sub-goal (research)" in user_of(request):
            return text("Paris is the capital of France.")
        return text("A short note on Paris.")

    empty = ToolRegistry([])
    team = HierarchicalAgent(
        {
            "researcher": ToolLoop(empty, name="researcher"),
            "writer": ToolLoop(empty, name="writer"),
        },
        descriptions={"researcher": "finds facts", "writer": "writes prose"},
    )
    out = await run(team, model, "Write about France.", empty)
    print("manager:", out.value["text"], "| sub-goals", [g["id"] for g in out.value["subgoals"]])


async def review() -> None:
    def model(request):
        s = system_of(request)
        if "planner" in s and "Steps still planned" in user_of(request):
            step = {
                "id": "check",
                "description": "verify the draft against the facts",
                "depends_on": ["gather"],
            }
            return text(json.dumps({"steps": [step]}))
        if "planner" in s:
            steps = [
                {"id": "gather", "description": "gather facts", "depends_on": []},
                {"id": "write", "description": "write the answer", "depends_on": ["gather"]},
            ]
            return text(json.dumps({"steps": steps}))
        if "Combine the step results" in s:
            return text("done")
        return text("ok")

    empty = ToolRegistry([])
    algorithm = PlanAndExecute(ToolLoop(empty), review_progress=True, max_parallel=1)
    out = await run(algorithm, model, "Answer.", empty)
    print("plan-and-execute with review:", sorted(out.value["results"]))


async def main() -> None:
    for demo in (rewoo, llm_compiler, manager, review):
        await demo()


asyncio.run(main())
rewoo: Paris has 2.1 million people. | evidence {'E1': 'Paris', 'E2': '2.1 million', 'E3': '4'} | model calls: 2
llm-compiler: Paris has 2.1 million people; 2 + 2 = 4. | rounds 2
manager: Report: Paris is the capital of France. | sub-goals ['research', 'write']
plan-and-execute with review: ['gather', 'r1-check']

ReWOO and LLMCompiler: a checked plan of tool calls

ReWOO(registry, tool_names=None, max_steps=8, on_step_error="fail") asks the model once for every tool call up front:

{"steps": [
  {"id": "E1", "tool": "lookup", "arguments": {"query": "capital of France"}},
  {"id": "E2", "tool": "lookup", "arguments": {"query": "population of #E1"}}
]}

#E1 in an argument means "the result of step E1". Steps whose inputs are ready run in parallel through the normal tool pipeline (approval, timeouts, guards, budgets all apply), and one solver call writes the answer from the evidence. Two model calls however many tools run, against ReAct's one per step. The price is that the plan cannot react to a surprising result.

LLMCompiler(registry, ..., max_replans=1) runs the same plan, then a joiner call decides: answer, or replan with feedback. A replan asks for additional steps (numbered after the last one, free to use earlier results), at most max_replans times; costs 2 * (max_replans + 1) calls at most. If it still wants to replan when the limit is reached, its answer is used when it gave one, else the run ends NO_SOLUTION with the evidence.

What is checked before any tool runs, because a plan is model output:

  • every tool name must be in tool_names (default: the registry); a plan naming anything else is refused, and nothing has run;
  • step ids are E1, E2, ... and unique; a step may refer only to earlier steps, so cycles cannot exist;
  • arguments must be a JSON object under 20 000 characters; the number of steps is capped by max_steps;
  • #E1 substitution is plain string work (a whole-string #E1 becomes the raw result, a mention inside text becomes the result as text cut at max_evidence_chars); nothing is evaluated.

A tool result that is substituted into a later call's arguments is data in that argument, and the solver and joiner see results only inside a quoted <evidence> block. A failed step ends the run (on_step_error="fail", with a fixed reason) or is recorded as (step failed) and the plan goes on ("continue").

Result {"text", "plan": [{"id", "tool", "arguments"}], "evidence": {id: text}, "rounds"}.

HierarchicalAgent: a manager with specialist workers

team = HierarchicalAgent(
    {"researcher": ToolLoop(tools, name="researcher"), "writer": ToolLoop(no_tools, name="writer")},
    descriptions={"researcher": "finds facts", "writer": "writes prose"},
    max_subgoals=6, max_parallel=3, max_depth=3,
    child_budget=Budget(max_steps=50, deadline_s=300),
)

The manager writes sub-goals {id, worker, goal, depends_on}. Ids, worker names and the dependency graph are validated before any child starts. Ready sub-goals run as child runs, at most max_parallel at a time, each under child_budget (so one worker cannot spend the others' budget) and each seeing only its own goal and the results of the sub-goals it depends on; then one call combines the results (synthesize=False returns the last sub-goal's result instead). Cancelling the parent cancels the children.

  • Nesting. A worker can itself be a HierarchicalAgent (give each a distinct name=). Every child is told its depth, and a manager at max_depth refuses to delegate (the run ends FAILED), so a model cannot recurse without limit.
  • Failure. A failed child fails the run (on_child_failure="fail") or is passed on as (this sub-goal failed) ("continue").
  • Compared with PlanAndExecute, which uses one executor for every step, and Supervisor, where the model delegates turn by turn through tool calls, the manager here commits to a validated DAG and picks a specialist per sub-goal.

Result {"text", "subgoals": [{"id", "worker", "goal", "result"}], "depth"}.

PlanAndExecute(review_progress=True)

By default PlanAndExecute replans only when a step fails. With review_progress=True the planner is also asked, after each finished step that leaves steps pending, whether the remaining steps still hold; it returns them unchanged or corrected, and the pending steps are replaced (finished and running steps are kept; new ids are prefixed r1-, r2-, ...). Each review counts against max_replans, so cost stays bounded. A review that fails, is not valid JSON, or is not a valid plan is ignored and the plan carries on (aaa.plan.kept is logged); only an invalid plan after a failure ends the run.

Choosing

You want Use
The fewest model calls, tools known up front ReWOO
The same, but able to correct course once or twice LLMCompiler
Different specialists for different parts of the work HierarchicalAgent
One executor for every step, plan may drift PlanAndExecute (with review_progress if it may)
The model should decide each next step from what it just saw ToolLoop