6.2.4AI Agents & Tool Use

Planning and task decomposition

2,375 words11 min readdifficulty · medium

What Is Task Decomposition?

WHY do we need it?

  1. Complexity management: A single LM call has a fixed context window and limited effective reasoning depth per forward pass—while chain-of-thought prompting lets a model reason over many steps, an agent that must call external tools (search, code, APIs) cannot fit all those interactions into one pass. Decomposition spreads the work across multiple grounded steps.
  2. Failure isolation: If subtask 3 fails, you don't restart from scratch—just retry that branch.
  3. Paralelization: Independent subtasks can run concurrently (e.g., fetch from 3 APIs simultaneously).
  4. Interpretability: The plan is human-readable; you see why the agent took each step.

HOW does it work? The agent uses one of three strategies:

  • Hierarchical planning: Top-down recursion (goal → subgoals → primitive actions).
  • Forward chaining: Start from current state, apply actions until goal is reached.
  • Backward chaining: Start from goal, work backward to find required preconditions.

Derivation: Why Decomposition Reduces Search Space

Problem: Given a goal GG and nn possible actions at each step, naive search over depth dd has complexity O(nd)O(n^d).

Insight: If we decompose GG into kk independent subgoals {G1,G2,,Gk}\{G_1, G_2, \ldots, G_k\}, each requiring depth d/kd/k, the complexity becomes:

O(knd/k)O(k \cdot n^{d/k})

Derivation:

  1. Without decomposition: Explore all ndn^d paths to depth dd.
  2. With decomposition:
    • Solve G1G_1 in nd/kn^{d/k} paths.
    • Solve G2G_2 independently in nd/kn^{d/k} paths.
    • Total: knd/kk \cdot n^{d/k} paths.
  3. Why is this better? For n=10,d=6,k=3n=10, d=6, k=3:
    • Naive: 106=1,000,00010^6 = 1{,}000{,}000 paths.
    • Decomposed: 3102=3003 \cdot 10^2 = 300 paths.

The exponential becomes linear in kk when subgoals are independent.

Planning Strategies in Detail

1. Hierarchical Task Network (HTN) Planning

HTN planning decomposes tasks using a hierarchy of methods: each complex task has multiple refinement rules.

Algorithm:

function HTN-Plan(task, state):
    if task is primitive:
        return [action(task)]
    else:
        for each method m that decomposes task:
            subtasks = m.decompose(task, state)
            subplans = [HTN-Plan(st, state) for st in subtasks]
            if all subplans succeed:
                return concatenate(subplans)
        return FAIL

Example: Goal: "Prepare coffee"

  • Method 1: [Boil water, Grind beans, Brew]
  • Method 2 (if no grinder): [Boil water, Use instant coffee]

The agent checks state.has_grinder and picks a method.

Why this step? Checking state ensures the plan is feasible given current resources.

Decomposition (forward chaining):

  1. Search for earthquake data API → finds USGS API.
  2. Fetch data for 2023 → HTTP GET request.
  3. Parse JSON → extract magnitude field.
  4. Plot histogram → call matplotlib.
  5. Return image to user.

Each step's output feeds the next. If step 2 fails (API down), retry or switch to CSV fallback.

2. ReAct: Reasoning + Acting

ReAct interleaves thought (reasoning about what to do) and action (tool use).

Pattern:

