Recursive AI

Trace LLM calls with OpenTelemetry in C#

OneLLM is a free, open-source LLM and agent library for .NET from Recursive AI. MIT or Apache-2.0, no native dependencies.

Trace a model call through the OneLlm activity source, using an offline scripted response.

Terminal
dotnet add package RecursiveAI.OneLlm

Requires .NET 10 (net10.0); .NET 9 and earlier aren’t supported.

C#
using System.Diagnostics;
using OneLlm;
using OneLlm.Testing;
 
using ActivityListener listener = new() {
    ShouldListenTo = s => s.Name == "OneLlm",
    Sample = (ref ActivityCreationOptions<
        ActivityContext> o) =>
        ActivitySamplingResult
            .AllDataAndRecorded,
    ActivityStopped = a =>
        Console.WriteLine(a.DisplayName)
};
ActivitySource.AddActivityListener(
    listener);
ScriptedProvider provider = new("openai");
provider.Enqueue(
    ScriptStep.Text("Hello."));
Llm.Default = new Runtime();
Llm.Default.Providers.Set(
    "openai", provider);
await Llm.AskAsync(
    "gpt-6-luna", "Say hello.");

Unzip the example, then dotnet run.

The zip holds the full program below. To run only the snippet at the top, paste it into Program.cs of a new console app and install the package above. That snippet prints:

Text
chat gpt-6-luna

The full program

OneLLM emits standard .NET Activity spans and Meter metrics, which the OpenTelemetry SDK reads.

Here, gpt-6-luna is a placeholder model name that works with the scripted provider; it does not select a live model.

The download adds a MeterListener to the trace listener. It prints the usage categories and duration instrument name, not measurement values. The response and usage are scripted; no provider account or telemetry backend is contacted.

When a model call fails, its span has Error status and an error.type attribute naming the exception.

C#
using System.Diagnostics;
using System.Diagnostics.Metrics;
using OneLlm;
using OneLlm.Testing;
 
using var spans = new ActivityListener
{
    ShouldListenTo = source => source.Name == "OneLlm",
    Sample = (ref ActivityCreationOptions<ActivityContext> options)
        => ActivitySamplingResult.AllDataAndRecorded,
    ActivityStopped = span =>
    {
        Console.WriteLine($"Span: {span.DisplayName} ({span.Kind})");
        Console.WriteLine($"Provider: {span.GetTagItem("gen_ai.provider.name")}");
    }
};
ActivitySource.AddActivityListener(spans);
using var metrics = new MeterListener();
metrics.InstrumentPublished = (instrument, listener) =>
{
    if (instrument.Meter.Name == "OneLlm")
        listener.EnableMeasurementEvents(instrument);
};
metrics.SetMeasurementEventCallback<long>((instrument, value, tags, state) =>
{
    foreach (var tag in tags)
        if (tag.Key == "gen_ai.token.type")
            Console.WriteLine($"Measurement: {instrument.Name} ({tag.Value})");
});
metrics.SetMeasurementEventCallback<double>((instrument, value, tags, state) =>
    Console.WriteLine($"Measurement: {instrument.Name}"));
metrics.Start();
 
var provider = new ScriptedProvider("openai");
provider.Enqueue(ScriptStep.TextWithUsage("Hello.", 12, 2));
Llm.Default = new Runtime();
Llm.Default.Providers.Set("openai", provider);
Console.WriteLine(await Llm.AskAsync("gpt-6-luna", "Say hello."));

Running dotnet run from the unzipped example prints:

Text
Measurement: gen_ai.client.operation.duration
Measurement: gen_ai.client.token.usage (input)
Measurement: gen_ai.client.token.usage (output)
Span: chat gpt-6-luna (Client)
Provider: openai
Hello.

Variations

To inspect the requested model instead of the span name, replace the ActivityStopped callback in the snippet at the top with:

