Skip to content

🪢 Open source agent evals & observability: Trace, evaluate, and improve LLM applications with one open platform.

fix(otel): treat gen_ai.usage.input_cached_tokens as cache-read alias (LFE-10765) (#14902)

Niklas Semmler8c1f23ad2a48
diff --git a/packages/shared/src/server/otel/OtelIngestionProcessor.ts b/packages/shared/src/server/otel/OtelIngestionProcessor.ts
index 548b7c81c8..5f10c9f275 100644
--- a/packages/shared/src/server/otel/OtelIngestionProcessor.ts
+++ b/packages/shared/src/server/otel/OtelIngestionProcessor.ts
@@ -2581,13 +2581,15 @@ export class OtelIngestionProcessor {
rawUsageDetails["cache_read_tokens"] ??
rawUsageDetails["details.cache_read_tokens"] ??
rawUsageDetails["details.cache_read_input_tokens"] ??
- rawUsageDetails["prompt_details.cache_read"];
+ rawUsageDetails["prompt_details.cache_read"] ??
+ rawUsageDetails["input_cached_tokens"];
const cacheCreationTokens =
rawUsageDetails["cache_creation.input_tokens"] ??
rawUsageDetails["cache_write_tokens"] ??
rawUsageDetails["details.cache_write_tokens"] ??
rawUsageDetails["details.cache_creation_input_tokens"] ??
- rawUsageDetails["prompt_details.cache_write"];
+ rawUsageDetails["prompt_details.cache_write"] ??
+ rawUsageDetails["input_cache_creation"];
const normalizedUsageDetails = Object.entries(rawUsageDetails).reduce(
(acc: Record<string, number>, [key, value]) => {
@@ -2606,11 +2608,13 @@ export class OtelIngestionProcessor {
"details.cache_read_tokens",
"details.cache_read_input_tokens",
"prompt_details.cache_read",
+ "input_cached_tokens",
"cache_creation.input_tokens",
"cache_write_tokens",
"details.cache_write_tokens",
"details.cache_creation_input_tokens",
"prompt_details.cache_write",
+ "input_cache_creation",
].includes(key)
) {
return acc;
diff --git a/web/src/__tests__/server/api/otel/otelMapping.servertest.ts b/web/src/__tests__/server/api/otel/otelMapping.servertest.ts
index bb7d165fd5..daeff6b933 100644
--- a/web/src/__tests__/server/api/otel/otelMapping.servertest.ts
+++ b/web/src/__tests__/server/api/otel/otelMapping.servertest.ts
@@ -2266,6 +2266,101 @@ describe("OTel Resource Span Mapping", () => {
).toBeUndefined();
});
+ it("should subtract gen_ai.usage.input_cached_tokens / input_cache_creation from inclusive input tokens", async () => {
+ // Integrators reading the Langfuse docs emit Langfuse's canonical usage
+ // keys through the gen_ai.usage.* namespace. They must be treated as
+ // cache aliases like gen_ai.usage.cache_read.input_tokens, otherwise the
+ // inclusive input stays unreduced and cached tokens count twice
+ // (github.com/langfuse/langfuse/issues/12306).
+ const traceId = "abcdef1234567890abcdef1234567892";
+
+ const langfuseKeysSpan = {
+ resource: {
+ attributes: [
+ {
+ key: "telemetry.sdk.language",
+ value: { stringValue: "python" },
+ },
+ {
+ key: "service.name",
+ value: { stringValue: "test-service" },
+ },
+ ],
+ },
+ scopeSpans: [
+ {
+ scope: {
+ name: "custom-instrumentation",
+ version: "1.0.0",
+ attributes: [],
+ },
+ spans: [
+ {
+ traceId: Buffer.from(traceId, "hex"),
+ spanId: Buffer.from("80854cd6bd218bf7", "hex"),
+ name: "langfuse-usage-keys-cache-test",
+ 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: 50000, high: 0, unsigned: false },
+ },
+ },
+ {
+ key: "gen_ai.usage.output_tokens",
+ value: { intValue: { low: 200, high: 0, unsigned: false } },
+ },
+ {
+ key: "gen_ai.usage.input_cached_tokens",
+ value: {
+ intValue: { low: 40000, high: 0, unsigned: false },
+ },
+ },
+ {
+ key: "gen_ai.usage.input_cache_creation",
+ value: {
+ intValue: { low: 5000, high: 0, unsigned: false },
+ },
+ },
+ ],
+ events: [],
+ status: { code: 1 },
+ },
+ ],
+ },
+ ],
+ };
+
+ const events = await convertOtelSpanToIngestionEvent(
+ langfuseKeysSpan,
+ new Set(),
+ );
+
+ const observationEvent = events.find((e) => e.type === "span-create");
+
+ expect(observationEvent).toBeDefined();
+ // input must be the uncached remainder: 50000 - 40000 - 5000 = 5000
+ expect(observationEvent?.body.usageDetails.input).toBe(5000);
+ expect(observationEvent?.body.usageDetails.output).toBe(200);
+ expect(observationEvent?.body.usageDetails.input_cached_tokens).toBe(
+ 40000,
+ );
+ expect(observationEvent?.body.usageDetails.input_cache_creation).toBe(
+ 5000,
+ );
+ });
+
it("should prepend gen_ai.system_instructions to pydantic_ai.all_messages input when system message is absent", async () => {
const traceId = "9d7aa9a729def1eadc0b2063ca4ebeb4";