Sub topic 10
Guide reasoning and handle uncertainty
This chapter assumes you can call a chat API, read basic Python, and have written a system prompt with a task, rules, and an output format. Here you will learn how to tell the model when to reason before answering, and what to do when it does not know.
Why reasoning and uncertainty need instructions
A model produces fluent text whether or not it has good grounds for it. Two failures follow from this:
- It answers too fast on multi-step tasks (arithmetic, policy checks, comparisons) and gets the result wrong.
- It guesses when the context lacks the answer, and the guess reads like a fact.
Your prompt can reduce both. It cannot remove them, so you still test for them (see the evaluation chapter).
Ask for reasoning only where it helps
Step-by-step reasoning helps on tasks with several dependent steps. It adds cost and latency on simple lookups. Many current models also have a built-in reasoning mode that you switch on through an API setting. If you use that mode, a long "think step by step" script in the prompt is usually unnecessary. A short goal-level hint is enough.
A common pattern is to separate the reasoning from the final answer, so your code can parse only the answer:
# Reasoning
Before answering, work through the question in <analysis> tags:
1. List the facts in <documents> that are relevant.
2. Check whether those facts are enough to answer.
3. Decide on the answer, or decide that you cannot answer.
# Output
After </analysis>, reply with one JSON object: {"answer": string, "confidence": "high" | "low"}.Note the trade-off. Visible reasoning is easier to debug, but it increases tokens, and you must strip it before showing output to users. Do not treat the written reasoning as a faithful record of how the model decided. It is useful evidence, not proof.
Tell the model what to do when it does not know
"Don't hallucinate" is too vague to follow. Give a concrete fallback instead:
- Name the condition: "If the documents do not state the refund window..."
- Name the action: "...say that the documents do not cover it and offer a human agent."
- Allow partial answers: "If only part of the question is covered, answer that part and name what is missing."
Also give the model permission to be unsure. Models tend to comply with the implied expectation that an answer always exists, so state that "I don't know" is an acceptable output.
Expose uncertainty in a form your code can use
Free-text hedging ("I think maybe...") is hard to act on. A field with fixed values is easier:
import json
ALLOWED_CONFIDENCE = {"high", "low"}
def route_reply(raw: str) -> str:
"""Decide what the app does with a model reply."""
data = json.loads(raw)
if data.get("confidence") not in ALLOWED_CONFIDENCE:
return "retry"
if data["confidence"] == "low":
return "hand_to_human"
return "show_to_user"
print(route_reply('{"answer": "30 days", "confidence": "high"}'))
print(route_reply('{"answer": "unclear", "confidence": "low"}'))The self-reported value is not a calibrated probability. Models are often over-confident. Treat it as a coarse signal, and check it against your test set: if "high" replies are wrong 20% of the time, route them differently.
Common errors
| Error | Effect | Fix |
|---|---|---|
| "Always answer" with no fallback | Invented answers | Add an explicit "cannot answer" path |
| Reasoning mixed into the final output | Broken JSON parsing | Use separate tags, and parse only the answer |
| Asking for reasoning on every request | Higher cost, slower replies | Limit it to multi-step tasks |
| Trusting the confidence field blindly | Wrong answers shown as certain | Measure it against labeled test cases |
Exercise
Exercise
Your support bot returns {"answer": "...", "confidence": "high"} but is sometimes wrong when the documents are silent. Name two prompt changes and one test you would add.
Show answer
First, add an explicit rule: if the documents do not contain the answer, say so and offer a human agent, and state that this is an acceptable reply. Second, add an <analysis> step that checks whether the documents are enough before answering. For the test, add cases where the documents deliberately lack the answer, and assert that the reply declines or has "low" confidence instead of inventing a fact.