Skip to content

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

fix(otel): recognize OpenInference llm.token_count.prompt_details.cache_read/cache_write (#13572)

gragragrabc04849802427
diff --git a/packages/shared/src/server/otel/OtelIngestionProcessor.ts b/packages/shared/src/server/otel/OtelIngestionProcessor.ts
index e504570482..a0ad4d11e3 100644
--- a/packages/shared/src/server/otel/OtelIngestionProcessor.ts
+++ b/packages/shared/src/server/otel/OtelIngestionProcessor.ts
@@ -2505,12 +2505,14 @@ export class OtelIngestionProcessor {
rawUsageDetails["cache_read.input_tokens"] ??
rawUsageDetails["cache_read_tokens"] ??
rawUsageDetails["details.cache_read_tokens"] ??
- rawUsageDetails["details.cache_read_input_tokens"];
+ rawUsageDetails["details.cache_read_input_tokens"] ??
+ rawUsageDetails["prompt_details.cache_read"];
const cacheCreationTokens =
rawUsageDetails["cache_creation.input_tokens"] ??
rawUsageDetails["cache_write_tokens"] ??
rawUsageDetails["details.cache_write_tokens"] ??
- rawUsageDetails["details.cache_creation_input_tokens"];
+ rawUsageDetails["details.cache_creation_input_tokens"] ??
+ rawUsageDetails["prompt_details.cache_write"];
const normalizedUsageDetails = Object.entries(rawUsageDetails).reduce(
(acc: Record<string, number>, [key, value]) => {
@@ -2528,10 +2530,12 @@ export class OtelIngestionProcessor {
"cache_read_tokens",
"details.cache_read_tokens",
"details.cache_read_input_tokens",
+ "prompt_details.cache_read",
"cache_creation.input_tokens",
"cache_write_tokens",
"details.cache_write_tokens",
"details.cache_creation_input_tokens",
+ "prompt_details.cache_write",
].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 ad9006f90d..d27f5f3893 100644
--- a/web/src/__tests__/server/api/otel/otelMapping.servertest.ts
+++ b/web/src/__tests__/server/api/otel/otelMapping.servertest.ts
@@ -1985,6 +1985,111 @@ describe("OTel Resource Span Mapping", () => {
expect(observationEvent?.body.usageDetails.output_audio_tokens).toBe(7);
});
+ it("should extract OpenInference llm.token_count.prompt_details.cache_read/cache_write into input_cached_tokens / input_cache_creation", async () => {
+ // OpenInference semantic conventions (used by openinference-instrumentation-{openai,anthropic,agno,...})
+ // emit cache token counts under llm.token_count.prompt_details.cache_read and
+ // llm.token_count.prompt_details.cache_write. Langfuse must recognize these
+ // canonical names alongside the existing details.cache_read_* / cache_creation_* aliases.
+ const traceId = "abcdef0123456789abcdef0123456789";
+
+ const openinferenceCacheSpan = {
+ resource: {
+ attributes: [
+ {
+ key: "telemetry.sdk.language",
+ value: { stringValue: "python" },
+ },
+ {
+ key: "service.name",
+ value: { stringValue: "test-agno" },
+ },
+ ],
+ },
+ scopeSpans: [
+ {
+ scope: {
+ name: "openinference.instrumentation.agno",
+ version: "0.1.27",
+ attributes: [],
+ },
+ spans: [
+ {
+ traceId: Buffer.from(traceId, "hex"),
+ spanId: Buffer.from("a1b2c3d4e5f60718", "hex"),
+ name: "openinference-cache-test",
+ kind: 1,
+ startTimeUnixNano: {
+ low: 1000000,
+ high: 406528574,
+ unsigned: true,
+ },
+ endTimeUnixNano: {
+ low: 2000000,
+ high: 406528574,
+ unsigned: true,
+ },
+ attributes: [
+ {
+ key: "llm.token_count.prompt",
+ value: {
+ intValue: { low: 50000, high: 0, unsigned: false },
+ },
+ },
+ {
+ key: "llm.token_count.completion",
+ value: {
+ intValue: { low: 200, high: 0, unsigned: false },
+ },
+ },
+ {
+ key: "llm.token_count.prompt_details.cache_read",
+ value: {
+ intValue: { low: 40000, high: 0, unsigned: false },
+ },
+ },
+ {
+ key: "llm.token_count.prompt_details.cache_write",
+ value: {
+ intValue: { low: 5000, high: 0, unsigned: false },
+ },
+ },
+ ],
+ events: [],
+ status: { code: 1 },
+ },
+ ],
+ },
+ ],
+ };
+
+ const events = await convertOtelSpanToIngestionEvent(
+ openinferenceCacheSpan,
+ new Set(),
+ );
+
+ const observationEvent = events.find((e) => e.type === "span-create");
+
+ expect(observationEvent).toBeDefined();
+ // input must be the uncached remainder: prompt - cache_read - cache_write
+ // = 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,
+ );
+ // The raw OpenInference keys must NOT be passed through after normalization
+ // (otherwise they would double-count in the UI breakdown).
+ expect(
+ observationEvent?.body.usageDetails["prompt_details.cache_read"],
+ ).toBeUndefined();
+ expect(
+ observationEvent?.body.usageDetails["prompt_details.cache_write"],
+ ).toBeUndefined();
+ });
+
it("should prepend gen_ai.system_instructions to pydantic_ai.all_messages input when system message is absent", async () => {
const traceId = "9d7aa9a729def1eadc0b2063ca4ebeb4";