# Practical and Useful Patterns for OpenAI Agent SDK
🌍 Control agent input and output safely with guardrails!
Use cheap, fast models for pre-checks to block inappropriate requests and reduce costs.
📌 Title: Guardrails
🔗 URL:
🧩 Overview
Guardrails run checks on agent inputs and outputs. Input guardrails validate user input, while output guardrails validate agent responses. By using cheap, fast models as guardrails to prevent unnecessary execution of expensive models, you achieve cost optimization. The tripwire mechanism immediately halts execution when a problem is detected.
🛠 Usage
Import `Agent`, `InputGuardrail`, `OutputGuardrail`, and `GuardrailFunctionOutput` from `agents`. Define a guardrail function `check_homework_request(context, agent, input_data)` that calls ` input_data)` and returns `GuardrailFunctionOutput(output_info= tripwire_triggered= Create the agent with `Agent(name="TutorBot", instructions="You are a tutoring assistant.", input_guardrails=[InputGuardrail(guardrail_function=check_homework_request)])`.
🏗 Practical Patterns
**Pre-filtering with Cheap Models (Cost Reduction)**
Before running the expensive main model, use a cheap fast model to detect and block "homework requests" or "abuse." When the tripwire triggers, the main model execution is skipped, reducing costs.
Define a cheap classifier `Agent(name="Classifier", model="gpt-4o-mini", output_type=ClassificationResult)` and an `abuse_check` function that runs ` input_data)` with `tripwire_triggered= Attach it to the main agent via `Agent(name="Assistant", model="gpt-4o", input_guardrails=[InputGuardrail(guardrail_function=abuse_check)])`.
**Output Guardrails for Content Checking**
Verify that agent responses don't contain sensitive or inappropriate content.
Define an output guardrail function `check_sensitive_output(context, agent, output)` that runs ` f"Check this output for sensitive content: {output}")` and returns `GuardrailFunctionOutput(output_info= tripwire_triggered= Attach it with `Agent(name="SupportBot", output_guardrails=[OutputGuardrail(guardrail_function=check_sensitive_output)])`.
**Relevance Check for Support Bots**
Pre-determine whether user questions fall within scope, handling out-of-scope queries early.
Define `relevance_check(context, agent, input_data)` which runs ` input_data)` and sets `tripwire_triggered=not to catch out-of-scope questions. Apply it with `Agent(name="SupportBot", instructions="Answer customer support questions about our product.", input_guardrails=[InputGuardrail(guardrail_function=relevance_check)])`.
💡 Use Cases
🛡 Block homework completion and abuse requests (cost reduction)
🔍 Prevent sensitive data in output (PII, internal information leak prevention)
📋 Control support bot response scope
⚡ Optimize API costs with cheap model pre-screening
⚠️ Considerations
- When a guardrail tripwire triggers, an `InputGuardrailTripwireTriggered` exception is raised — catch and handle it appropriately
- Input guardrails run **in parallel** with the main model by default, so the main execution may proceed if the guardrail doesn't finish in time
- Factor in the model call cost of the guardrails themselves
- Multiple guardrails can be set — execution halts if any one triggers
✨ Use guardrails to build agents that balance safety and cost efficiency!
#
OpenAIAgentSDK# #
AIAgent#