The Items system is the fundamental data structure that represents everything that happens during an agent run. Every message, tool call, handoff, and model response is represented as an "Item". Think of Items as the "ledger" or "transaction log" of an agent run - they record every event in a structured, serializable format.
Core Concepts
What is an Item?
An Item is a structured representation of an event that occurs during an agent run. Items are:
Typed - Each item has a specific type (message, tool call, handoff, etc.)
Serializable - Can be converted to/from JSON
Traceable - Each item can be traced back to the agent that created it
Convertible - Can be converted to input items for the model
Why Items Matter
Audit Trail - Complete record of what happened during a run
Represents conversation compaction (for long conversations):
Python
from agents import CompactionItem
item = CompactionItem(
agent=my_agent,
raw_item=ResponseOutputMessage(
id="compact_123",
content=[{"type": "text", "text": "Summary of conversation"}],
),
)
When it's created:
When conversation history is compacted
To reduce token usage while preserving context
Item Base Class
RunItemBase
All items inherit from RunItemBase:
Python
@dataclassclassRunItemBase(Generic[T], abc.ABC):
agent: Agent[Any]
"""The agent whose run caused this item to be generated."""
raw_item: T
"""The raw Responses item from the run."""
_agent_ref: weakref.ReferenceType[Agent[Any]] | None"""Weak reference to the agent for memory management."""
Key Features:
Agent Reference - Every item knows which agent created it
Weak References - Uses weak references to avoid memory leaks
Type Generic - Generic over the raw item type
Convertible - Can convert to input items
Memory Management
Items use weak references to agents to prevent memory leaks:
Python
item = MessageOutputItem(agent=agent, raw_item=...)
# Release the strong reference
item.release_agent()
# Agent can now be garbage collected# Item still has weak reference for debugging
Why weak references?
Long-running sessions with many items
Prevents agents from being kept alive by old items
Still allows debugging with agent information
Item Conversion
to_input_item()
Every item can be converted to an input item for the model:
from agents import ItemHelpers
# Get all tool call items
tool_calls = ItemHelpers.get_tool_calls(items)
# Get all message items
messages = ItemHelpers.get_messages(items)
# Get text content from messages
text = ItemHelpers.get_text_content(message_item)
# Convert to input list
input_list = ItemHelpers.to_input_list(items)
Key Methods:
get_tool_calls(items) - Extract tool call items
get_messages(items) - Extract message items
get_text_content(item) - Get text from a message
to_input_list(items) - Convert items to input list
get_function_call_outputs(items) - Get tool outputs
Model Response
ModelResponse Class
Represents a complete model response:
Python
@dataclassclassModelResponse:
response: Response
"""The raw OpenAI Response object."""
request_id: str | None"""The request ID for this response."""
usage: Usage
"""Token usage for this response."""
agent: Agent[Any]
"""The agent that made the request."""
When it's created:
After each model call
Contains all output items from the call
Includes usage information
Structure:
response - Full OpenAI Response object
request_id - Request identifier
usage - Token usage data
agent - Agent that made the request
Item Lifecycle
Creation Flow
Python
# 1. Model generates response
response = await model.get_response(...)
# 2. Response is processedfor output_item in response.output:
# 3. Create appropriate item typeifisinstance(output_item, ResponseOutputMessage):
item = MessageOutputItem(agent=agent, raw_item=output_item)
elifisinstance(output_item, ResponseFunctionToolCall):
item = ToolCallItem(agent=agent, raw_item=output_item)
# ... other types# 4. Add to run state
state._generated_items.append(item)
Persistence Flow
Python
# 1. Run completes
result = await Runner.run(agent, input)
# 2. Items are saved to sessionif session:
await session.save_items(
conversation_id=conversation_id,
items=result.new_items,
)
# 3. Items can be loaded later
loaded_items = await session.load_items(conversation_id)
Replay Flow
Python
# 1. Load items from session
items = await session.load_items(conversation_id)
# 2. Convert to input items
input_items = run_items_to_input_items(items)
# 3. Run with loaded items
result = await Runner.run(agent, input_items)
Raw items are Pydantic models with built-in serialization
Agent references are not serialized (use weak references)
Context is not serialized (user data)
Only the raw item data is serialized
RunState Serialization
Items are part of RunState serialization:
Python
state = result.to_state()
json_str = state.to_json()
# Later
restored_state = RunState.from_json(json_str)
What gets serialized:
All generated items
Agent identities (name, handoff description)
Tool call metadata
Usage information
Guardrail results
Item Filtering
Filtering by Type
Python
from agents import ItemHelpers
# Get only tool calls
tool_calls = [i for i in items ifisinstance(i, ToolCallItem)]
# Get only messages
messages = [i for i in items ifisinstance(i, MessageOutputItem)]
Filtering by Agent
Python
# Get items from specific agent
agent_items = [i for i in items if i.agent.name == "specialist"]
Filtering by Tool Name
Python
# Get calls to specific tool
tool_calls = [
i for i in items
ifisinstance(i, ToolCallItem)
and i.tool_name == "search"
]
Item Analysis
Analyzing Tool Usage
Python
from collections import Counter
# Count tool usage
tool_names = [i.tool_name for i in items ifisinstance(i, ToolCallItem)]
counts = Counter(tool_names)
print(counts)
# Output: Counter({'search': 5, 'calculate': 3, 'write_file': 2})
Analyzing Turn Structure
Python
# Group items by turn
turns = []
current_turn = []
for item in items:
ifisinstance(item, MessageOutputItem):
if current_turn:
turns.append(current_turn)
current_turn = [item]
else:
current_turn.append(item)
if current_turn:
turns.append(current_turn)
# Each turn is a list of itemsfor i, turn inenumerate(turns):
print(f"Turn {i}: {len(turn)} items")
# Old items are replaced with a summary
compaction_item = CompactionItem(
agent=agent,
raw_item=ResponseOutputMessage(
content=[{"type": "text", "text": "Summary of previous conversation"}],
),
)
# Replace old items with compaction item
session.replace_old_items(compaction_item)
Item and Tracing
Trace Spans from Items
Each item can create a trace span:
Python
from agents import generation_span, tool_span
# Create spans from itemsfor item in items:
ifisinstance(item, MessageOutputItem):
with generation_span(name="message_generation"):
# Trace the message generationpasselifisinstance(item, ToolCallItem):
with tool_span(name=item.tool_name):
# Trace the tool executionpass
defcount_agent_turns(items: list[RunItem], agent_name: str) -> int:
"""Count turns by a specific agent."""returnsum(
1for item in items
ifisinstance(item, MessageOutputItem)
and item.agent.name == agent_name
)
3. Find Handoff Points
Python
deffind_handoffs(items: list[RunItem]) -> list[tuple[str, str]]:
"""Find all handoff points."""
handoffs = []
for item in items:
ifisinstance(item, HandoffCallItem):
handoffs.append((item.agent.name, item.target_agent.name))
return handoffs
4. Calculate Token Usage
Python
defcalculate_total_usage(items: list[RunItem]) -> Usage:
"""Calculate total token usage."""
total = Usage(request_tokens=0, response_tokens=0, total_tokens=0)
for item in items:
ifhasattr(item, 'usage'):
total += item.usage
return total
Summary
The Items system is the foundation of run representation. Key takeaways:
Items represent all events in an agent run
Item types include messages, tool calls, handoffs, reasoning, etc.
RunItemBase is the base class for all items
Weak references prevent memory leaks
Conversion enables replay and persistence
ItemHelpers provide utility functions
ModelResponse wraps a complete model response
Serialization enables state persistence
Filtering allows item analysis
Sessions store items as conversation history
Compaction reduces token usage for long conversations
Tracing uses items for observability
Analysis enables pattern detection
Replay enables reproducing runs
Debugging is easier with item inspection
Agent references track which agent created each item
Tool metadata tracks tool usage
Handoff tracking follows agent delegation
Usage tracking monitors token consumption
Type safety ensures correct item handling
Understanding Items is essential for debugging, persistence, and analysis of agent runs.