Module 2 · Reasoning techniques
Prompt chaining and task decomposition
5 min read
A prompt that asks for everything at once — read a document, extract its data, reason, compute, format — concentrates every difficulty in a single generation. Splitting the task into steps, each with its own prompt, makes each step simpler, more verifiable and easier to improve. You still have to split in the right place: this lesson shows, with numbers, that the best chain is not the one that hands every step to the model.
The experiment
60 invoices with three to five lines (quantity × unit price), sometimes with a trade discount, 20% VAT. Question: the total including tax.
Facture n° F-48213
Détail de la commande :
- 3 × chaise Axis à 289,00 € l'unité
- 2 × bureau Lift à 649,00 € l'unité
- 5 × mug Thermo à 24,50 € l'unité
Remise commerciale de 10 % sur le total hors taxes.
TVA applicable : 20 %.
Three architectures:
| Architecture | Step 1 | Step 2 |
|---|---|---|
| Single prompt | "Compute the total including tax", with step-by-step reasoning | — |
| Chain: the LLM computes | Extract the lines and the discount as JSON | The LLM computes the total from the JSON, with reasoning |
| Chain: code computes | Extract the lines and the discount as JSON | Code computes the total |
The extraction step:
EXTRACT = """<facture>
{text}
</facture>
Extrais les lignes de la facture et la remise. Réponds uniquement par un objet JSON de la forme :
{{"lignes": [{{"article": "...", "quantite": 2, "prix_unitaire": 289.0}}], "remise_pct": 0}}"""
And the computation by code:
def compute_total(data: dict | None) -> float | None:
"""Le calcul fait par du code : exact, testable, gratuit."""
try:
ht = sum(float(line["quantite"]) * float(line["prix_unitaire"]) for line in data["lignes"])
return round(ht * (100 - float(data.get("remise_pct", 0))) / 100 * 1.2, 2)
except (TypeError, KeyError, ValueError):
return None
The results
| Architecture | Correct total incl. tax (within €1) |
|---|---|
| Single prompt, with reasoning | 0.18 |
| Chain: extraction, then the LLM computes | 0.03 |
| Chain: extraction, then code computes | 0.57 |
The extraction step itself produced usable JSON in 100% of cases.
Three lessons:
- The model extracts well and computes badly. Reading "3 × chaise Axis à 289,00 €" and turning it into
{"quantite": 3, "prix_unitaire": 289.0}is a recognition task, which the model succeeds at. Multiplying, adding five products, applying 10% then 20% is an exact-computation task, which it fails (lesson 1.1: numbers are sequences of tokens). - A badly split chain can make things worse: having the LLM compute from the JSON (3%) is worse than the single prompt (18%). The JSON, full of decimals and stripped of the document's context, does not help a model that cannot compute.
- The right split gives code what belongs to code: 57%, three times better than the single prompt, and every remaining failure is an extraction error, visible in the JSON, hence diagnosable and fixable (examples, targeted instruction, validation).
Principles of a good chain
| Principle | Why |
|---|---|
| One step = one capability | Reading, reasoning, computing, writing: different difficulties, different prompts |
| Code does what code does better | Arithmetic, comparisons, sorting, dates, format checks: exact and free |
| Structured intermediate outputs | JSON between steps: validatable, loggable, replayable |
| Check between steps | A validation (schema, consistency) stops the chain before the error spreads |
| Each step is evaluated separately | You know which step to improve: here, extraction |
Chain, agent or single call?
| Approach | When to use it |
|---|---|
| Single call | Simple task, or a very capable model on a short task |
| Fixed chain | The steps are known in advance: extraction → computation → writing |
| Agent with tools | The steps depend on results: search, read, search again (lesson 4.3) |
A fixed chain is more predictable, cheaper and easier to test than an agent: it is the right option as long as the path is known. The code computation can also become a tool given to the model (a calculator, a Python interpreter) when the model must decide by itself when to compute.
Key takeaways
- Splitting a task into steps simplifies each step and makes errors traceable.
- The model extracts well (100% usable JSON) and computes badly: leave arithmetic to code.
- A badly split chain can make things worse: 3% when the LLM computes from the JSON, versus 18% with a single prompt and 57% when code computes.
- Structured intermediate outputs, validation between steps, evaluation of each step.
- Fixed chain when the steps are known; agent when they depend on results.
Practice
- Run
examples/03_fewshot_and_chaining.pyand classify the errors of the "code" chain: wrongly extracted quantity, price or discount? - Add a validation between steps (the number of extracted lines must equal the number of "×" lines in the text).
- Add a third step: a confirmation sentence written by the LLM from the total computed by code.