๊ฐ€์ž… ํ›„ ์ดˆ๋Œ€ ๋งํฌ๋ฅผ ๊ณต์œ ํ•˜๋ฉด ๋™์˜์ƒ ์žฌ์ƒ ๋ฐ ์ดˆ๋Œ€ ๋ณด์ƒ์„ ๋ฐ›์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

cv usk
@cv_usk
AI / Software Research Notes AI Agent, LLMOps, MLOps, Software Architecture ๆŠ•็จฟใฏๅ€‹ไบบใฎๆ„่ฆ‹ใงใ™ใ€‚
๊ฐ€์ž… May 2026
258 ํŒ”๋กœ์ž‰ ์ค‘    220 ํŒฌ
# Practical and Useful Patterns for OpenAI Agent SDK ๐ŸŒ Want to filter what goes into the model or gracefully handle errors without crashing? Input filters and error handlers give you fine-grained control over agent behavior at the edges. ๐Ÿ“Œ Title: Running agents โ€“ Hooks and customization / Error handlers ๐Ÿ”— URL: ๐Ÿงฉ Overview `call_model_input_filter` is a hook that transforms model input just before it's sent โ€” perfect for trimming history, masking secrets, or injecting dynamic system instructions. `error_handlers` lets you catch specific errors (tool failures, model refusals, max turns) and return app-specific fallback output instead of raising exceptions. Together, they enable production-grade resilient agents. ๐Ÿ›  Usage Import `Agent`, `Runner`, and `ErrorHandlers` from `agents`. Define `trim_history(input_data)` to keep only the last 10 messages via `input_data.messages = input_data.messages[-10:]`, and `mask_secrets(input_data)` to replace secrets with `msg.content.replace(os.environ.get("API_KEY", ""), "***")`. Define `handle_refusal(ctx, error)` to return a domain-specific fallback on model refusal. Configure the agent with `Agent(name="chef", instructions="You are a recipe assistant.", call_model_input_filter=trim_history, error_handlers=ErrorHandlers(model_refusal=handle_refusal, max_turns=lambda ctx, err: FallbackOutput(message="Processing did not complete. Try shorter input.", include_in_history=False)))`. ๐Ÿ— Practical Patterns **History Trimming for Cost Control** Use `call_model_input_filter` to keep only the last N messages, reducing token consumption in long conversations. A practical pattern: always preserve the system prompt at the top while pruning older user messages. **Secret Masking** When tool outputs contain API keys or tokens, mask them before they reach the model via the input filter. This reduces the risk of the model memorizing or echoing sensitive information. **Dynamic System Instruction Injection** Inside the filter, inject context-aware system instructions based on user permissions or state. For example: "This customer is on the Premium plan" โ€” letting the model tailor its responses dynamically. **Graceful max_turns Fallback** When a looping agent hits the turn limit, return a user-friendly message instead of an exception. Setting `include_in_history=False` keeps the fallback out of future turns, so retries start clean. **App-Specific Model Refusal Handling** Instead of catching `ModelRefusalError` and returning a generic error, return a structured fallback matching your domain model. A recipe app returns an empty Recipe with `refusal_reason`; a chat app suggests alternative topics. ๐Ÿ’ก Use Cases ๐Ÿ”’ Prevent API keys and tokens from reaching the model ๐Ÿ“ Auto-trim long chat history to optimize token costs ๐Ÿ”„ Guide users to next actions when max_turns is reached ๐Ÿ›ก Return structured domain-specific fallbacks on model refusal โš ๏ธ Caveats - Over-pruning messages in `call_model_input_filter` causes the model to lose context and degrade response quality. Always preserve critical system prompts. - Ensure fallback outputs from `error_handlers` match the agent's `output_type`. Type mismatches cause runtime errors. - Fallback outputs with `include_in_history=False` won't be available in subsequent turns. Use this only for display-only information. - Exceptions thrown inside filters or handlers will halt the entire agent. Write defensive logic within these hooks. โœจ Combine input filters and error handlers to build agents that handle edge cases gracefully in production! #OpenAIAgentSDK# #AIAgent#
๋” ๋ณด๊ธฐ