# 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#