diff --git a/packages/junior/src/chat/logging.ts b/packages/junior/src/chat/logging.ts index 040e0277..93659ba7 100644 --- a/packages/junior/src/chat/logging.ts +++ b/packages/junior/src/chat/logging.ts @@ -1835,8 +1835,9 @@ export function extractGenAiUsageAttributes( Record< | "gen_ai.usage.input_tokens" | "gen_ai.usage.output_tokens" - | "gen_ai.usage.cache_read.input_tokens" - | "gen_ai.usage.cache_creation.input_tokens", + | "gen_ai.usage.total_tokens" + | "gen_ai.usage.input_tokens.cached" + | "gen_ai.usage.input_tokens.cache_write", number > > { @@ -1847,6 +1848,7 @@ export function extractGenAiUsageAttributes( cachedInputTokens, cacheCreationTokens, ); + const totalTokens = sumTokenCounts(semanticInputTokens, outputTokens); return { ...(semanticInputTokens !== undefined @@ -1855,11 +1857,14 @@ export function extractGenAiUsageAttributes( ...(outputTokens !== undefined ? { "gen_ai.usage.output_tokens": outputTokens } : {}), + ...(totalTokens !== undefined + ? { "gen_ai.usage.total_tokens": totalTokens } + : {}), ...(cachedInputTokens !== undefined - ? { "gen_ai.usage.cache_read.input_tokens": cachedInputTokens } + ? { "gen_ai.usage.input_tokens.cached": cachedInputTokens } : {}), ...(cacheCreationTokens !== undefined - ? { "gen_ai.usage.cache_creation.input_tokens": cacheCreationTokens } + ? { "gen_ai.usage.input_tokens.cache_write": cacheCreationTokens } : {}), }; } diff --git a/packages/junior/tests/unit/logging/extract-gen-ai-usage-summary.test.ts b/packages/junior/tests/unit/logging/extract-gen-ai-usage-summary.test.ts index ab9d7c3c..8081c6f3 100644 --- a/packages/junior/tests/unit/logging/extract-gen-ai-usage-summary.test.ts +++ b/packages/junior/tests/unit/logging/extract-gen-ai-usage-summary.test.ts @@ -100,7 +100,7 @@ describe("extractGenAiUsageSummary", () => { }); }); - it("maps cache token counters to current GenAI semantic attributes", () => { + it("maps cache token counters to Sentry convention attribute names", () => { expect( extractGenAiUsageAttributes({ inputTokens: 10, @@ -111,8 +111,9 @@ describe("extractGenAiUsageSummary", () => { ).toEqual({ "gen_ai.usage.input_tokens": 15, "gen_ai.usage.output_tokens": 2, - "gen_ai.usage.cache_read.input_tokens": 4, - "gen_ai.usage.cache_creation.input_tokens": 1, + "gen_ai.usage.total_tokens": 17, + "gen_ai.usage.input_tokens.cached": 4, + "gen_ai.usage.input_tokens.cache_write": 1, }); }); }); diff --git a/specs/logging/semantics.md b/specs/logging/semantics.md index 916f0e57..02825f38 100644 --- a/specs/logging/semantics.md +++ b/specs/logging/semantics.md @@ -79,8 +79,9 @@ This file is the canonical attribute and naming map for instrumentation in this - `gen_ai.output.messages` (when captured) - `gen_ai.usage.input_tokens` (when available) - `gen_ai.usage.output_tokens` (when available) -- `gen_ai.usage.cache_read.input_tokens` (when available) -- `gen_ai.usage.cache_creation.input_tokens` (when available) +- `gen_ai.usage.total_tokens` (when available) +- `gen_ai.usage.input_tokens.cached` (when available; Sentry convention) +- `gen_ai.usage.input_tokens.cache_write` (when available; Sentry convention) - `gen_ai.tool.description` (when available) - `gen_ai.tool.name` (for `execute_tool`) - `gen_ai.tool.call.id` (when available) diff --git a/specs/logging/tracing-spec.md b/specs/logging/tracing-spec.md index 93430aef..a14dd9fc 100644 --- a/specs/logging/tracing-spec.md +++ b/specs/logging/tracing-spec.md @@ -77,8 +77,8 @@ Define the canonical tracing contract for span naming, boundaries, attributes, a - `gen_ai.response.finish_reasons` when available from provider responses. - `gen_ai.system_instructions` when provided separately from chat history and safely captured. - `gen_ai.input.messages` / `gen_ai.output.messages` when safely captured. -- `gen_ai.usage.input_tokens` / `gen_ai.usage.output_tokens` when available from provider responses. -- `gen_ai.usage.cache_read.input_tokens` / `gen_ai.usage.cache_creation.input_tokens` when available from provider responses. +- `gen_ai.usage.input_tokens` / `gen_ai.usage.output_tokens` / `gen_ai.usage.total_tokens` when available from provider responses. +- `gen_ai.usage.input_tokens.cached` / `gen_ai.usage.input_tokens.cache_write` when available from provider responses (Sentry convention names; required for AI Monitoring UI visibility). - `gen_ai.tool.description` when available on tool execution spans. - `gen_ai.tool.call.arguments` / `gen_ai.tool.call.result` on tool execution spans when captured. - Keep existing context keys aligned with `packages/junior/src/chat/logging.ts`.