![]() |
VOOZH | about |
dotnet add package Azure.AI.Projects.Agents --version 2.0.0
NuGet\Install-Package Azure.AI.Projects.Agents -Version 2.0.0
<PackageReference Include="Azure.AI.Projects.Agents" Version="2.0.0" />
<PackageVersion Include="Azure.AI.Projects.Agents" Version="2.0.0" />Directory.Packages.props
<PackageReference Include="Azure.AI.Projects.Agents" />Project file
paket add Azure.AI.Projects.Agents --version 2.0.0
#r "nuget: Azure.AI.Projects.Agents, 2.0.0"
#:package Azure.AI.Projects.Agents@2.0.0
#addin nuget:?package=Azure.AI.Projects.Agents&version=2.0.0Install as a Cake Addin
#tool nuget:?package=Azure.AI.Projects.Agents&version=2.0.0Install as a Cake Tool
Develop Agents using the Azure AI Foundry platform, leveraging an extensive ecosystem of models, tools, and capabilities from OpenAI, Microsoft, and other LLM providers.
Note: This package is dedicated to perform CRUD operations on Agents and can be used to enable the telemetry.
Product documentation | Samples | API reference documentation | Package (NuGet) | SDK source code
To use Azure AI Agents capabilities, you must have an Azure subscription. This will allow you to create an Azure AI resource and get a connection URL.
Install the client library for .NET with NuGet:
dotnet add package Azure.AI.Extensions.OpenAI --prerelease
You must have an Azure subscription and Cosmos DB account (SQL API). In order to take advantage of the C# 8.0 syntax, it is recommended that you compile using the .NET Core SDK 3.0 or higher with a language version of
latest. It is also possible to compile with the .NET Core SDK 2.1.x using a language version ofpreview.
To be able to create, update and delete Agents, please use AgentAdministrationClient. It is a good practice to only allow this operation for users with elevated permissions, for example, administrators.
var projectEndpoint = System.Environment.GetEnvironmentVariable("FOUNDRY_PROJECT_ENDPOINT");
var modelDeploymentName = System.Environment.GetEnvironmentVariable("FOUNDRY_MODEL_NAME");
AgentAdministrationClient agentsClient = new(endpoint: new Uri(projectEndpoint), tokenProvider: new DefaultAzureCredential());
When clients send REST requests to the endpoint, one of the query parameters is api-version. It allows us to select the API versions supporting different features. The current stable version is v1 (default).
The API version may be set supplying version parameter to AgentAdministrationClientOptions constructor as shown in the example code below.
AgentAdministrationClientOptions options = new(version: AgentAdministrationClientOptions.ServiceVersion.V1);
AgentAdministrationClient agentsClient = new(endpoint: new Uri(projectEndpoint), tokenProvider: new DefaultAzureCredential(), options: options);
The Azure.AI.Projects.Agents framework organized in a way that for each call, requiring the REST API request, there are synchronous and asynchronous counterparts where the letter has the "Async" suffix. For example, the following code demonstrates the creation of a ProjectsAgentVersion object.
Synchronous call:
DeclarativeAgentDefinition agentDefinition = new(model: modelDeploymentName)
{
Instructions = "You are a prompt agent."
};
ProjectsAgentVersion agentVersion1 = agentsClient.CreateAgentVersion(
agentName: "myAgent1",
options: new(agentDefinition));
Console.WriteLine($"Agent created (id: {agentVersion1.Id}, name: {agentVersion1.Name}, version: {agentVersion1.Version})");
ProjectsAgentVersion agentVersion2 = agentsClient.CreateAgentVersion(
agentName: "myAgent2",
options: new(agentDefinition));
Console.WriteLine($"Agent created (id: {agentVersion2.Id}, name: {agentVersion2.Name}, version: {agentVersion2.Version})");
Asynchronous call:
DeclarativeAgentDefinition agentDefinition = new(model: modelDeploymentName)
{
Instructions = "You are a prompt agent."
};
ProjectsAgentVersion agentVersion1 = await agentsClient.CreateAgentVersionAsync(
agentName: "myAgent1",
options: new(agentDefinition));
Console.WriteLine($"Agent created (id: {agentVersion1.Id}, name: {agentVersion1.Name}, version: {agentVersion1.Version})");
ProjectsAgentVersion agentVersion2 = await agentsClient.CreateAgentVersionAsync(
agentName: "myAgent2",
options: new(agentDefinition));
Console.WriteLine($"Agent created (id: {agentVersion2.Id}, name: {agentVersion2.Name}, version: {agentVersion2.Version})");
In the most of code snippets we will show only asynchronous sample for brevity. Please refer individual samples for both synchronous and asynchronous code.
When creating the Agents we need to supply Agent definitions to its constructor. To create a declarative prompt Agent, use the DeclarativeAgentDefinition:
DeclarativeAgentDefinition agentDefinition = new(model: modelDeploymentName)
{
Instructions = "You are a prompt agent."
};
ProjectsAgentVersion agentVersion1 = await agentsClient.CreateAgentVersionAsync(
agentName: "myAgent1",
options: new(agentDefinition));
Console.WriteLine($"Agent created (id: {agentVersion1.Id}, name: {agentVersion1.Name}, version: {agentVersion1.Version})");
ProjectsAgentVersion agentVersion2 = await agentsClient.CreateAgentVersionAsync(
agentName: "myAgent2",
options: new(agentDefinition));
Console.WriteLine($"Agent created (id: {agentVersion2.Id}, name: {agentVersion2.Name}, version: {agentVersion2.Version})");
The code above will result in creation of ProjectsAgentVersion object, which is the data object containing Agent's name and version.
Note: This feature is in the preview, to use it, please disable the AAIP001 warning.
#pragma warning disable AAIP001
Hosted agents simplify the custom agent deployment on fully controlled environment see more.
To use hosted agent we need to provide the Foundry-Features header in our REST requests. It can be done using PipelinePolicy.
internal class FeaturePolicy(string feature) : PipelinePolicy
{
private const string _FEATURE_HEADER = "Foundry-Features";
public override void Process(PipelineMessage message, IReadOnlyList<PipelinePolicy> pipeline, int currentIndex)
{
message.Request.Headers.Add(_FEATURE_HEADER, feature);
ProcessNext(message, pipeline, currentIndex);
}
public override async ValueTask ProcessAsync(PipelineMessage message, IReadOnlyList<PipelinePolicy> pipeline, int currentIndex)
{
message.Request.Headers.Add(_FEATURE_HEADER, feature);
await ProcessNextAsync(message, pipeline, currentIndex);
}
}
To create the hosted agent, please use the HostedAgentDefinition while creating the AgentVersion object.
private static HostedAgentDefinition GetAgentDefinition(string dockerImage, string modelDeploymentName, string accountId, string applicationInsightConnectionString, string projectEndpoint)
{
HostedAgentDefinition agentDefinition = new(
versions: [new ProtocolVersionRecord(ProjectsAgentProtocol.ActivityProtocol, "v1")],
cpu: "1",
memory: "2Gi"
)
{
EnvironmentVariables = {
{ "AZURE_OPENAI_ENDPOINT", $"https://{accountId}.cognitiveservices.azure.com/" },
{ "AZURE_OPENAI_CHAT_DEPLOYMENT_NAME", modelDeploymentName },
// Optional variables, used for logging
{ "APPLICATIONINSIGHTS_CONNECTION_STRING", applicationInsightConnectionString },
{ "AGENT_PROJECT_RESOURCE_ID", projectEndpoint },
},
Image = dockerImage,
};
return agentDefinition;
}
The created agent needs to be deployed using Azure CLI
az login
az cognitiveservices agent start --account-name ACCOUNTNAME --project-name PROJECTNAME --name myHostedAgent --agent-version 1
After the deployment is complete, this Agent can be used for calling responses.
Agent deletion should be done through Azure CLI.
az cognitiveservices agent delete-deployment --account-name ACCOUNTNAME --project-name PROJECTNAME --name myHostedAgent --agent-version 1
az cognitiveservices agent delete --account-name ACCOUNTNAME --project-name PROJECTNAME --name myHostedAgent --agent-version 1
Note: Tracing functionality is in preliminary preview and is subject to change. Spans, attributes, and events may be modified in future versions.
Environment variable values: All tracing-related environment variables accept
true(case-insensitive) or1as equivalent enabling values.
Tracing requires enabling GenAI-specific OpenTelemetry support. One way to do this is to set the AZURE_EXPERIMENTAL_ENABLE_GENAI_TRACING environment variable value to true. You can also enable the feature with the following code:
AppContext.SetSwitch("Azure.Experimental.EnableGenAITracing", true);
Precedence: If both the
AppContextswitch and the environment variable are set, theAppContextswitch takes priority. No exception is thrown on conflict. If neither is set, the value defaults tofalse.
Important: When you enable Azure.Experimental.EnableGenAITracing, the SDK automatically enables the Azure.Experimental.EnableActivitySource flag, which is required for the OpenTelemetry instrumentation to function.
You can add an Application Insights Azure resource to your Microsoft Foundry project. If one was enabled, you can get the Application Insights connection string, configure your AI Projects client, and observe traces in Azure Monitor. Typically, you might want to start tracing before you create a client or Agent.
First, set the APPLICATIONINSIGHTS_CONNECTION_STRING environment variable to point to your Azure Monitor resource.
For tracing to Azure Monitor from your application, the preferred option is to use Azure.Monitor.OpenTelemetry.AspNetCore. Install the package with NuGet:
dotnet add package Azure.Monitor.OpenTelemetry.AspNetCore
More information about using the Azure.Monitor.OpenTelemetry.AspNetCore package can be found here.
Another option is to use Azure.Monitor.OpenTelemetry.Exporter package. Install the package with NuGet:
dotnet add package Azure.Monitor.OpenTelemetry.Exporter
Here is an example how to set up tracing to Azure Monitor using Azure.Monitor.OpenTelemetry.Exporter:
var tracerProvider = Sdk.CreateTracerProviderBuilder()
.AddSource("Azure.AI.Projects.*")
.SetResourceBuilder(ResourceBuilder.CreateDefault().AddService("AgentTracingSample"))
.AddAzureMonitorTraceExporter().Build();
For tracing to console from your application, install the OpenTelemetry.Exporter.Console with NuGet:
dotnet add package OpenTelemetry.Exporter.Console
Here is an example how to set up tracing to console:
var tracerProvider = Sdk.CreateTracerProviderBuilder()
.AddSource("Azure.AI.Projects.*") // Add the required sources name
.SetResourceBuilder(OpenTelemetry.Resources.ResourceBuilder.CreateDefault().AddService("AgentTracingSample"))
.AddConsoleExporter() // Export traces to the console
.Build();
Content recording controls whether message contents and tool call related details, such as parameters and return values, are captured with the traces. This data may include sensitive user information.
To enable content recording, set the OTEL_INSTRUMENTATION_GENAI_CAPTURE_MESSAGE_CONTENT environment variable to true. Alternatively, you can control content recording with the following code:
AppContext.SetSwitch("Azure.Experimental.TraceGenAIMessageContent", false);
If neither the environment variable nor the AppContext switch is set, content recording defaults to false.
Precedence: If both the
AppContextswitch and the environment variable are set, theAppContextswitch takes priority. No exception is thrown on conflict.
Any operation that fails will throw a ClientResultException. The exception's Status will hold the HTTP response status code. The exception's Message contains a detailed message that may be helpful in diagnosing the issue:
try
{
ProjectsAgentVersion agent = await agentsClient.GetAgentVersionAsync(
agentName: "agent_which_dies_not_exist", agentVersion: "1");
}
catch (ClientResultException e) when (e.Status == 404)
{
Console.WriteLine($"Exception status code: {e.Status}");
Console.WriteLine($"Exception message: {e.Message}");
}
To further diagnose and troubleshoot issues, you can enable logging following the Azure SDK logging documentation. This allows you to capture additional insights into request and response details, which can be particularly helpful when diagnosing complex issues.
Beyond the introductory scenarios discussed, the AI Agents client library offers support for additional scenarios to help take advantage of the full feature set of the AI services. To help explore some of these scenarios, the AI Agents client library offers a set of samples to serve as an illustration for common scenarios. Please see the Samples
See the Azure SDK CONTRIBUTING.md for details on building, testing, and contributing to this library.
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net5.0 net5.0 was computed. net5.0-windows net5.0-windows was computed. net6.0 net6.0 was computed. net6.0-android net6.0-android was computed. net6.0-ios net6.0-ios was computed. net6.0-maccatalyst net6.0-maccatalyst was computed. net6.0-macos net6.0-macos was computed. net6.0-tvos net6.0-tvos was computed. net6.0-windows net6.0-windows was computed. net7.0 net7.0 was computed. net7.0-android net7.0-android was computed. net7.0-ios net7.0-ios was computed. net7.0-maccatalyst net7.0-maccatalyst was computed. net7.0-macos net7.0-macos was computed. net7.0-tvos net7.0-tvos was computed. net7.0-windows net7.0-windows was computed. net8.0 net8.0 is compatible. net8.0-android net8.0-android was computed. net8.0-browser net8.0-browser was computed. net8.0-ios net8.0-ios was computed. net8.0-maccatalyst net8.0-maccatalyst was computed. net8.0-macos net8.0-macos was computed. net8.0-tvos net8.0-tvos was computed. net8.0-windows net8.0-windows was computed. net9.0 net9.0 was computed. net9.0-android net9.0-android was computed. net9.0-browser net9.0-browser was computed. net9.0-ios net9.0-ios was computed. net9.0-maccatalyst net9.0-maccatalyst was computed. net9.0-macos net9.0-macos was computed. net9.0-tvos net9.0-tvos was computed. net9.0-windows net9.0-windows was computed. net10.0 net10.0 is compatible. net10.0-android net10.0-android was computed. net10.0-browser net10.0-browser was computed. net10.0-ios net10.0-ios was computed. net10.0-maccatalyst net10.0-maccatalyst was computed. net10.0-macos net10.0-macos was computed. net10.0-tvos net10.0-tvos was computed. net10.0-windows net10.0-windows was computed. |
| .NET Core | netcoreapp2.0 netcoreapp2.0 was computed. netcoreapp2.1 netcoreapp2.1 was computed. netcoreapp2.2 netcoreapp2.2 was computed. netcoreapp3.0 netcoreapp3.0 was computed. netcoreapp3.1 netcoreapp3.1 was computed. |
| .NET Standard | netstandard2.0 netstandard2.0 is compatible. netstandard2.1 netstandard2.1 was computed. |
| .NET Framework | net461 net461 was computed. net462 net462 was computed. net463 net463 was computed. net47 net47 was computed. net471 net471 was computed. net472 net472 was computed. net48 net48 was computed. net481 net481 was computed. |
| MonoAndroid | monoandroid monoandroid was computed. |
| MonoMac | monomac monomac was computed. |
| MonoTouch | monotouch monotouch was computed. |
| Tizen | tizen40 tizen40 was computed. tizen60 tizen60 was computed. |
| Xamarin.iOS | xamarinios xamarinios was computed. |
| Xamarin.Mac | xamarinmac xamarinmac was computed. |
| Xamarin.TVOS | xamarintvos xamarintvos was computed. |
| Xamarin.WatchOS | xamarinwatchos xamarinwatchos was computed. |
Showing the top 2 NuGet packages that depend on Azure.AI.Projects.Agents:
| Package | Downloads |
|---|---|
|
Azure.AI.Projects
This is the Azure.AI.Projects client library for developing .NET applications with rich experience. |
|
|
Ananke.Federation.Azure
Azure AI Agent Service adapter for Ananke Federation — deploy workflows to Azure AI Foundry, monitor platform agents, and translate manifests to Assistants API configuration. |
This package is not used by any popular GitHub repositories.
| Version | Downloads | Last Updated |
|---|---|---|
| 2.1.0-beta.3 | 5,896 | 5/30/2026 |
| 2.1.0-beta.2 | 31,059 | 5/14/2026 |
| 2.1.0-beta.1 | 71,277 | 4/21/2026 |
| 2.0.0 | 220,227 | 4/1/2026 |
| 2.0.0-beta.1 | 130,148 | 3/18/2026 |