Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -528,6 +528,14 @@ private void TraceResponse(
AddMetricTags(ref tags, requestModelId, response);
_tokenUsageHistogram.Record((int)outputTokens, tags);
}

if (usage.CachedInputTokenCount is long cachedInputTokens)
{
TagList tags = default;
tags.Add(OpenTelemetryConsts.GenAI.Token.Type, OpenTelemetryConsts.TokenTypeCacheRead);
AddMetricTags(ref tags, requestModelId, response);
_tokenUsageHistogram.Record((int)cachedInputTokens, tags);
}
}

if (error is not null)
Expand Down Expand Up @@ -570,6 +578,11 @@ private void TraceResponse(
_ = activity.AddTag(OpenTelemetryConsts.GenAI.Usage.OutputTokens, (int)outputTokens);
}

if (response.Usage?.CachedInputTokenCount is long cachedInputTokens)
{
_ = activity.AddTag(OpenTelemetryConsts.GenAI.Usage.CacheReadInputTokens, (int)cachedInputTokens);
}

// Log all additional response properties as raw values on the span.
// Since AdditionalProperties has undefined meaning, we treat it as potentially sensitive data.
if (EnableSensitiveData && response.AdditionalProperties is { } props)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ internal static class OpenTelemetryConsts

public const string TokenTypeInput = "input";
public const string TokenTypeOutput = "output";
public const string TokenTypeCacheRead = "cache_read";
Comment thread
stephentoub marked this conversation as resolved.
Outdated

public static class Error
{
Expand Down Expand Up @@ -138,6 +139,7 @@ public static class Usage
{
public const string InputTokens = "gen_ai.usage.input_tokens";
public const string OutputTokens = "gen_ai.usage.output_tokens";
public const string CacheReadInputTokens = "gen_ai.usage.cache_read_input_tokens";
Comment thread
stephentoub marked this conversation as resolved.
Outdated
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ public async Task ExpectedInformationLogged_Async(bool enableSensitiveData, bool
InputTokenCount = 10,
OutputTokenCount = 20,
TotalTokenCount = 42,
CachedInputTokenCount = 5,
},
AdditionalProperties = new()
{
Expand Down Expand Up @@ -84,6 +85,7 @@ async static IAsyncEnumerable<ChatResponseUpdate> CallbackAsync(
InputTokenCount = 10,
OutputTokenCount = 20,
TotalTokenCount = 42,
CachedInputTokenCount = 5,
})],
AdditionalProperties = new()
{
Expand Down Expand Up @@ -180,6 +182,7 @@ async static IAsyncEnumerable<ChatResponseUpdate> CallbackAsync(
Assert.Equal("""["stop"]""", activity.GetTagItem("gen_ai.response.finish_reasons"));
Assert.Equal(10, activity.GetTagItem("gen_ai.usage.input_tokens"));
Assert.Equal(20, activity.GetTagItem("gen_ai.usage.output_tokens"));
Assert.Equal(5, activity.GetTagItem("gen_ai.usage.cache_read_input_tokens"));
Assert.Equal(enableSensitiveData ? "abcdefgh" : null, activity.GetTagItem("system_fingerprint"));
Assert.Equal(enableSensitiveData ? "value2" : null, activity.GetTagItem("AndSomethingElse"));

Expand Down
Loading