Skip to main content

Sessions API Reference

Session classes for managing conversation state and context.

AgentSession

Base abstraction for all agent sessions, containing the state of a specific conversation with an agent.

Overview

An AgentSession contains the state of a specific conversation with an agent, which may include:
  • Conversation history or a reference to externally stored conversation history
  • Memories or a reference to externally stored memories
  • Any other state that the agent needs to persist across runs
Sessions may also have behaviors attached that provide:
  • Customized storage of state
  • Data extraction from and injection into conversations
  • Chat history reduction (e.g., message summarization or truncation)

Properties

AgentSessionStateBag
required
Gets any arbitrary state associated with this session. This is a dictionary for storing custom data.

Methods

GetService()

Retrieves a service of the specified type from the session.
Type
required
The type of object being requested.
object?
An optional key to help identify the target service.
object? | TService?
The found object, or null if not available.

Example: Using StateBag


ChatClientAgentSession

Session implementation for use with ChatClientAgent.

Properties

string?
Gets or sets the ID of the underlying service chat history. Used to support cases where chat history is stored by the agent service (server-side).
AgentSessionStateBag
Inherited from AgentSession. Dictionary for storing arbitrary state.

Remarks

The ConversationId property may be null in the following cases:
  • The agent stores messages via a ChatHistoryProvider and not in the agent service
  • This is a new session and server-managed chat history has not yet been created
The ConversationId may change over time when pointing to service-managed chat history, depending on the service’s default behavior (e.g., forking history with each iteration).

Example: Client-Side History (Default)

Example: Server-Side History

Example: Session Serialization and Persistence


AgentSessionStateBag

A dictionary for storing arbitrary state in a session.

Overview

AgentSessionStateBag is a dictionary-like container for storing custom data in a session. It’s used by context providers, chat history providers, and application code to maintain state across agent runs.

Example: Using StateBag for User Preferences


ChatHistoryProvider

Provides chat history management for cases where history is not stored by the agent service.

Overview

ChatHistoryProvider is an abstract base class for implementing custom chat history storage. The framework includes InMemoryChatHistoryProvider as a default implementation.

Built-in Implementation: InMemoryChatHistoryProvider

Example: Custom Chat History Provider (Redis)


AIContextProvider

Provides additional context to agents during invocation.

Overview

AIContextProvider is an abstract base class for implementing providers that inject additional context (messages, tools, instructions) into agent runs. Common uses include RAG (Retrieval Augmented Generation), dynamic tool selection, and context-aware instructions.

Example: Simple RAG Context Provider

Example: Dynamic Tool Provider