What is a Session?
A session is a container for conversation state that includes:- Session IDs for identifying and tracking conversations
- Conversation history (stored messages)
- Mutable state for context providers
- Provider-managed data (memory, context, metadata)
- Python
- .NET
In Python, Sessions are created per agent and can be serialized for persistence.
AgentSession is a lightweight state container:Creating and Using Sessions
Basic Session Usage
- Python
- .NET
- Call
agent.create_session()to create a new session - Pass the same
sessionobject to all relatedrun()calls - The agent automatically manages conversation history
Session with Custom ID
- Python
- .NET
Conversation History Management
Automatic History Storage
- Python
- .NET
When you provide a session without configuring storage, the framework automatically adds an in-memory history provider:The
InMemoryHistoryProvider is added automatically when:- A session is provided
- No context providers are configured
- Service-side storage is not requested (no
service_session_id)
Custom History Providers
- Python
- .NET
Implement custom history storage by extending Configure what gets stored:
BaseHistoryProvider:Session State
- Python
- .NET
Sessions maintain a mutable Custom types in state are automatically handled if they implement
state dictionary for provider data:Serialization
Sessions can be serialized for persistence:to_dict()/from_dict() or are Pydantic models.Context Providers
Context providers add context to agent invocations through the session:- Python
- .NET
Service-Managed Sessions
- Python
- .NET
Some services (like Azure AI Foundry) manage sessions server-side:The framework automatically:
- Skips local history providers when
service_session_idis set - Updates
session.service_session_idfrom service responses - Propagates conversation IDs between calls
Session Streaming
- Python
- .NET
Sessions work with both streaming and non-streaming modes:
Complete Example: Multi-Agent Session
- Python
- .NET
Best Practices
Session Management Tips
- Session Scope: Create one session per conversation/user
- Cleanup: Clear sessions when conversations end
- Serialization: Persist sessions for long-running conversations
- State Size: Keep session state small; offload large data to external storage
- Provider Order: Providers run in order; plan dependencies carefully
- Testing: Test session behavior with multi-turn scenarios
Next Steps
Agents
Learn how agents interact with sessions
Middleware
Modify session behavior with middleware
Tools
Tools can access session state
Observability
Monitor session lifecycle with telemetry