Sessions API Reference
Session management and context provider system for conversation state and context engineering.AgentSession
A lightweight conversation session with an agent.Constructor
Unique identifier for this session. If None, a UUID will be generated.
Service-managed session ID (e.g., thread ID from Assistants API). If using service-side storage.
Properties
The unique identifier for this session.
Service-managed session ID. Can be updated after creation.
Mutable state dictionary shared with all providers. Used to store conversation history, context, and provider-specific data.
Methods
to_dict()
Serialize session to a plain dict for storage/transfer.Serialized session dictionary. Values in
state that implement to_dict()/from_dict() are automatically serialized.from_dict() (classmethod)
Restore session from a previously serialized dict.Dictionary from a previous
to_dict() call.Restored AgentSession instance.
Example: Basic Session Usage
SessionContext
Per-invocation state passed through the context provider pipeline.Attributes
The ID of the current session.
Service-managed session ID.
The new messages being sent to the agent (set by caller).
Dictionary mapping source_id -> messages added by that provider. Maintains insertion order (provider execution order).
Additional instructions added by providers.
Additional tools added by providers.
After invocation, contains the full AgentResponse. Read-only for providers.
Options passed to agent.run() - read-only, for reflection only.
Shared metadata dictionary for cross-provider communication.
Methods
extend_messages()
Add context messages from a specific source.Either a plain source_id string, or an object with a
source_id attribute (e.g., a context provider).The messages to add. Messages are copied before attribution is added.
extend_instructions()
Add instructions to be prepended to the conversation.The provider source_id adding these instructions.
A single instruction string or sequence of strings.
extend_tools()
Add tools to be available for this invocation.The provider source_id adding these tools.
The tools to add. Tools are attributed with source metadata.
get_messages()
Get context messages, optionally filtered and including input/response.If provided, only include context messages from these sources.
If provided, exclude context messages from these sources.
If True, append input_messages after context.
If True, append response.messages at the end.
Flattened list of messages in conversation order.
BaseContextProvider
Base class for context providers that participate in the context engineering pipeline.Constructor
Unique identifier for this provider instance. Used for message/tool attribution.
Methods
before_run()
Called before model invocation.The agent running this invocation.
The current session.
The invocation context - add messages/instructions/tools here.
The provider-scoped mutable state dict. This is a reference to
session.state[provider.source_id].after_run()
Called after model invocation.The agent that ran this invocation.
The current session.
The invocation context with response populated.
The provider-scoped mutable state dict.
Example: RAG Context Provider
BaseHistoryProvider
Base class for conversation history storage providers.Constructor
Unique identifier for this provider instance.
Whether to load messages before invocation. When False, the agent skips calling
before_run entirely.Whether to store input messages.
Whether to store context from other providers.
If set, only store context from these source_ids.
Whether to store response messages.
Abstract Methods
get_messages()
Retrieve stored messages for this session.The session ID to retrieve messages for.
Additional arguments (e.g.,
state for in-memory providers).List of stored messages.
save_messages()
Persist messages for this session.The session ID to store messages for.
The messages to persist.
Additional arguments.
Example: Database History Provider
InMemoryHistoryProvider
Built-in history provider that stores messages in session.state.Constructor
Unique identifier for this provider instance.
Whether to load messages before invocation.
Whether to store input messages.
Whether to store context from other providers.
If set, only store context from these source_ids.
Whether to store response messages.
Notes
- Messages are stored in
state["messages"]as a list of Message objects - Serialization to/from dicts is handled by
AgentSession.to_dict()/from_dict() - This provider holds no instance state - all data lives in the session’s state dict
- Auto-added by the agent for local sessions when no providers are configured and service-side storage is not requested
Example
register_state_type()
Register a type for automatic deserialization in session state.Usage
The type to register. Types with
to_dict/from_dict methods or Pydantic BaseModel subclasses are handled automatically.Notes
- Pydantic models are auto-registered on first serialization
- Pre-registering ensures deserialization works even if the model hasn’t been serialized in this process yet
- The type identifier defaults to
cls.__name__.lower()but can be overridden by defining a_get_type_identifierclassmethod