A guardrail agent checks another agent’s input or output. Instead of relying only on fixed rules such as string matching or length checks, it can use a prompt to validate, annotate, or handle data. That flexibility adds complexity: a prompt-based check can also fail, so its result needs a clear handling policy. section 4.4.2
Check an input before research begins
A research workflow can give an input guardrail agent a list of forbidden topics. The agent returns typed fields describing the request and whether the research is forbidden. Changing the restriction can mean changing the guardrail prompt rather than rewriting a string check; the new behavior still needs testing. section 4.4.2
For generated answers, choose the check that matches the failure you need to catch:
- A grounding agent compares an answer with its retrieved context to check whether the claims are supported. Give both the answer-generating agent and the grounding agent access to that context. section 7.3.1
- A critic checks generated content against criteria or a rubric and can request regeneration. An evaluation agent can return typed correctness results and feedback for more complex output. section 7.3 section 7.2.4
Block an unsupported RAG answer
A RAG agent produces an answer, then a grounding agent returns a typed grounded flag and feedback. An output guardrail trips when the flag is false. The calling code catches that exception and returns “No grounded answer available.” Another policy could send the feedback back for regeneration, with an attempt limit and a defined fallback. section 7.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
)
The output-guardrail listing shows the enforcement boundary: run the grounding agent, preserve its flag and feedback, and trigger the tripwire when the answer is not grounded. section 7.3.3
Decide what happens after a failed check before deploying the flow: retry, block with a static response, pass the result forward, or escalate when the stakes warrant it. Test guardrail behavior with benchmark questions and inspect failures; adding a checking agent does not make its judgments infallible. section 7.3 section 7.5
