Share via


AgentThread Class

Definition

Base abstraction for all agent threads.

public abstract class AgentThread
type AgentThread = class
Public MustInherit Class AgentThread
Inheritance
AgentThread
Derived

Remarks

An AgentThread 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 for a conversation.

An AgentThread may also have behaviors attached to it that may include:

  • Customized storage of state.
  • Data extraction from and injection into a conversation.
  • Chat history reduction, e.g. where messages needs to be summarized or truncated to reduce the size.
An AgentThread is always constructed by an AIAgent so that the AIAgent can attach any necessary behaviors to the AgentThread. See the GetNewThread() and DeserializeThread(JsonElement, JsonSerializerOptions) methods for more information.

Because of these behaviors, an AgentThread may not be reusable across different agents, since each agent may add different behaviors to the AgentThread it creates.

To support conversations that may need to survive application restarts or separate service requests, an AgentThread can be serialized and deserialized, so that it can be saved in a persistent store. The AgentThread provides the Serialize(JsonSerializerOptions) method to serialize the thread to a JsonElement and the DeserializeThread(JsonElement, JsonSerializerOptions) method can be used to deserialize the thread.

Constructors

AgentThread()

Initializes a new instance of the AgentThread class.

Methods

GetService(Type, Object)

Asks the AgentThread for an object of the specified type serviceType.

GetService<TService>(Object)

Asks the AgentThread for an object of type TService.

MessagesReceivedAsync(IEnumerable<ChatMessage>, CancellationToken)

This method is called when new messages have been contributed to the chat by any participant.

Serialize(JsonSerializerOptions)

Serializes the current object's state to a JsonElement using the specified serialization options.

Applies to

See also