AntRunner.Chat is a .NET library that lets you easily create and manage conversations with tool-based AI assistants. These AI agents can help you by answering questions, running tools, or performing tasks all through a simple chat interface.
- Converse naturally with AI assistants powered by OpenAI models like GPT-5, gpt-4.1-mini, and reasoning models (o1, o3, o4).
- Streaming responses - receive assistant responses in real-time as they're generated via
StreamingMessageProgressEventHandler. - Intelligent model parameters - automatically adapts between
temperature/topPfor standard models andreasoning_effortfor thinking models, allowing the same assistant definition to work with both model types. - Manage multi-turn conversations with context.
- Switch between different AI assistants on the fly.
- Undo the last message if you want to change something.
- Save and load conversations to keep your chat history.
- Track token usage to monitor your API consumption.
- External tool call support - delegate tool execution to client applications for client-handled tools via
ExternalToolCallEventHandler. - Multi-provider OAuth - pass external auth tokens for multiple providers (e.g., Microsoft Graph, GitHub) via
ExternalAuthTokens.
You can add AntRunner.Chat to your .NET project using the NuGet package manager:
dotnet add package AntRunner.Chat --version 0.9.4Or, add this directive in your C# notebook or script:
#r "nuget: AntRunner.Chat, 0.9.4"Here is a simple example showing how to start a conversation with an AI assistant and send messages:
using System.Threading.Tasks;
using AntRunner.Chat;
// Load your environment variables and AI service configuration (example method)
var envVariables = Settings.GetEnvironmentVariables();
foreach (var kvp in envVariables)
{
Environment.SetEnvironmentVariable(kvp.Key, kvp.Value);
}
var config = AzureOpenAiConfigFactory.Get();
// Set up the chat options with assistant name and model deployment
static ChatRunOptions chatConfiguration = new()
{
AssistantName = "Python Ants",
DeploymentId = "gpt-4.1-mini",
};
// Create a new conversation instance
var conversation = await Conversation.Create(chatConfiguration, config);
// Convenience method to send a message and get a response
async Task<ChatRunOutput> Chat(string message)
{
chatConfiguration.Instructions = message;
var runnerOutput = await conversation.Chat(message);
runnerOutput.LastMessage.DisplayAs("text/markdown");
return runnerOutput;
}
// Example usage: send a message
var output = await Chat("Hello AI! What can you do?");You can receive assistant responses in real-time using streaming:
// Subscribe to streaming events
void OnStreamProgress(object? sender, StreamingMessageProgressEventArgs e)
{
Console.Write(e.ContentDelta); // Write each chunk as it arrives
}
// Use ChatRunner.RunThread with streaming handler
var output = await ChatRunner.RunThread(
chatConfiguration,
serviceConfiguration,
messages,
httpClient,
onMessage: null,
onStream: OnStreamProgress);AntRunner.Chat automatically detects reasoning models (o1, o3, o4, gpt-5) and uses the appropriate parameters:
- For standard models: uses
temperatureandtopPfrom assistant definition - For reasoning models: uses
reasoning_effortinstead
This allows the same assistant definition to work seamlessly with both model types.
You can keep chatting by sending more messages:
var output = await Chat("Tell me about the prerequisites for setting up .NET 8 SDK and Docker.");If you want to remove the last message from the conversation:
conversation.Undo();Change to a different AI assistant anytime:
await conversation.ChangeAssistant("Web Ants");
var output = await Chat("What events are happening next week in my city?");Save your conversation to a file:
conversation.Save("./savedConversation.json");Load a conversation from a saved file:
var conversation2 = Conversation.Create(@"./savedConversation.json", AzureOpenAiConfigFactory.Get());See the last message or the full dialog:
var lastMessage = conversation.LastResponse.LastMessage;
var fullDialog = conversation.LastResponse.Dialog;Check token usage for the entire conversation or a single turn:
var usage = conversation.Usage;
var lastTurnUsage = output.Usage;AntRunner.Chat supports multi-agent workflows where a lead assistant can delegate tasks to specialized crew member assistants. This is powered by the CrewBridgeSchemaGenerator from AntRunner.ToolCalling.
- Define a set of specialized assistants (e.g., ResearchAgent, WriterAgent, ReviewerAgent)
- Generate a crew bridge schema that exposes each assistant as a callable tool
- The lead assistant can invoke crew members via
Agent.Invoketo delegate subtasks
using AntRunner.ToolCalling.Functions;
// Generate schema for crew members
var crewSchema = CrewBridgeSchemaGenerator.GetSchema(new[]
{
"ResearchAgent",
"WriterAgent",
"ReviewerAgent"
});
// The lead assistant can now call these as tools during conversationThis enables complex workflows like:
- A project manager assistant coordinating research, writing, and review tasks
- A coding assistant delegating to specialized testing or documentation assistants
- A customer service lead routing requests to domain-specific experts
AntRunner.Chat uses AntRunner.ToolCalling for all tool execution. Tools can be defined in multiple ways:
Define tools using OpenAPI specifications for web APIs. The library handles request building, authentication, and response processing automatically.
Use attributes to register local functions as tools without writing JSON schemas:
using AntRunner.ToolCalling.Attributes;
[Tool(OperationId = "search_files", Summary = "Search for files by pattern")]
public static async Task<string> SearchFiles(
[Parameter(Description = "The search pattern (e.g., *.cs)")] string pattern,
[Parameter(Description = "Directory to search in")] string directory = ".")
{
// Implementation
}| Attribute | Description |
|---|---|
[Tool] |
Marks a method as a callable tool with OperationId and Summary |
[Parameter] |
Provides description and metadata for tool parameters |
[RequiresNotebookContext] |
Indicates the tool needs notebook context injection |
[OAuth] |
Specifies OAuth token handling (None, Forward, Required) |
The ToolContractRegistry automatically discovers annotated methods and generates OpenAPI schemas at runtime.
A major feature of AntRunner.Chat is the use of container sandboxes to provide isolated environments for different AI assistants and tools. These sandboxes are implemented as Docker containers, enabling you to run services such as the .NET server, Python environments with or without CUDA support, PlantUML, and more.
- dotnet-server: The main .NET service container.
- python-app: Python 3.11 environment with .NET 9 SDK and optional CUDA support.
- plantuml: Container for PlantUML diagram rendering.
- qdrant: Vector search engine container.
- kernel-memory: Custom service container for kernel memory management.
To help you get started with these containers, we provide a comprehensive Setup Guide that covers all prerequisites and detailed instructions for building the local Docker images.
You can use the provided build scripts:
build_local_images.shfor Linux/macOSbuild_local_images.ps1for Windows PowerShell
These scripts will prompt you to select whether to build the CPU-only or CUDA-enabled Python images and will build all necessary images in the correct order.
Make sure to follow the setup guide to prepare your environment and build the containers before running the solution.
After setup, you run new-chat-project to create a new project with starter assistant definitions and Project Template Sample Notebooks
If you have questions or want to contribute, feel free to open an issue or pull request!