Skip to content
GitHubLog inStart free
cetra is not released yet. These docs describe the development version.

Microsoft.Extensions.AI

cetra traces Microsoft.Extensions.AI IChatClient calls as model call and tool spans, from the OpenTelemetry instrumentation that UseOpenTelemetry adds to the client.

Terminal window
dotnet add package Microsoft.Extensions.AI --version 11.0.0
dotnet add package Microsoft.Extensions.AI.OpenAI --version 11.0.0
dotnet add package OpenAI --version 2.15.0
dotnet add package OpenTelemetry --version 1.21.0
dotnet add package OpenTelemetry.Exporter.OpenTelemetryProtocol --version 1.21.0

The application owns the OpenTelemetry SDK. Export over OTLP/HTTP with the standard variables, and set a service name so your traces are named:

Terminal window
export OTEL_EXPORTER_OTLP_ENDPOINT=http://localhost:8080
export OTEL_EXPORTER_OTLP_HEADERS="Authorization=Bearer sk-cetra-…"
export OTEL_SERVICE_NAME=my-app

Add UseOpenTelemetry to the client builder, and listen on the source name you pass it. Set OPENAI_API_KEY to your OpenAI key:

using System.ClientModel;
using System.ComponentModel;
using Microsoft.Extensions.AI;
using OpenAI;
using OpenTelemetry;
using OpenTelemetry.Trace;
using var tracerProvider = Sdk.CreateTracerProviderBuilder()
.AddSource("Experimental.Microsoft.Extensions.AI")
.AddOtlpExporter()
.Build();
var openAi = new OpenAIClient(
new ApiKeyCredential(Environment.GetEnvironmentVariable("OPENAI_API_KEY")!));
IChatClient inner = openAi.GetChatClient("gpt-4o-mini").AsIChatClient();
IChatClient client = new ChatClientBuilder(inner)
.UseFunctionInvocation()
.UseOpenTelemetry(sourceName: "Experimental.Microsoft.Extensions.AI",
configure: c => c.EnableSensitiveData = true)
.Build();
[Description("Get the current weather for a city")]
static string GetWeather([Description("City name")] string city) => $"Sunny in {city}, 21 C";
var tool = AIFunctionFactory.Create(GetWeather, "get_weather");
var answer = await client.GetResponseAsync("What is the weather in Lyon? Use the tool.",
new ChatOptions { Tools = [tool] });
Console.WriteLine("answer: " + answer.Text);

EnableSensitiveData = true records the messages and the tool arguments and results; it is off by default. UseFunctionInvocation runs the called tools and sends their results back to the model.

With one tool round, a call produces:

  • an llm span named chat gpt-4o-mini for each model call, with the provider openai, the request and response models, the input and output messages, the tool definitions, the token counts and the cost computed from the built-in prices;
  • a tool span named execute_tool get_weather, with the arguments as its input and the result as its output;
  • a plain orchestrate_tools span around the call. It carries the summed token usage of the model calls below it; cetra counts the tokens on the llm spans, so nothing is counted twice.
  • Content is off by default: without EnableSensitiveData = true, the spans carry no messages, tool arguments or results.
  • OpenTelemetry .NET 1.21.0 ignores OTEL_EXPORTER_OTLP_TRACES_ENDPOINT. Set OTEL_EXPORTER_OTLP_ENDPOINT, the base URL without the /v1/traces path; the exporter appends the path itself.
  • The capture sent no Authorization header; the header setting above is the library’s documented option, not part of the captured run.
  • The fixture was captured with Microsoft.Extensions.AI 11.0.0, OpenAI 2.15.0 and OpenTelemetry .NET 1.21.0.
  • If a field is missing, look at the span’s Attributes tab and compare it with Span attributes.