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

cv usk
@cv_usk
AI / Software Research Notes AI Agent, LLMOps, MLOps, Software Architecture ๆŠ•็จฟใฏๅ€‹ไบบใฎๆ„่ฆ‹ใงใ™ใ€‚
๊ฐ€์ž… May 2026
286 ํŒ”๋กœ์ž‰ ์ค‘    469 ํŒฌ
# Practical and Useful Patterns for OpenAI Agent SDK ๐ŸŒ Are you extracting everything you need from agent execution results โ€” for the next turn, UI rendering, and debugging? Understanding RunResult's surfaces lets you get maximum value from every agent run. ๐Ÿ“Œ Title: Results ๐Ÿ”— URL: ๐Ÿงฉ Overview `RunResult` holds the complete outcome of an agent execution. It provides multiple surfaces: `final_output` for the user response, `to_input_list()` for building the next turn, `new_items` for UI rendering and audit trails with metadata, `last_agent` for continuing after handoffs, `interruptions`/`to_state()` for resumable snapshots, `raw_responses` for provider-level debugging, and `last_response_id` for Responses API chaining. ๐Ÿ›  Usage Define `Agent(name="assistant", instructions="You are a helpful assistant.")` and run with `result = await input="Explain Python async/await")`. Access the final answer via ` Build the next turn's input with ` + [{"role": "user", "content": "Show me a concrete example"}]` and pass it to ` again. Iterate ` to inspect each `item.type` and ` Use `result.last_agent` to get the agent after a handoff for the next turn. If `result.interruptions` exist, save state with ` and resume later via ` state=state)`. Inspect `result.raw_responses` for model name and token usage, and use `result.last_response_id` for Responses API chaining. ๐Ÿ— Practical Patterns **final_output โ€” User-Facing Response** `final_output` is the agent's final answer. When `output_type` is set, it returns a Pydantic model instance; otherwise, a string. Use it directly for API responses or chat UI rendering. **to_input_list() โ€” Multi-Turn Conversation Building** `to_input_list()` converts execution results into the input format for the next turn. Append the user's new message and pass it back to ` for natural multi-turn conversations. This is the provider-agnostic approach. **new_items โ€” UI Display and Audit Trails** `new_items` contains every item generated during execution (messages, tool calls, handoffs) with metadata. Use it for step-by-step chat UI rendering or audit logging. You can trace which agent called which tool and when. **last_agent โ€” Continuation After Handoff** When a handoff occurs, `last_agent` returns the agent that handled the final step. Use it as the agent for the next ` call to continue the conversation with the correct specialist. **interruptions + to_state() โ€” Pause and Resume** In human-in-the-loop workflows, save execution state as a snapshot with `to_state()` when an interruption occurs. After human approval, resume from the saved state โ€” picking up exactly where the agent left off. **raw_responses โ€” Provider-Level Debugging** `raw_responses` contains raw provider responses with model name, token usage, and latency data. Essential for cost analysis, performance tuning, and debugging unexpected model behavior. **last_response_id โ€” Responses API Chaining** When using OpenAI's Responses API, pass `last_response_id` as `previous_response_id` in the next run for server-managed conversation continuation. The lightest-weight approach to multi-turn. ๐Ÿ’ก Use Cases ๐Ÿ’ฌ `final_output`: Display final answer in API responses or chat UI ๐Ÿ”„ `to_input_list()`: Manage multi-turn conversation history ๐Ÿ“‹ `new_items`: Step-by-step UI rendering, audit trail logging ๐Ÿค– `last_agent`: Continue conversation with the correct agent after handoff ๐Ÿ“ธ `to_state()`: Pause and resume human-in-the-loop workflows ๐Ÿ” `raw_responses`: Monitor token usage and analyze costs โš ๏ธ Caveats - `final_output` can be `None` (e.g., when execution ends with only a handoff). Always null-check before using it. - `to_input_list()` includes tool call history. Filter manually if the target agent doesn't need it. - `new_items` count grows proportionally with execution complexity. Watch memory usage with many tool calls. - States saved with `to_state()` are serializable, but be mindful of SDK version compatibility for long-term storage. - `last_response_id` is OpenAI-specific and not available with other providers. โœจ Master every surface of RunResult to extract maximum value from your agent executions! #OpenAIAgentSDK# #AIAgent#
๋” ๋ณด๊ธฐ