Path: redb-route/redb.Route.Llm/Telemetry/LlmMetrics.cs
Snapshot: redb-route, development snapshot 2026-12-06. This is the development state of the public mirrors; it may differ from the published packages.
License: Apache-2.0. Repository: https://github.com/redbase-app/redb-route
Page: https://redbase.app/src/dev-2026-12-06/redb-route/redb.Route.Llm/Telemetry/LlmMetrics.cs
----
using System.Diagnostics.Metrics;
using redb.Route.Telemetry;
namespace redb.Route.Llm.Telemetry;
///
/// LLM-specific counters and histograms. Published on the same shared meter
/// ( = "redb.Route") so a single
/// .AddMeter("redb.Route") in OpenTelemetry picks up everything.
///
/// Standard tags: llm.provider, llm.model.id, llm.factory.
/// Optional: llm.stop_reason, llm.tool.name.
///
///
public static class LlmMetrics
{
/// Meter instance. Same canonical name as .
public static readonly Meter Meter = new(RouteMetrics.MeterName, GetVersion());
private static string GetVersion() =>
typeof(LlmMetrics).Assembly.GetName().Version?.ToString() ?? "0.0.0";
/// Total LLM calls dispatched to a provider (one per provider HTTP roundtrip).
public static readonly Counter Calls =
Meter.CreateCounter("redb.route.llm.calls", "calls",
"Number of provider calls (one per HTTP roundtrip; an agent run with N iterations emits N).");
/// Failed provider calls (timeout, transport error, parsing error).
public static readonly Counter CallsFailed =
Meter.CreateCounter("redb.route.llm.calls.failed", "calls",
"Provider calls that ended with an exception.");
/// Provider call latency in milliseconds (one observation per call).
public static readonly Histogram CallDuration =
Meter.CreateHistogram("redb.route.llm.call.duration", "ms",
"Wall-clock duration of a single provider call.");
/// Input tokens billed (sum across calls).
public static readonly Counter TokensIn =
Meter.CreateCounter("redb.route.llm.tokens.in", "tokens",
"Input tokens reported by the provider.");
/// Output tokens billed.
public static readonly Counter TokensOut =
Meter.CreateCounter("redb.route.llm.tokens.out", "tokens",
"Output tokens reported by the provider.");
/// Estimated USD cost (counter; converted to long-cents via x10000).
public static readonly Counter CostMicroUsd =
Meter.CreateCounter("redb.route.llm.cost.usd_micro", "usd*1e-6",
"Estimated cost in micro-USD (1e-6 USD).");
/// Number of agent runs (one per To(\"llm://...\") exchange).
public static readonly Counter AgentRuns =
Meter.CreateCounter("redb.route.llm.agent.runs", "runs",
"Number of agent runs (each may include multiple provider calls).");
/// Total tool-loop iterations performed by the agent (histogram per run).
public static readonly Histogram AgentIterations =
Meter.CreateHistogram("redb.route.llm.agent.iterations", "iterations",
"Tool-loop iterations per agent run.");
/// Tool invocations (one per tool_use block executed).
public static readonly Counter ToolInvocations =
Meter.CreateCounter("redb.route.llm.tool.invocations", "calls",
"Number of tool invocations performed by the agent.");
/// Tool invocation failures.
public static readonly Counter ToolFailures =
Meter.CreateCounter("redb.route.llm.tool.failures", "calls",
"Number of tool invocations that threw an exception.");
/// Tool-cache reads that returned a live (non-expired) cached output. Tag llm.tool.name when known.
public static readonly Counter ToolCacheHits =
Meter.CreateCounter("redb.route.llm.tool_cache.hits", "reads",
"Tool-cache reads that returned a live cached output.");
/// Tool-cache reads that found no row (cold key).
public static readonly Counter ToolCacheMisses =
Meter.CreateCounter("redb.route.llm.tool_cache.misses", "reads",
"Tool-cache reads that found no row.");}