Skip to main content

AIAgent API Reference

The base abstraction for all AI agents, defining the core interface for agent interactions and conversation management.

AIAgent

Provides the foundational class for implementing AI agents that can participate in conversations and process user requests.

Overview

The AIAgent class serves as the base for implementing AI agents in the .NET SDK. An agent instance may participate in multiple concurrent conversations, and each conversation may involve multiple agents working together.

Properties

string
required
The unique identifier for this agent instance. For in-memory agents, defaults to a randomly-generated ID, while service-backed agents typically use the identifier assigned by the backing service.
string?
The human-readable name of the agent, used for display purposes and to help users identify the agent’s purpose or capabilities.
string?
A description of the agent’s purpose, capabilities, or behavior. Helps models and users understand the agent’s intended purpose.
AgentRunContext?
Gets or sets the AgentRunContext for the current agent run. This value flows across async calls.

Methods

CreateSessionAsync()

Creates a new conversation session that is compatible with this agent.
CancellationToken
default:"default"
The cancellation token to monitor for cancellation requests.
ValueTask<AgentSession>
A new AgentSession instance ready for use with this agent.
Each session represents an independent conversation. If the agent supports multiple session types, this method returns the default or configured session type.

RunAsync()

Runs the agent with the provided messages, session, and options.
string | ChatMessage
The message to send to the agent. String messages are automatically wrapped in a ChatMessage with ChatRole.User.
IEnumerable<ChatMessage>
A collection of messages to send to the agent for processing.
AgentSession?
The conversation session to use. If null, a new session will be created. The session is updated with input messages and response messages.
AgentRunOptions?
Optional configuration parameters for controlling the agent’s invocation behavior.
CancellationToken
default:"default"
The cancellation token to monitor for cancellation requests.
Task<AgentResponse>
An AgentResponse containing the agent’s output, including all generated messages and metadata.

RunStreamingAsync()

Runs the agent in streaming mode, providing real-time updates as the agent generates its response.
string | ChatMessage
The message to send to the agent. String messages are automatically wrapped in a ChatMessage with ChatRole.User.
IEnumerable<ChatMessage>
A collection of messages to send to the agent for processing.
AgentSession?
The conversation session to use. If null, a new session will be created.
AgentRunOptions?
Optional configuration parameters for controlling the agent’s invocation behavior.
CancellationToken
default:"default"
The cancellation token to monitor for cancellation requests.
IAsyncEnumerable<AgentResponseUpdate>
An asynchronous enumerable of AgentResponseUpdate instances representing the streaming response. Each update represents a portion of the complete response.

SerializeSessionAsync()

Serializes an agent session to its JSON representation for persistent storage.
AgentSession
required
The session to serialize.
JsonSerializerOptions?
Optional settings to customize the serialization process.
CancellationToken
default:"default"
The cancellation token to monitor for cancellation requests.
ValueTask<JsonElement>
A JsonElement containing the serialized session state.

DeserializeSessionAsync()

Deserializes an agent session from its JSON serialized representation.
JsonElement
required
A JsonElement containing the serialized session state.
JsonSerializerOptions?
Optional settings to customize the deserialization process.
CancellationToken
default:"default"
The cancellation token to monitor for cancellation requests.
ValueTask<AgentSession>
A restored AgentSession instance with the state from the serialized data.

GetService()

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

Example: Basic Agent Usage

Example: Streaming Responses

Example: Multi-Turn Conversation

Example: Session Serialization

Protected Methods

These methods are intended for implementation by derived classes:

CreateSessionCoreAsync()

Core implementation of session creation logic.

RunCoreAsync()

Core implementation of the agent invocation logic.

RunCoreStreamingAsync()

Core implementation of the agent streaming invocation logic.

SerializeSessionCoreAsync()

Core implementation of session serialization logic.

DeserializeSessionCoreAsync()

Core implementation of session deserialization logic.