🪢 Open source agent evals & observability: Trace, evaluate, and improve LLM applications with one open platform.
TypeScript analyticsautogenevaluationlangchainlarge-language-modelsllama-indexllmllm-evaluationllm-observabilityllmopsmonitoringobservabilityopen-sourceopenaiplaygroundprompt-engineeringprompt-managementself-hostedycombinator
fix(otel): normalize official cache-write usage (#17129)
Hassieb Pakzadddf44348a3fc
diff --git a/packages/shared/src/server/otel/OtelIngestionProcessor.ts b/packages/shared/src/server/otel/OtelIngestionProcessor.tsindex d93dedfbd9..c5b25da912 100644--- a/packages/shared/src/server/otel/OtelIngestionProcessor.ts+++ b/packages/shared/src/server/otel/OtelIngestionProcessor.ts@@ -3065,6 +3065,7 @@ export class OtelIngestionProcessor {rawUsageDetails["input_cached_tokens"];const cacheCreationTokens =rawUsageDetails["cache_creation.input_tokens"] ??+ rawUsageDetails["cache_write.input_tokens"] ??rawUsageDetails["cache_creation_input_tokens"] ??rawUsageDetails["cache_write_tokens"] ??rawUsageDetails["details.cache_write_tokens"] ??@@ -3098,6 +3099,7 @@ export class OtelIngestionProcessor {"prompt_details.cache_read","input_cached_tokens","cache_creation.input_tokens",+ "cache_write.input_tokens","cache_creation_input_tokens","cache_write_tokens","details.cache_write_tokens",diff --git a/web/src/__tests__/server/api/otel/otelMapping.servertest.ts b/web/src/__tests__/server/api/otel/otelMapping.servertest.tsindex d105411aaf..681986e74c 100644--- a/web/src/__tests__/server/api/otel/otelMapping.servertest.ts+++ b/web/src/__tests__/server/api/otel/otelMapping.servertest.ts@@ -8109,6 +8109,77 @@ describe("OTel Resource Span Mapping", () => {expect(usageDetails.output_reasoning_tokens).toBe(25);expect(usageDetails["reasoning.output_tokens"]).toBeUndefined();});++ it("should normalize the official cache-write token attribute", async () => {+ const traceId = "abcdef1234567890abcdef1234567893";+ const officialCacheUsageSpan = {+ resource: {+ attributes: [+ {+ key: "service.name",+ value: { stringValue: "test-service" },+ },+ ],+ },+ scopeSpans: [+ {+ scope: {+ name: "opentelemetry",+ version: "1.0.0",+ },+ spans: [+ {+ traceId: Buffer.from(traceId, "hex"),+ spanId: Buffer.from("1234567890abcde2", "hex"),+ name: "official-cache-usage",+ kind: 1,+ startTimeUnixNano: {+ low: 1000000,+ high: 406528574,+ unsigned: true,+ },+ endTimeUnixNano: {+ low: 2000000,+ high: 406528574,+ unsigned: true,+ },+ attributes: [+ {+ key: "gen_ai.usage.input_tokens",+ value: { intValue: { low: 300, high: 0, unsigned: false } },+ },+ {+ key: "gen_ai.usage.cache_read.input_tokens",+ value: { intValue: { low: 40, high: 0, unsigned: false } },+ },+ {+ key: "gen_ai.usage.cache_write.input_tokens",+ value: { intValue: { low: 25, high: 0, unsigned: false } },+ },+ ],+ status: {},+ },+ ],+ },+ ],+ };++ const events = await convertOtelSpanToIngestionEvent(+ officialCacheUsageSpan,+ new Set(),+ );+ const observationEvent = events.find(+ (event) =>+ event.type === "generation-create" || event.type === "span-create",+ );++ expect(observationEvent?.body.usageDetails).toEqual({+ input: 235,+ input_cached_tokens: 40,+ input_cache_creation: 25,+ });+ });+it("should normalize raw Anthropic cache_read_input_tokens / cache_creation_input_tokens into Langfuse canonical keys", async () => {// flat Anthropic cache spellings must map to cache aliases, not opaque passthrough bucketsconst traceId = "abcdef1234567890abcdef1234567893";diff --git a/worker/src/services/IngestionService/tests/calculateTokenCost.unit.test.ts b/worker/src/services/IngestionService/tests/calculateTokenCost.unit.test.tsindex 59b038e21a..ab65d01760 100644--- a/worker/src/services/IngestionService/tests/calculateTokenCost.unit.test.ts+++ b/worker/src/services/IngestionService/tests/calculateTokenCost.unit.test.ts@@ -190,6 +190,22 @@ describe("Token Cost Calculation", () => {expect(costs.total_cost).toBe(9.0);});+ it("should apply the configured cache-creation price", () => {+ const costs = (IngestionService as any).calculateUsageCosts(+ [+ {+ price: new Decimal(0.00000375),+ usageType: "input_cache_creation",+ },+ ],+ { provided_cost_details: {} },+ { input_cache_creation: 25 },+ );++ expect(costs.cost_details.input_cache_creation).toBe(0.00009375);+ expect(costs.total_cost).toBe(0.00009375);+ });+it("should correctly calculate token costs with user provided costs", async () => {const prices = await prisma.price.findMany({where: {