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.
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:
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:
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:
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
| Task | Technique |
|---|---|
| Simple lookup or single step | None — direct call |
| Multi-step reasoning | Chain-of-thought |
| Complex goal, knowable shape | Decomposition + planning |
| Large task | Hierarchical planning |
| Hard problem, direct approach fails often | Tree-of-thought |
| Quality-critical output | Add 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?
Does chain-of-thought actually improve agent reasoning?
What is tree-of-thought?
Should the agent plan first or reason step by step?
Related tutorials
- Tool Use & Function CallingHow agents use tools well: designing tool schemas, dynamic tool selection, composing tools into workflows, error recovery, and keeping the tool set small enough to choose from.
- Memory Systems for AgentsHow agent memory works beyond a chat window: working, episodic and semantic memory, vector-based recall, memory consolidation, and implementing persistent agent memory in Java.
- Agent Architecture PatternsThe core agent architecture patterns explained with Java: ReAct, plan-and-execute, reflection, orchestrator-worker and routing — when to use each, and why simpler is usually better.
- Multi-Agent Systems (MAS)Building multi-agent systems in Java: orchestrator-worker coordination, agent handoffs, communication protocols and conflict resolution — and the honest case for when one agent is better.