# Practical and Useful Patterns for OpenAI Agent SDK
🌍 Add guardrails to tool inputs and outputs for enhanced security!
Check tool arguments and mask outputs to prevent sensitive data leaks and unauthorized operations.
📌 Title: Guardrails – Tool guardrails
🔗 URL:
🧩 Overview
Tool guardrails enable security checks before tool execution (argument checking) and after execution (output checking). They prevent API key injection, mask sensitive data, and block specific operations at the tool level. Even in complex workflows using manager patterns, Handoffs, or delegation, you can apply fine-grained checks to individual tools.
🛠 Usage
Import `Agent` and `function_tool` from `agents`. Decorate a tool function with `
@function_tool` such as `search_api(query: str) -> str`, then configure the agent with `Agent(name="SecureAgent", tools=[search_api], tool_guardrails=[check_tool_args])` to attach tool-level guardrails.
🏗 Practical Patterns
**Block API Key Injection (reject_content)**
Check if tool arguments contain API keys starting with `sk-` and block tool execution when detected.
Import `re` and `GuardrailFunctionOutput`. Define `reject_api_keys(context, agent, tool_call)` which checks ` str(tool_call.arguments))` to detect API key injection, returning `GuardrailFunctionOutput(output_info={"checked": "api_key_presence"}, tripwire_triggered=has_api_key)`. Attach it with `Agent(name="SecureAgent", tools=[search_api, call_external_service], tool_guardrails=[reject_api_keys])`.
**Mask Sensitive Data in Tool Output**
Automatically mask sensitive information (email addresses, phone numbers, etc.) in tool execution output.
Define `mask_sensitive_output(context, agent, tool_call, tool_output)` which applies `re.sub(r'[\w.+-]+@[\w-]+\.[\w.]+', '[MASKED_EMAIL]', str(tool_output))` for emails and `re.sub(r'\d{3}-\d{4}-\d{4}', '[MASKED_PHONE]', masked)` for phone numbers. Return `GuardrailFunctionOutput(output_info={"masked": True}, tripwire_triggered=False, modified_output=masked)` to pass the sanitized output. Configure with `Agent(name="DataAgent", tools=[query_customer_db], tool_guardrails=[mask_sensitive_output])`.
**Per-Tool Checks in Complex Workflows**
Apply fine-grained guardrails to specific tools even in complex workflows combining manager patterns, Handoffs, and delegation.
Define `check_delete_permission(context, agent, tool_call)` which checks ` == "delete_record"` and verifies `context.get("user_role", "viewer")` is in `["admin", "editor"]`, triggering the tripwire for unauthorized users. Non-delete tools return `tripwire_triggered=False`. Combine multiple guardrails with `Agent(name="Manager", tools=[query_db, update_record, delete_record], tool_guardrails=[check_delete_permission, reject_api_keys])`.
💡 Use Cases
🔑 Prevent API key/secret injection in tool arguments
🎭 Automatic PII masking in tool outputs
🚫 Block specific tool operations based on permissions
🔒 Security control in complex multi-agent workflows
⚠️ Considerations
- Tool guardrails are invoked per tool execution — consider performance impact
- Regex-based checks are not exhaustive — use multiple defense layers for critical security requirements
- Masking may change the original data type — verify it doesn't affect downstream processing
- When multiple tool guardrails are set, all execute sequentially
✨ Use tool guardrails for fine-grained control over agent tool operations and enhanced security!
#
OpenAIAgentSDK# #
AIAgent#