C#
ActivityStopped = a => Console.WriteLine(
    a.GetTagItem("gen_ai.request.model"))
Text
gpt-6-luna

Use the OpenTelemetry SDK and console exporter

In a new console app, install these packages in addition to RecursiveAI.OneLlm:

Terminal
dotnet add package OpenTelemetry
dotnet add package OpenTelemetry.Exporter.Console

This standalone variation was run with both OpenTelemetry packages at version 1.21.0. It selects the OneLlm activity source and meter and exports to the console, without a collector or API key:

C#
using OneLlm;
using OneLlm.Testing;
using OpenTelemetry;
using OpenTelemetry.Metrics;
using OpenTelemetry.Trace;
 
using var tracing = Sdk.CreateTracerProviderBuilder()
    .AddSource("OneLlm")
    .AddConsoleExporter()
    .Build();
using var metrics = Sdk.CreateMeterProviderBuilder()
    .AddMeter("OneLlm")
    .AddConsoleExporter()
    .Build();
 
var provider = new ScriptedProvider("openai");
provider.Enqueue(ScriptStep.Text("Hello."));
Llm.Default = new Runtime();
Llm.Default.Providers.Set("openai", provider);
await Llm.AskAsync("gpt-6-luna", "Say hello.");
metrics.ForceFlush();

An excerpt of the console exporter’s actual output:

Text
Activity.DisplayName:        chat gpt-6-luna
Activity.Kind:               Client

The short answer

Subscribe to the OneLlm activity source before calling the model. Subscribe to the meter with the same name for metrics. OneLLM emits through System.Diagnostics, so the download uses the .NET listeners directly and does not require an exporter or an OpenTelemetry SDK package to inspect an event.

How do I trace an LLM call in C#?

Create an ActivityListener that selects OneLlm, asks to record activity data and handles ActivityStopped. Keep the listener alive until the asynchronous model call has finished. The snippet at the top registers the listener before making its scripted call and disposes it only when the program ends.

OneLLM’s telemetry reference documents one client span per model call, with the name formed from the operation and requested model. For the download that is chat gpt-6-luna. The response is not generated by a live model: ScriptedProvider returns the configured text while the runtime takes its normal model-call path.

The span carries the provider name, requested model, response model and response identifier when supplied. It also records the finish reason. Use those attributes to distinguish what your application requested from what a provider says answered it. On a failed call, inspect the error.type attribute and the error status rather than assuming a stopped activity means a successful response.

How do I collect LLM observability metrics?

A trace listener does not also subscribe to metrics. The full program creates a separate MeterListener, selects instruments from the OneLlm meter and enables measurement events. Its callbacks show when a measurement arrives.

The documented instruments are gen_ai.client.token.usage, a histogram of long values, and gen_ai.client.operation.duration, a histogram of double values in seconds. Usage has an input or output category in gen_ai.token.type. Operation, provider and requested model are metric tags; the duration metric adds error.type for a failed call. The example prints names and categories only, so its output stays stable across runs.

The measurement callbacks have different numeric types because the instruments do. Registering only the long callback does not inspect the double-valued duration instrument. Enable both before the call when checking that you receive both kinds of signal.

How does OneLLM connect to OpenTelemetry?

The source and meter are both named OneLlm, with that exact capitalization. In an existing OpenTelemetry SDK configuration, select that activity source with AddSource("OneLlm") and that meter with AddMeter("OneLlm"), as documented in the library reference. Configure exporting in your application’s telemetry setup; the console listeners in this recipe inspect data locally rather than sending it to a collector.

Prompts, completions and tool arguments are not recorded on OneLLM spans. The text Hello. in the output comes from the application’s explicit Console.WriteLine, not from span content. Keep that distinction when reviewing what your own logging adds.

OneLLM’s documentation also notes that the GenAI semantic conventions are evolving. Use the attribute and instrument names documented for the package version you run rather than assuming a dashboard written against another convention revision will match automatically.

All onellm recipes