Skip to content
JavaAgentic

Type at least two characters. Try “RAG”, “pgvector” or “tool calling”.

Planning & Reasoning in Agents

How agents plan and reason: task decomposition, hierarchical planning, chain-of-thought and tree-of-thought — with Java examples and honest guidance on when planning helps.

Advanced4 min readUpdated
On this page

Planning and reasoning are what separate an agent that stumbles through a task from one that solves it deliberately. This tutorial covers the techniques — decomposition, hierarchical planning, chain-of-thought, tree-of-thought — and, as always, is honest about their cost and when they earn it.

Key Takeaways

  • Decomposition turns a sprawling goal into focused sub-tasks a model handles more reliably.
  • Chain-of-thought improves multi-step reasoning; skip it for simple lookups.
  • Hierarchical planning combines a high-level plan with detailed sub-plans.
  • Tree-of-thought explores branches — powerful but expensive, for hard problems only.

Task decomposition

A model handles several focused steps more reliably than one sprawling request. Decomposition also gives you an inspectable plan:

Decompose then solve
record SubTask(String description, List<String> dependsOn) {}
 
public Result solve(String goal) {
    // The model breaks the goal into sub-tasks with dependencies.
    List<SubTask> plan = planner.decompose(goal);   // structured output
 
    // Execute in dependency order, feeding results forward.
    Map<String, String> results = new LinkedHashMap<>();
    for (SubTask task : topologicalOrder(plan)) {
        results.put(task.description(), executor.execute(task, results));
    }
    return synthesise(goal, results);
}

Chain-of-thought

Prompting the model to reason through steps before answering improves accuracy on multi-step problems:

String prompt = """
        Work through this step by step, showing your reasoning, then give the
        final answer on a line starting with "ANSWER:".
 
        Problem: %s
        """.formatted(problem);
 
String response = model.chat(prompt);
String answer = extractAfter(response, "ANSWER:");

The reasoning is not for the user — you extract just the answer. The intermediate steps are what improve the result.

Hierarchical planning

For large tasks, plan at a high level, then plan each high-level step in detail. This mirrors how people tackle big projects:

Hierarchical planning: a high-level plan whose steps each expand into detailed sub-plans.

It keeps each planning call focused — the high-level planner reasons about the shape, the detailed planners about mechanics — and it lets you re-plan a single step without redoing the whole plan.

Tree-of-thought

When a single line of reasoning often fails, explore several branches and evaluate them:

Exploring reasoning branches
public String solveHard(String problem) {
    // Generate several candidate approaches.
    List<String> branches = generator.propose(problem, 3);
 
    // Evaluate each, pursue the most promising, prune the rest.
    return branches.stream()
            .map(branch -> new Scored(branch, evaluator.score(branch, problem)))
            .max(Comparator.comparingDouble(Scored::score))
            .map(best -> executor.pursue(best.branch(), problem))
            .orElseThrow();
}

Reflection as reasoning

Reflection — critiquing and revising output — is a form of reasoning that catches errors after the fact:

String draft = solver.solve(problem);
Critique critique = critic.evaluate(draft, problem);
if (!critique.isSound()) {
    draft = solver.revise(draft, critique);
}

It pairs well with planning: plan, execute, then reflect on whether the result actually met the goal. See agent architecture patterns.

Choosing a technique

TaskTechnique
Simple lookup or single stepNone — direct call
Multi-step reasoningChain-of-thought
Complex goal, knowable shapeDecomposition + planning
Large taskHierarchical planning
Hard problem, direct approach fails oftenTree-of-thought
Quality-critical outputAdd reflection

The through-line: match the reasoning cost to the task difficulty. Most tasks need far less than the elaborate techniques suggest, and over-applying them wastes money and adds latency for no gain.

Next

Frequently Asked Questions

What is task decomposition in AI agents?
Breaking a complex goal into smaller, tractable sub-tasks that can be solved individually and combined. It helps because models handle a series of focused steps more reliably than one sprawling request, and because a decomposed plan is inspectable — you can see and check the intended approach before executing it.
Does chain-of-thought actually improve agent reasoning?
Yes, for multi-step problems. Prompting the model to reason through intermediate steps before answering improves accuracy on tasks that need several logical hops, at the cost of extra tokens and latency. For simple lookups it adds cost without benefit, so apply it where the task genuinely requires reasoning.
What is tree-of-thought?
An extension of chain-of-thought that explores several reasoning branches and evaluates them, rather than committing to one line of thought. It can solve problems where the first approach often fails, but it multiplies model calls significantly, so it is reserved for hard problems where the extra cost is justified.
Should the agent plan first or reason step by step?
It depends on the task shape. Plan first when the overall approach is knowable up front and you want an inspectable plan. Reason step by step (ReAct) when the path depends on what each step reveals. Many robust agents combine them: a high-level plan with step-by-step reasoning within each step.

Related tutorials