Skip to content

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

chore(worker): record LLM-as-a-judge egress on export volume metric (#14627)

Niklas Semmlerf3625434ceb0
diff --git a/worker/src/__tests__/exportVolumeMetric.test.ts b/worker/src/__tests__/exportVolumeMetric.test.ts
index a2bd557c49..bc5e85b352 100644
--- a/worker/src/__tests__/exportVolumeMetric.test.ts
+++ b/worker/src/__tests__/exportVolumeMetric.test.ts
@@ -56,4 +56,21 @@ describe("recordExportVolume", () => {
expect(tags).not.toHaveProperty("source");
expect(tags).not.toHaveProperty("table");
});
+
+ it("emits llmaj egress with only integration/projectId tags", () => {
+ recordExportVolume({
+ integration: "llmaj",
+ bytes: 5120,
+ projectId: "p4",
+ });
+ expect(recordIncrement).toHaveBeenCalledWith(EXPORT_VOLUME_METRIC, 5120, {
+ integration: "llmaj",
+ projectId: "p4",
+ });
+ const tags = recordIncrement.mock.calls[0][2];
+ expect(tags).not.toHaveProperty("destination_type");
+ expect(tags).not.toHaveProperty("source");
+ expect(tags).not.toHaveProperty("table");
+ expect(tags).not.toHaveProperty("path");
+ });
});
diff --git a/worker/src/features/evaluation/__tests__/evalExecutionDeps.test.ts b/worker/src/features/evaluation/__tests__/evalExecutionDeps.test.ts
index a329be1f42..2fd711cb44 100644
--- a/worker/src/features/evaluation/__tests__/evalExecutionDeps.test.ts
+++ b/worker/src/features/evaluation/__tests__/evalExecutionDeps.test.ts
@@ -1,8 +1,11 @@
import { describe, expect, it, vi, beforeEach } from "vitest";
+import { z } from "zod";
import { createProductionEvalExecutionDeps } from "../evalExecutionDeps";
+import { EXPORT_VOLUME_METRIC } from "../../../services/exportVolumeMetric";
-const { mockFetchLLMCompletion } = vi.hoisted(() => ({
+const { mockFetchLLMCompletion, mockRecordIncrement } = vi.hoisted(() => ({
mockFetchLLMCompletion: vi.fn(),
+ mockRecordIncrement: vi.fn(),
}));
vi.mock("@langfuse/shared/src/server", async (importOriginal) => {
@@ -11,6 +14,7 @@ vi.mock("@langfuse/shared/src/server", async (importOriginal) => {
return {
...original,
fetchLLMCompletion: mockFetchLLMCompletion,
+ recordIncrement: mockRecordIncrement,
};
});
@@ -76,4 +80,58 @@ describe("createProductionEvalExecutionDeps", () => {
}),
);
});
+
+ it("records llmaj export volume using the schema's JSON Schema form", async () => {
+ const deps = createProductionEvalExecutionDeps();
+
+ const messages = [
+ { role: "user", type: "user", content: "Judge this answer" },
+ ];
+ // Production passes a Zod schema, not a plain object.
+ const structuredOutputSchema = z.object({
+ reasoning: z.string().describe("why this score was given"),
+ score: z.number().describe("score between 0 and 1"),
+ });
+
+ await deps.callLLM({
+ messages: messages as any,
+ modelConfig: {
+ provider: "openai",
+ model: "gpt-4.1",
+ apiKey: { adapter: "openai", secretKey: "secret" },
+ adapter: "openai" as any,
+ modelParams: {},
+ },
+ structuredOutputSchema: structuredOutputSchema as any,
+ traceSinkParams: {
+ targetProjectId: "project-123",
+ traceId: "trace-123",
+ traceName: "Judge trace",
+ environment: "langfuse-llm-as-a-judge",
+ metadata: {},
+ },
+ });
+
+ const expectedBytes =
+ Buffer.byteLength(JSON.stringify(messages), "utf8") +
+ Buffer.byteLength(
+ JSON.stringify(z.toJSONSchema(structuredOutputSchema)),
+ "utf8",
+ );
+
+ expect(expectedBytes).toBeGreaterThan(0);
+ expect(mockRecordIncrement).toHaveBeenCalledWith(
+ EXPORT_VOLUME_METRIC,
+ expectedBytes,
+ { integration: "llmaj", projectId: "project-123" },
+ );
+ // Not the Zod _def form.
+ const zodDefBytes = Buffer.byteLength(
+ JSON.stringify(structuredOutputSchema),
+ "utf8",
+ );
+ expect(expectedBytes).not.toBe(
+ Buffer.byteLength(JSON.stringify(messages), "utf8") + zodDefBytes,
+ );
+ });
});
diff --git a/worker/src/features/evaluation/evalExecutionDeps.ts b/worker/src/features/evaluation/evalExecutionDeps.ts
index 982bcd0ee1..f5ef61d8da 100644
--- a/worker/src/features/evaluation/evalExecutionDeps.ts
+++ b/worker/src/features/evaluation/evalExecutionDeps.ts
@@ -1,4 +1,5 @@
import { randomUUID } from "crypto";
+import { z } from "zod";
import { JobExecutionStatus } from "@prisma/client";
import { prisma } from "@langfuse/shared/src/db";
import {
@@ -13,6 +14,7 @@ import {
import { buildEvalMessages } from "./evalRuntime";
import { getEvalS3StorageClient } from "./s3StorageClient";
import { createInternalEventsWriter } from "../internal-tracing/createInternalEventsWriter";
+import { recordExportVolume } from "../../services/exportVolumeMetric";
type StructuredOutputSchema = NonNullable<
Parameters<typeof fetchLLMCompletion>[0]["structuredOutputSchema"]
@@ -130,6 +132,15 @@ export interface EvalExecutionDeps {
) => Promise<ModelConfigResult>;
}
+// Measure the schema as the JSON Schema LangChain ships, not Zod's _def.
+function serializeSchemaForEgress(schema: unknown): string {
+ try {
+ return JSON.stringify(z.toJSONSchema(schema as z.ZodType));
+ } catch {
+ return JSON.stringify(schema);
+ }
+}
+
/**
* Creates the production implementation of eval execution dependencies.
* This is the default implementation used in production code.
@@ -202,7 +213,17 @@ export function createProductionEvalExecutionDeps(): EvalExecutionDeps {
typeof fetchLLMCompletion
>[0]["modelParams"]["adapter"];
- return fetchLLMCompletion({
+ // llmaj egress: serialized request body (messages + schema), uncompressed.
+ const bytes =
+ Buffer.byteLength(JSON.stringify(params.messages), "utf8") +
+ (params.structuredOutputSchema
+ ? Buffer.byteLength(
+ serializeSchemaForEgress(params.structuredOutputSchema),
+ "utf8",
+ )
+ : 0);
+
+ const result = await fetchLLMCompletion({
streaming: false,
llmConnection,
messages: params.messages,
@@ -223,6 +244,15 @@ export function createProductionEvalExecutionDeps(): EvalExecutionDeps {
eventsWriter: createInternalEventsWriter(),
},
});
+
+ // Record only after a successful send, like the other integrations.
+ recordExportVolume({
+ integration: "llmaj",
+ bytes,
+ projectId: params.traceSinkParams.targetProjectId,
+ });
+
+ return result;
},
fetchModelConfig: async ({ projectId, provider, model, modelParams }) => {
diff --git a/worker/src/services/exportVolumeMetric.ts b/worker/src/services/exportVolumeMetric.ts
index c31dcc369f..b5bccb014a 100644
--- a/worker/src/services/exportVolumeMetric.ts
+++ b/worker/src/services/exportVolumeMetric.ts
@@ -2,11 +2,16 @@ import { recordIncrement } from "@langfuse/shared/src/server";
export const EXPORT_VOLUME_METRIC = "langfuse.export.serialized_bytes";
-export type ExportIntegration = "blob_storage" | "posthog" | "mixpanel";
+export type ExportIntegration =
+ | "blob_storage"
+ | "posthog"
+ | "mixpanel"
+ | "llmaj";
type ExportVolume = {
integration: ExportIntegration;
- // Gzipped on-wire bytes the integration shipped this run.
+ // On-wire egress bytes shipped this run: gzipped for blob/posthog/mixpanel,
+ // uncompressed for llmaj (LLM requests aren't gzipped).
bytes: number;
projectId: string;
// Egress-cost classification; blob only (S3 / S3_COMPATIBLE / AZURE_*).