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







