Register and share your invite link to earn from video plays and referrals.

cv usk
@cv_usk
AI / Software Research Notes AI Agent, LLMOps, MLOps, Software Architecture ๆŠ•็จฟใฏๅ€‹ไบบใฎๆ„่ฆ‹ใงใ™ใ€‚
Joined May 2026
258 Following    224 Followers
# 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#
Show more