Zenaique

Where the OpenAI Agents SDK puts input and output guardrails relative to the agent loop

Flashcard·Medium·4.0 · 0·~30s·Asked atDeloitteMeeshoUniphore
Attempt it
TL;DR

Input guardrails fire pre-agent and short-circuit on trip; output guardrails fire after the final response and can suppress, regenerate, or remediate; both are deterministic Python hooks.

Memory aid
Sign in to see the mnemonic that makes this stick.
Easy to grasp

Think of a kitchen. The host (input guardrail) checks the order at the door before the chef sees it: 'no peanut allergies on this table' or 'this looks like spam, do not cook.' If the host trips, the chef never starts. After the chef plates the dish, the food runner (output guardrail) looks one more time before serving: 'this has the wrong garnish, send it back.' Both checks are people separate from the chef, so the chef cannot just lie and serve anyway. Guardrails in the OpenAI Agents SDK are the host and food runner: code that runs before and after the agent, not prompt instructions the agent might ignore.

Concept explanation~2 min read

Everything you need to truly understand this topic: intuition, mechanics, step by step explanation, code, formulas, and worked example. Click to expand.

Safety in multi-agent systems is structurally harder than in single-agent systems because there are more surfaces where adversarial input can do damage and more places where outputs need checking. The OpenAI Agents SDK addresses this with first-class guardrail hooks: input_guardrails that run before the agent processes input, and output_guardrails that run after the agent produces a final response.

The distinction between hooks and prompt-based instructions is the core architectural choice and the one most teams underweight when they first encounter the SDK. Prompt-based safety is talked-around; framework hooks are not. This walkthrough explains exactly when each hook fires, what trip behaviours are available, why parallel evaluation keeps latency bounded, and how guardrails compose across multi-agent handoffs.

Mental model: guardrails are deterministic Python code outside the agent's reasoning loop. The agent does not get a vote; the code runs.

Input guardrails: pre-agent, short-circuit on trip

When they fire

Input guardrails run once, before the active agent processes its input. Critically, 'input' means either the initial user message OR the handoff payload from a previous agent in a multi-agent handoff. The receiving agent's input guardrails treat the handoff payload as a fresh input and check it.

What they check

Common implementations:

  • PII detection: regex or ML classifier; redact or trip.
  • Policy-violating intent: LLM-as-judge prompted to classify the input against a policy.
  • Jailbreak detection: pattern matching for known prompt-injection markers, plus an LLM-judge for novel attempts.
  • Out of scope requests: classifier for whether the request matches the agent's intended use case.

Trip behaviour

A tripped input guardrail raises a GuardrailTripwireTriggered exception or returns a structured trip result. The runtime catches it and short-circuits the agent's run. The user receives a guardrail-defined response (typically a generic refusal or a redirect to a different surface).

The agent never spends inference tokens on a tripped input. That makes input guardrails the cheapest place to enforce constraints; the cost of the run is bounded by the guardrail latency, not the agent's reasoning depth.

Cost shape

Input guardrails add a fixed per-request overhead. For cheap deterministic checks (regex, classifier), this is negligible (1 to 5ms). For LLM-judge guardrails, the overhead is a small extra LLM call (100 to 500ms). The SDK runs multiple guardrails in parallel, so total overhead is bounded by the slowest single guardrail, not the sum.

Output guardrails: post final response, not per tool call
Why framework hooks beat prompt-based safety
Production layering and tuning
Sign in to unlock the full deep dive.

Situations where this technique stops working.

Sign in to see when this approach fails.

2–4 min · Everything important, quickly.

Sign in to see the quick scan of the deep dive.

Real products, models, and research that use this idea.

  • OpenAI Agents SDK ships with example input_guardrails for off-topic detection and output_guardrails for harmful content, used in production deployments of GPT-5.5 powered agents.
  • Production customer-support agents at AI-first companies use input guardrails for PII redaction before the agent sees the message and output guardrails for compliance review before the response ships.
Sign in to see more production examples.

What an interviewer would ask next. Try answering before peeking at the approach.

QHow do guardrails behave across multi-agent handoffs?
A

Each receiving agent's input_guardrails fire on the handoff payload as if it were a fresh input. Output guardrails fire when an agent produces its final response (which may be a handoff itself, terminating that agent's turn). This means a multi-agent run can trip a guardrail mid-conversation when one agent hands off something the next agent's input guardrail rejects. Useful safety property; design handoffs to fail closed.

2 more follow-ups an interviewer would ask next. Sign in to reveal them.

Red flags & common mistakes

The phrases that signal junior thinking. Click to expand.

Most common mistake

Implementing safety as prompt instructions inside the agent ('please refuse if...') instead of as framework-level guardrail hooks, which the agent can be talked out of by a sufficiently adversarial input.

Sign in to see all red flags and common mistakes.

60 second bullets to scan on the way to the call.

Sign in to unlock the revision sheet.

Primary sources. Browse if you want the original framing.

Similar questions

Same topic, related formats. Practice these next.

4 curated
Next question
Why AutoGen 0.4 makes TerminationCondition a first class primitive instead of leaving it to convention
Flashcard·Medium