0Pricing
AI Agents · Lesson

Sub-Question Decomposition Strategy

For complex multi-hop questions, split into sub-questions, answer each, then synthesize a final answer.

Sub-Question Decomposition Strategy is a free AI Agents lesson on CoddyKit — lesson 4 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the AI Agents learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

When Single-Hop Fails

Some questions need multiple lookups:

"Compare the revenue of Apple in 2022 and Microsoft in 2023."

One similarity search will not retrieve both facts; you need TWO sub-queries.

Sub-Question Decomposition

LlamaIndex SubQuestionQueryEngine splits a question into sub-questions, answers each, then synthesises:

from llama_index.core.query_engine import SubQuestionQueryEngine
from llama_index.core.tools import QueryEngineTool, ToolMetadata

query_engine_tools = [
    QueryEngineTool(
        query_engine=apple_index.as_query_engine(),
        metadata=ToolMetadata(name='apple', description='Financial data for Apple Inc.')
    ),
    QueryEngineTool(
        query_engine=msft_index.as_query_engine(),
        metadata=ToolMetadata(name='microsoft', description='Financial data for Microsoft Corp.')
    )
]

engine = SubQuestionQueryEngine.from_defaults(query_engine_tools=query_engine_tools)
response = engine.query('Compare 2022 revenue of Apple vs 2023 revenue of Microsoft')

How It Works

  1. LLM analyses the question
  2. LLM generates sub-questions and assigns each to a tool
  3. Each sub-question runs in parallel
  4. LLM synthesises a final answer from the sub-answers

Sub-Question Visibility

You can see the sub-questions the engine generated:

response = engine.query('Compare X and Y')
for sq in response.metadata.get('sub_qa', []):
    print(sq.sub_q.sub_question, '->', sq.answer)

When to Use Sub-Question

  • Comparison queries (X vs Y)
  • Multi-source aggregation
  • Questions with multiple distinct facts

When NOT to Use

  • Simple Q&A — adds latency and cost
  • Single-document context
  • Tasks with no decomposition value

Routing With LLM

Sub-Question Engine pairs with RouterQueryEngine to first decide which collection of indexes to even hit:

from llama_index.core.query_engine import RouterQueryEngine

router = RouterQueryEngine.from_defaults(query_engine_tools=query_engine_tools)
# Routes 'apple revenue' to apple index, 'msft margins' to msft, etc.

Sub-Questions for Coding Agents

For agents that write code, sub-question is a way to plan steps:

"Build a REST API" -> sub: design models, sub: write CRUD handlers, sub: add auth.

Cost Implications

Sub-question makes N sub-queries instead of 1. Each is an LLM call + retrieval. Budget accordingly — 3-4x more expensive than single-hop.

Compared to ReAct

Sub-question is upfront planning. ReAct (next course) is step-by-step. For known-decomposable queries, sub-question is faster; for exploratory tasks, ReAct adapts better.

Combined: ReAct + Sub-Question Tools

The most flexible: a ReAct agent where sub-question engines are tools — each "tool call" can itself decompose into many sub-queries.

Eval Sub-Question Quality

Track whether the LLM generates sensible sub-questions. Bad decomposition is hard to debug without inspecting traces.

Decomposition Use Case

For which kind of question is sub-question decomposition most useful?

Recap

Sub-question decomposition turns one complex query into several simple ones — at higher cost but much higher accuracy on comparison and multi-source questions.

Frequently asked questions

Is the “Sub-Question Decomposition Strategy” lesson free?

Yes — the full text of “Sub-Question Decomposition Strategy” is free to read here on the web, and the AI Agents course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the AI Agents course, upgrade to CoddyKit PRO.

What will I learn in “Sub-Question Decomposition Strategy”?

For complex multi-hop questions, split into sub-questions, answer each, then synthesize a final answer. You practise AI Agents with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start AI Agents?

No prior experience is required. AI Agents on CoddyKit is structured for beginners through advanced learners; this is — lesson 4 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Sub-Question Decomposition Strategy” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this AI Agents lesson?

Yes. Every AI Agents lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Document Loaders and Parsers
  2. The Index Hierarchy: Vector, Tree, Keyword
  3. Query Engines and Response Synthesis
  4. Sub-Question Decomposition Strategy
← Back to AI Agents