Thought: I need to find the current population of Tokyo.
Action: search("Greater Tokyo Area population 2026")
Observation: ~37 million (Greater Tokyo Area, world's largest metro)
Thought: Now compare to the New York metro area.
Action: search("New York metropolitan area population 2026")
Observation: ~19.5 million
Thought: Tokyo is larger by ~17.5 million.
Answer: The Greater Tokyo Area (~37M) is roughly 90% larger than the New York metro area (~19.5M).

Why "Thought"? It makes the next action grounded in the current subgoal, not random tool spam. Note on units: always specify which boundary you mean—city proper (Tokyo ~14M) vs. metro area (Greater Tokyo ~37M)—or the comparison is meaningless.

Plan (backward chaining):

  1. Need: Grammy winner in year YY.
  2. To get YY: release year of 1989.
  3. Action 1: search("1989 Taylor Swift release year") → 2014.
  4. Action 2: search("Grammy Album of the Year 2015")Morning Phase by Beck (awarded in 2015 for 2014 albums).
  5. Answer: Beck.

Why this order? We can't ask for the Grammy winner until we know the year.

3. LM-Modulo Framework

Combines LLM's creativity with external verifiers (solvers, simulators).

Loop:

  1. LM proposes a plan PP.
  2. Verifier checks PP (e.g., run code, check constraints).
  3. If valid, execute PP. Else, LM refines based on error feedback.

Example: Planning robot actions in a kitchen.

  • LLM: "Pick up mug, pour coffee, place mug on table."
  • Verifier (physics sim): "Collision! Mug is inside table."
  • LLM (revision): "Pick up mug, move to position above table, lower mug, release."

Why this step? The verifier catches infeasible plans before real execution (safety-critical for robots).

Common Mistakes

Example:

  • Bad: "Add 2+2" → [Parse input, Identify operation, Execute addition, Format output]
  • Good: Just call calculate(2+2).

Example:

  • Goal: "Summarize paper X and compare to paper Y."
  • Subtasks:
    • S1S_1: Fetch paper X.
    • S2S_2: Fetch paper Y.
    • S3S_3: Summarize X (depends on S1S_1).
    • S4S_4: Summarize Y (depends on S2S_2).
    • S5S_5: Compare summaries (depends on S3,S4S_3, S_4).
  • Correct order: Run S1S2S_1 \parallel S_2, then S3S4S_3 \parallel S_4, then S5S_5.

Example:

  • Plan: [Search Wikipedia, Extract info, Cite source]
  • Failure: Wikipedia returns "Page not found."
  • Replan: [Search Google Scholar, Download PDF, Extract info, Cite source]

Active Recall Flashcards

#flashcards/ai-ml

What is task decomposition in AI agents? :: Breaking a complex goal into a DAG of simpler, executable subtasks with clear dependencies.

Why does decomposition reduce search complexity?
It changes O(nd)O(n^d) to O(knd/k)O(k \cdot n^{d/k}) by solving kk independent subproblems of depth d/kd/k instead of one problem of depth dd.
What is hierarchical task network (HTN) planning?
A planning strategy that uses a hierarchy of methods to recursively decompose complex tasks into primitive actions based on the current state.
What is the ReAct pattern?
Interleaving Thought (reasoning about what to do next) and Action (executing tools) to ground each step in the current subgoal.
What is the LLM-Modulo framework?
A loop where an LLM proposes a plan, an external verifier checks it, and the LM refines based on feedback until the plan is valid.
What is adaptive decomposition?
Only breaking a task into subtasks when necessary (task is ambiguous or first attempt fails), avoiding overhead for simple tasks.
Why must we build a dependency graph for subtasks?
To identify which subtasks depend on others' outputs and prevent race conditions or errors from parallel execution of dependent tasks.
What is replanning in agent systems?
Regenerating or modifying the plan when a subtask fails, enabling the agent to try alternative methods instead of giving up.
What is forward chaining in planning?
Starting from the current state and applying actions until the goal is reached.
What is backward chaining in planning?
Starting from the goal and working backward to find required preconditions and actions.
Recall Explain to a 12-Year-Old

Imagine you want to build a huge LEGO castle, but you've never done it before. If you just start randomly sticking bricks together, you'll get stuck or make a mess. Instead, you make a plan:

  1. Break it down: "I'll build the base first, then the walls, then the towers, then the roof."
  2. Order matters: You can't put the roof on before the walls exist! So you do them in sequence.
  3. Check as you go: After building the base, you check—does it look right? Is it strong enough? If not, fix it before moving on.
  4. If something breaks: Say a tower falls over. You don't throw away the whole castle! You just rebuild that tower.

That's exactly what AI agents do with big tasks. They split them into steps, do them in order, check their work, and fix mistakes. Without a plan, the AI would just guess randomly and probably fail. With a plan, it's like having LEGO instructions—it knows exactly what to do next.

Connections

  • 6.2.01-Tool-augmented-LMs: Task decomposition determines when and which tools to call.
  • 6.2.03-Multi-agent-systems: Each agent can have its own planner; coordination requires planning at the team level.
  • 5.3.02-Chain-of-thought-prompting: CoT is single-call multi-step reasoning; agent planning adds external actions interleaved with reasoning.
  • 6.2.05-Memory-in-agents: Long-term memory stores past plans and outcomes to improve future decomposition.
  • 8.1.03-Reasoning-under-uncertainty: Plans must account for uncertain outcomes (use probability over actions).
  • Classical AI: HTN planning from STRIPS (1971), means-ends analysis from Newell & Simon (1961).

Key Takeaway: Planning transforms an intractable search problem into a structured, verifiable process. The agent's intelligence lies not just in acting, but in deciding what to do next and recognizing when to revise.

Concept Map

broken into

produces

motivated by

includes

includes

includes

implemented via

top-down

state to goal

goal to preconditions

formalized as

reduces search from

down to

Complex Goal

Task Decomposition

DAG of Subtasks

Why Decompose

Complexity Management

Failure Isolation

Parallelization

Planning Strategies

Hierarchical Planning

Forward Chaining

Backward Chaining

HTN Planning

O of n to the d

O of k times n to d over k

Hinglish (regional understanding)

Intuition Hinglish mein samjho

Chalo, ise ek simple tarike se samajhte hain. Task decomposition ka core idea yeh hai ki AI agent ko jab koi bada aur complex goal diya jaye — jaise "research question ka answer do" — toh woh usko chhote-chhote executable steps mein tod deta hai, jaise "papers search karo → key claims nikalo → findings ko jodo". Bilkul waise jaise ek lambi road trip ko aap gas stops, hotel booking aur route checkpoints mein baant dete ho. Agar aap decomposition nahi karoge, toh agent ya toh confuse hoke atak jaayega (bahut complex hai!) ya phir randomly sab kuch try karta rahega. Isliye directed acyclic graph (DAG) banana zaroori hai, jahan har subtask ke clear inputs, outputs aur dependencies ho.

Ab yeh matter kyun karta hai? Sabse bada reason hai search space ka kam ho jaana. Socho, agar har step pe nn possible actions hain aur depth dd hai, toh naive search ki complexity O(nd)O(n^d) hoti hai — jo exponentially badh jaati hai. Lekin agar goal ko kk independent subgoals mein tod do, toh complexity O(knd/k)O(k \cdot n^{d/k}) ho jaati hai. Example dekho: n=10,d=6n=10, d=6 par naive search mein 10 lakh paths lagte, par decomposition ke saath sirf 300 paths! Yeh exponential se linear ka jaadu hai. Iske alawa failure isolation milta hai (agar subtask 3 fail ho toh sirf usko retry karo, poora restart nahi), independent tasks ko parallel chala sakte ho, aur plan human-readable hota hai toh aap samajh sakte ho ki agent ne har step kyun liya.

Practical mein teen strategies use hoti hain: Hierarchical planning (top-down, goal ko subgoals aur phir primitive actions mein todna), Forward chaining (current state se shuru karke goal tak pahunchna), aur Backward chaining (goal se ulta chalke required preconditions dhoondhna). Jaise HTN planning mein "coffee banao" goal ke liye agent state check karta hai — agar grinder hai toh beans grind karega, warna instant coffee use karega. Real example mein GPT-4 + Code Interpreter earthquake data plot karne ke liye pehle API search karta hai, phir data fetch, parse, aur matplotlib se histogram banata hai — har step ka output agle step mein jaata hai. Yahi hai smart agents ka asli dimaag, dost — bade problem ko manageable pieces mein todna.

Go deeper — visual, from zero

Test yourself — AI Agents & Tool Use

Connections