liveBook · Manning

Hallucination

As explained in AI Agents and Applications

A hallucination is an incorrect, misleading, or fabricated response generated by a language model.

A hallucination is an incorrect, misleading, or fabricated response from a language model. It can sound plausible: when information is missing, the model may still try to complete an answer. Poor-quality training data can also contribute. Treat factual answers as unverified unless you supplied or checked the facts. section 1.4.2 section 6.1.1

A missing fact becomes a wrong answer

Asked for the total number of columns across three temples, a chatbot supplied counts even though the provided text lacked the needed figures. One count was wrong: it gave 30 columns for the Temple of Hera I, rather than 50. A safer prompt says to use only the supplied text and admit when the answer is unknown. section 6.1.1

For document Q&A, retrieval-augmented generation (RAG) retrieves relevant document chunks and places them in the prompt. This grounding reduces reliance on pretrained knowledge, but retrieval alone is not a guarantee. Tell the model to answer only from retrieved context and to say “I do not know” when that context is insufficient. Guardrails and validators can help; high-stakes answers still warrant human review. section 1.4.2 section 6.3.2

def prompt_template(question, text):
    return f'Use the following pieces of retrieved context to 
      answer the question. Only use the retrieved context to 
      answer the question. If you don\'t know the answer, or 
      the answer is not contained in the retrieved context, 
      just say that you don\'t know. Use three sentences 
      maximum and keep the answer concise. \nQuestion: 
      {question}\nContext: {text}. Remember: if you do not 
      know, just say: I do not know. Do not make up an 
      answer. For example do not say the three temples have 
      got a total of three columns. \nAnswer:'

This prompt template makes the fallback explicit: use only retrieved context, and do not invent an answer when it is missing. section 6.3.2

Match the safeguard to the task:

  • For text-to-SQL, include the target schema and sample records in a few-shot prompt to reduce invented table or column names. section 10.3
  • For tool-based agents, describe tools clearly and instruct the model to obtain facts through them. Inspect tool calls to check whether it searched before answering. section 11.8.2

Test the failure case, not just an easy question. A simple Q&A prompt produced different results across model runs: one model admitted that the temple figures were missing, while an earlier model supplied a wrong total. An explicit fallback makes the desired behavior clear without making every answer automatically reliable. section 6.3.2

How AI Agents and Applications covers it

How deep it goes
Defines the failure, then works through prompting and grounding safeguards; does not explain model internals beyond why missing information invites guesses.
Examples
Uses a wrong temple-column count, an “I do not know” response, invented SQL identifiers, and a travel agent’s search and weather calls.
What it assumes
Starts with a document-and-question chatbot before introducing retrieved context and prompt functions.
What you can do after
Diagnose/debug with missing-context questions, then apply safer prompts, schema examples, or tool guidance to reduce unsupported answers.
How it connects
Connects hallucination to retrieval-augmented generation, grounding, prompt design, few-shot prompting, and tool use.

Also explained in

Compare how 29 books explain hallucination →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of AI Agents and Applications. The section links point to the parts of the book it draws on.