liveBook · Manning

Grounding

As explained in AI Agents in Action, Second Edition

Grounding is the process of anchoring a model's responses in relevant external information, data, or context.

Grounding anchors an agent’s answer to retrieved context instead of letting it rely on internal training data. The retrieved documents become the authoritative source, reducing answers that contradict the available evidence. section 6.3.2

A practical grounding pattern combines retrieval, instructions, and verification:

  • Retrieve relevant passages from a document store, such as a vector database populated with semantic chunks and embeddings. section 6.3.2
  • Tell the answering agent to use only the supplied context and to say when the context does not contain the answer. section 6.3.2
  • Require citations or quoted passages so each claim points back to retrieved evidence. section 6.3.2 section 6.6
  • Check the result with a grounding agent, then approve it, request regeneration with feedback, or block it with a static response. section 7.3.1 section 7.3.3

A grounded script question

A RAG agent retrieves passages from the Back to the Future screenplay and answers only from those passages. If they do not answer the question, it reports that no grounded answer is available rather than filling the gap from general knowledge. section 6.3.2 section 7.3.3

Listing 7.9 06_RAG_grounding_guardrail_agent.py (guardrail)
@output_guardrail    #1
async def ground_answer(
    context: RunContextWrapper, agent: Agent, output: AnswerResult
) -> GuardrailFunctionOutput:
    grounding_input = dict(
        question=output.question,
        answer=output.answer,
    )
    result = await Runner.run(    #2
             grounding_agent, input=str(grounding_input))
    result = result.final_output

    return GuardrailFunctionOutput(    #3
        output_info={
            "answer_is_grounded": result.is_answer_grounded,
            "feedback": result.feedback,
        },
        tripwire_triggered=result.is_answer_grounded is False,    #4
    )

agent = Agent(
    name="RAG Agent",
    instructions="""
You are a retrieval-augmented knowledge agent.
Break down the user's query into smaller parts if needed
to fetch relevant context for the user's query.
""",
    output_type=AnswerResult,
    tools=[search_knowledge_by_keyword],
    model="gpt-4o",  # Specify the model to use
    output_guardrails=[ground_answer],    #5
)

Inspect a typed output guardrail that evaluates an answer, records grounding feedback, and triggers failure handling when the answer is unsupported. section 7.3.3

How AI Agents in Action, Second Edition covers it

How deep it goes
Explains retrieval, prompting, citations, and guardrail handling, but stops short of model internals.
Examples
Uses a Back to the Future screenplay, retrieved passages, citation prompts, and an output guardrail with a static fallback.
What it assumes
Assumes an agent can retrieve context and generate answers, while demonstrating grounding instructions and failure handling explicitly.
What you can do after
Apply it via RAG prompts and guardrails to produce, verify, reject, or retry answers against retrieved evidence.
How it connects
Connects grounding with retrieval-augmented generation, vector databases, embeddings, citations, memory, and guardrails.

Also explained in

Compare how 8 books explain grounding →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of AI Agents in Action, Second Edition. The section links point to the parts of the book it draws on.