Sub topic 3
What models get wrong and why
This chapter covers the main ways language models fail and the habits that contain each failure. It assumes you can read basic Python, know what a prompt and a model API call are, and have read the chapter on how a model turns a prompt into text.
The root cause
A model predicts a likely next token, one token at a time. It does not look facts up, and it has no built-in check that its output is true. Most failures follow from that one design fact.
Hallucination
A hallucination is fluent output that is false or unsupported. The model may invent a citation, an API method, or a policy that sounds right. Fluent text is the thing it is trained to produce, so confidence in the tone says nothing about accuracy.
Common triggers:
- The question asks for a fact the model never saw or saw rarely.
- The prompt assumes something false, and the model goes along with it.
- The model has no source in the prompt and fills the gap from memory.
Typical fixes are to put source text in the prompt, tell the model it may answer "unknown", and check outputs against a known list when you can.
Nondeterminism
Models usually sample from a probability distribution, so the same prompt can give different answers. Lowering the temperature setting makes output more repeatable, but it does not guarantee identical output. Design for variation. Test with many inputs, not one lucky run.
Context limits
The context window is the maximum amount of text, in tokens, that the model can read and write in one call. Two problems follow:
- Input beyond the limit is rejected or cut off.
- Even inside the limit, details buried in the middle of a long prompt are used less reliably than details at the start or end.
Models also have a training cutoff, so they do not know recent events unless you supply them.
Failure modes at a glance
| Failure | What you see | First defense |
|---|---|---|
| Hallucination | Confident, invented details | Supply sources, allow "unknown" |
| Nondeterminism | Different answers per run | Lower temperature, test many cases |
| Context overflow | Errors or ignored details | Trim input, retrieve only what matters |
| Stale knowledge | Outdated answers | Pass current data in the prompt |
Containing failures in code
Treat model output as untrusted input. Validate it before use, and choose a safe fallback. This classifier accepts only allowed labels:
LABELS = ["billing", "bug", "feature_request", "other"]
def classify(ticket: str, complete) -> str:
prompt = (
"Classify the support ticket into exactly one label from: "
+ ", ".join(LABELS)
+ ".\nReply with the label only.\n\nTicket: " + ticket
)
answer = complete(prompt).strip().lower()
return answer if answer in LABELS else "other"If the model replies with a made-up label or a full sentence, the function returns "other" instead of passing bad data downstream.
Exercise
A support bot sometimes gives a refund deadline that is not in your policy documents, and it sounds certain. Name the failure and give two fixes.
Show answer
This is hallucination. Put the relevant policy text in the prompt and instruct the model to say it does not know when the text lacks the answer. Also check any deadline in the reply against the policy data, or route such answers to a person.
Exercise
You run the same prompt twice at temperature 0 and get slightly different answers. Does this mean your code is broken?
Show answer
No. Lower temperature reduces variation but does not guarantee identical output. Build tests that tolerate small differences, such as checking for an allowed label or required fields.