Skip to main content
Sessions enable multi-turn conversations by maintaining conversation history and context across multiple agent invocations. They provide a lightweight mechanism for stateful interactions without requiring external storage.

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)
In Python, AgentSession is a lightweight state container:
Sessions are created per agent and can be serialized for persistence.

Creating and Using Sessions

Basic Session Usage

Key points:
  • Call agent.create_session() to create a new session
  • Pass the same session object to all related run() calls
  • The agent automatically manages conversation history

Session with Custom ID

Conversation History Management

Automatic History Storage

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

Implement custom history storage by extending BaseHistoryProvider:
Configure what gets stored:

Session State

Sessions maintain a mutable state dictionary for provider data:

Serialization

Sessions can be serialized for persistence:
Custom types in state are automatically handled if they implement to_dict()/from_dict() or are Pydantic models.

Context Providers

Context providers add context to agent invocations through the session:

Provider Lifecycle

Context providers have two hooks:

Service-Managed Sessions

Some services (like Azure AI Foundry) manage sessions server-side:
The framework automatically:
  • Skips local history providers when service_session_id is set
  • Updates session.service_session_id from service responses
  • Propagates conversation IDs between calls

Session Streaming

Sessions work with both streaming and non-streaming modes:

Complete Example: Multi-Agent Session

Best Practices

Session Management Tips
  1. Session Scope: Create one session per conversation/user
  2. Cleanup: Clear sessions when conversations end
  3. Serialization: Persist sessions for long-running conversations
  4. State Size: Keep session state small; offload large data to external storage
  5. Provider Order: Providers run in order; plan dependencies carefully
  6. Testing: Test session behavior with multi-turn scenarios
Production Considerations
  • Expiration: Implement session timeouts to free memory
  • Storage: Use durable storage (database/cache) for production sessions
  • Scaling: Sessions should be stateless externally for horizontal scaling
  • Security: Validate session ownership before resuming conversations
  • Limits: Set maximum session size to prevent memory exhaustion
  • Monitoring: Track active sessions and memory usage

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