Observability for Microsoft Agent Framework (.NET) with Opik

Microsoft Agent Framework is a comprehensive multi-language framework for building, orchestrating, and deploying AI agents and multi-agent workflows with support for both Python and .NET implementations.

The framework provides everything from simple chat agents to complex multi-agent workflows with graph-based orchestration, built-in OpenTelemetry integration for distributed tracing and monitoring, and a flexible middleware system for request/response processing.

Account Setup

Comet provides a hosted version of the Opik platform, simply create an account and grab your API Key.

You can also run the Opik platform locally, see the installation guide for more information.

Microsoft Agent Framework .NET tracing

Getting started

To use the Microsoft Agent Framework .NET integration with Opik, you will need to have the Agent Framework and the required OpenTelemetry packages installed:

$# Agent Framework (2 packages)
>dotnet add package Microsoft.Agents.AI --prerelease
>dotnet add package Microsoft.Extensions.AI.OpenAI --prerelease
>
># Hosting (1 package)
>dotnet add package Microsoft.Extensions.Hosting
>
># OpenTelemetry (3 packages)
>dotnet add package OpenTelemetry.Exporter.OpenTelemetryProtocol
>dotnet add package OpenTelemetry.Extensions.Hosting
>dotnet add package OpenTelemetry.Instrumentation.Http

You will also need to configure your OpenAI API key:

$export OPENAI_API_KEY=<your-openai-api-key>

In addition, you will need to set the following environment variables to configure OpenTelemetry to send data to Opik:

If you are using Opik Cloud, you will need to set the following environment variables:

$export OTEL_EXPORTER_OTLP_ENDPOINT=https://www.comet.com/opik/api/v1/private/otel/v1/traces
>export OTEL_EXPORTER_OTLP_HEADERS="Authorization=<your-api-key>,Comet-Workspace=default"
>export OTEL_EXPORTER_OTLP_PROTOCOL=http/protobuf

To log the traces to a specific project, you can add the projectName parameter to the OTEL_EXPORTER_OTLP_HEADERS environment variable:

$export OTEL_EXPORTER_OTLP_HEADERS="Authorization=<your-api-key>,Comet-Workspace=default,projectName=<your-project-name>"

You can also update the Comet-Workspace parameter to a different value if you would like to log the data to a different workspace.

Using Opik with Microsoft Agent Framework (.NET)

The Microsoft Agent Framework has built-in OpenTelemetry instrumentation. You need to configure the OpenTelemetry SDK to use HTTP/Protobuf protocol and export to Opik. Here’s a complete example:

1using Microsoft.Agents.AI;
2using Microsoft.Extensions.AI;
3using Microsoft.Extensions.DependencyInjection;
4using Microsoft.Extensions.Hosting;
5using OpenAI;
6using OpenTelemetry;
7using OpenTelemetry.Resources;
8using OpenTelemetry.Trace;
9
10#pragma warning disable OPENAI001
11
12const string SourceName = "AgentFramework";
13
14// Create host with OpenTelemetry configuration
15var builder = Host.CreateApplicationBuilder(args);
16
17// Configure OpenTelemetry
18builder.Services.AddOpenTelemetry()
19 .ConfigureResource(resource => resource
20 .AddService(serviceName: "agent-framework-demo", serviceVersion: "1.0.0"))
21 .WithTracing(tracing => tracing
22 .AddSource(SourceName) // Our custom spans
23 .AddSource("*Microsoft.Extensions.AI") // Chat client telemetry
24 .AddSource("*Microsoft.Extensions.Agents*") // Agent telemetry
25 .AddHttpClientInstrumentation() // HTTP calls
26 .AddOtlpExporter()); // Reads OTEL_EXPORTER_OTLP_* env vars automatically
27
28var host = builder.Build();
29await host.StartAsync();
30
31// Create instrumented chat client
32// Note: OpenAI SDK requires explicit API key, but you can read from env var
33var apiKey = Environment.GetEnvironmentVariable("OPENAI_API_KEY")
34 ?? throw new InvalidOperationException("OPENAI_API_KEY environment variable is required");
35
36using var instrumentedChatClient = new OpenAIClient(apiKey)
37 .GetChatClient("gpt-4o-mini")
38 .AsIChatClient() // Convert to Microsoft.Extensions.AI.IChatClient
39 .AsBuilder()
40 .UseOpenTelemetry( // Enable telemetry on chat client
41 sourceName: SourceName,
42 configure: (cfg) => cfg.EnableSensitiveData = true)
43 .Build();
44
45// Create instrumented agent
46var agent = new ChatClientAgent(
47 instrumentedChatClient,
48 name: "Assistant",
49 instructions: "You are a helpful assistant.")
50 .AsBuilder()
51 .UseOpenTelemetry(SourceName) // Enable telemetry on agent
52 .Build();
53
54// Run agent - telemetry is automatic!
55Console.WriteLine(await agent.RunAsync("Write a haiku about AI agents."));
56
57// Ensure traces are flushed
58var tracerProvider = host.Services.GetService<TracerProvider>();
59tracerProvider?.ForceFlush();
60await Task.Delay(2000);
61
62await host.StopAsync();

Important: The .NET OpenTelemetry SDK requires explicitly setting OTEL_EXPORTER_OTLP_PROTOCOL=http/protobuf to use HTTP with Protocol Buffers. If not set, it defaults to gRPC which is not supported by Opik’s endpoint.

You can also configure this programmatically:

1.AddOtlpExporter(options =>
2{
3 options.Protocol = OtlpExportProtocol.HttpProtobuf;
4})

The framework will automatically:

  • Create traces for agent executions
  • Log input prompts and outputs
  • Track token usage and performance metrics
  • Capture any errors or exceptions

Further improvements

If you would like to see us improve this integration, simply open a new feature request on Github.

Reference

For more information about the Microsoft Agent Framework OpenTelemetry integration, see: