Skip to content

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

feat: allow LLM-as-a-judge to filter by tool names and tool call count (#12799)

Tobias Wochinger459510955175
diff --git a/packages/shared/src/features/evals/observationForEval.ts b/packages/shared/src/features/evals/observationForEval.ts
index 0c7ac4e6e5..e0c302f718 100644
--- a/packages/shared/src/features/evals/observationForEval.ts
+++ b/packages/shared/src/features/evals/observationForEval.ts
@@ -54,6 +54,7 @@ export const observationForEvalSchema = z.object({
tool_definitions: z.record(z.string(), z.unknown()).default({}),
tool_calls: z.array(z.unknown()).default([]),
tool_call_names: z.array(z.string()).default([]),
+ tool_call_count: z.number().default(0),
// Experiment
experiment_id: z.string().nullish(),
@@ -80,9 +81,11 @@ export function convertEventRecordToObservationForEval(
record.metadata_values,
);
+ const toolCallNames = record.tool_call_names ?? [];
return observationForEvalSchema.parse({
...record,
metadata,
+ tool_call_count: toolCallNames.length,
});
}
@@ -102,6 +105,8 @@ export type ObservationEvalFilterColumnInternal =
| "experiment_dataset_id"
| "metadata"
| "parent_span_id"
+ | "tool_call_names"
+ | "tool_call_count"
>;
export type ObservationEvalMappingColumnInternal = keyof Pick<
@@ -159,7 +164,7 @@ export const observationEvalVariableColumns: ObservationEvalVariableColumn[] = [
export const availableObservationEvalVariableColumns = [
...observationEvalVariableColumns,
{
- id: "toolCalls",
+ id: "toolCalls", // Needs to match the `ID` from `mapObservationsTable.ts`
name: "Tool Calls",
description: "Tool calls",
internal: "tool_calls",
@@ -290,6 +295,21 @@ export const observationEvalFilterColumns: ObservationEvalColumnDef[] = [
internal: "parent_span_id",
nullable: true,
},
+ {
+ name: "Called Tool Names",
+ id: "calledToolNames",
+ type: "arrayOptions",
+ internal: "tool_call_names",
+ options: [], // to be filled at runtime
+ },
+ {
+ name: "Tool Call Count",
+ id: "toolCalls",
+ type: "number",
+ internal: "tool_call_count",
+ step: 1,
+ min: 0,
+ },
];
export const experimentEvalFilterColumns: ObservationEvalColumnDef[] = [
@@ -313,6 +333,7 @@ export type ObservationEvalOptions = {
tags?: Array<SingleValueOption>;
traceName?: Array<SingleValueOption>;
name?: Array<SingleValueOption>;
+ calledToolNames?: Array<SingleValueOption>;
};
export type ExperimentEvalOptions = {
@@ -336,6 +357,9 @@ export function observationEvalFilterColsWithOptions(
if (col.id === "name") {
return formatColumnOptions(col, options?.name ?? []);
}
+ if (col.id === "calledToolNames") {
+ return formatColumnOptions(col, options?.calledToolNames ?? []);
+ }
return col;
});
}
diff --git a/packages/shared/src/tableDefinitions/types.ts b/packages/shared/src/tableDefinitions/types.ts
index a48eeef951..03f850644e 100644
--- a/packages/shared/src/tableDefinitions/types.ts
+++ b/packages/shared/src/tableDefinitions/types.ts
@@ -30,6 +30,10 @@ export type ColumnDefinition =
type: "number" | "string" | "datetime" | "boolean" | "null";
internal: string;
nullable?: boolean;
+ /** Step for number inputs (e.g. 1 for integers). Defaults to 0.01 in UI. */
+ step?: number;
+ /** Minimum value for number inputs. */
+ min?: number;
}
| {
name: string;
diff --git a/web/src/features/evals/components/inner-evaluator-form.tsx b/web/src/features/evals/components/inner-evaluator-form.tsx
index f78aa0603f..7b88af4edc 100644
--- a/web/src/features/evals/components/inner-evaluator-form.tsx
+++ b/web/src/features/evals/components/inner-evaluator-form.tsx
@@ -1027,7 +1027,7 @@ export const InnerEvaluatorForm = (props: {
disabled={props.disabled}
columnsWithCustomSelect={
isEventTarget(target) || isTraceTarget(target)
- ? ["tags", "name"]
+ ? ["tags", "name", "calledToolNames"]
: undefined
}
/>
diff --git a/web/src/features/evals/hooks/useEvalConfigFilterOptions.ts b/web/src/features/evals/hooks/useEvalConfigFilterOptions.ts
index 79532198a7..e22889fdf9 100644
--- a/web/src/features/evals/hooks/useEvalConfigFilterOptions.ts
+++ b/web/src/features/evals/hooks/useEvalConfigFilterOptions.ts
@@ -106,6 +106,10 @@ export function useEvalConfigFilterOptions({
name: observationsFilterOptionsResponse.data?.name?.map((n) => ({
value: n.value,
})),
+ calledToolNames:
+ observationsFilterOptionsResponse.data?.calledToolNames?.map((t) => ({
+ value: t.value,
+ })),
};
}, [
traceFilterOptionsResponse.data,
diff --git a/web/src/features/filters/components/filter-builder.tsx b/web/src/features/filters/components/filter-builder.tsx
index abcb9ca3d0..221dae637a 100644
--- a/web/src/features/filters/components/filter-builder.tsx
+++ b/web/src/features/filters/components/filter-builder.tsx
@@ -871,7 +871,13 @@ function FilterBuilderForm({
value={filter.value ?? undefined}
disabled={disabled}
type="number"
- step="0.01"
+ step={
+ (column?.type === "number" && column.step) || 0.01
+ }
+ min={
+ column?.type === "number" ? column.min : undefined
+ }
+ placeholder="number"
lang="en-US"
onChange={(e) =>
handleFilterChange(
diff --git a/worker/src/features/batchAction/processBatchedObservationEval.test.ts b/worker/src/features/batchAction/processBatchedObservationEval.test.ts
index b2f03388a8..d66824346d 100644
--- a/worker/src/features/batchAction/processBatchedObservationEval.test.ts
+++ b/worker/src/features/batchAction/processBatchedObservationEval.test.ts
@@ -84,4 +84,84 @@ describe("processBatchedObservationEval", () => {
(prisma.batchAction.update as Mock).mock.calls.length,
).toBeGreaterThan(0);
});
+
+ it("derives tool_call_count from tool_call_names in batch records", async () => {
+ const projectId = "project-1";
+ const batchActionId = "batch-action-2";
+
+ const evaluators: ObservationEvalConfig[] = [
+ {
+ id: "config-1",
+ projectId,
+ filter: [],
+ sampling: { toNumber: () => 1 } as ObservationEvalConfig["sampling"],
+ evalTemplateId: "template-1",
+ scoreName: "quality",
+ targetObject: EvalTargetObject.EVENT,
+ variableMapping: [],
+ status: JobConfigState.ACTIVE,
+ blockedAt: null,
+ },
+ ];
+
+ const rowWithToolCalls: Record<string, unknown> = {
+ span_id: "obs-2",
+ trace_id: "trace-2",
+ project_id: projectId,
+ parent_span_id: null,
+ type: "GENERATION",
+ name: "tool-test",
+ usage_details: {},
+ cost_details: {},
+ provided_usage_details: {},
+ provided_cost_details: {},
+ tags: [],
+ input: "input",
+ output: "output",
+ metadata: {},
+ tool_call_names: ["search", "calculator", "fetch"],
+ };
+
+ const rowWithoutToolCalls: Record<string, unknown> = {
+ span_id: "obs-3",
+ trace_id: "trace-3",
+ project_id: projectId,
+ parent_span_id: null,
+ type: "GENERATION",
+ name: "no-tool-test",
+ usage_details: {},
+ cost_details: {},
+ provided_usage_details: {},
+ provided_cost_details: {},
+ tags: [],
+ input: "input",
+ output: "output",
+ metadata: {},
+ };
+
+ const observationStream = (async function* () {
+ yield rowWithToolCalls;
+ yield rowWithoutToolCalls;
+ })();
+
+ await processBatchedObservationEval({
+ projectId,
+ batchActionId,
+ evaluators,
+ observationStream,
+ });
+
+ expect(scheduleObservationEvals).toHaveBeenCalledTimes(2);
+
+ const firstCall = (scheduleObservationEvals as Mock).mock.calls[0][0];
+ expect(firstCall.observation.tool_call_count).toBe(3);
+ expect(firstCall.observation.tool_call_names).toEqual([
+ "search",
+ "calculator",
+ "fetch",
+ ]);
+
+ const secondCall = (scheduleObservationEvals as Mock).mock.calls[1][0];
+ expect(secondCall.observation.tool_call_count).toBe(0);
+ });
});
diff --git a/worker/src/features/batchAction/processBatchedObservationEval.ts b/worker/src/features/batchAction/processBatchedObservationEval.ts
index 20458dbe40..7c2165725e 100644
--- a/worker/src/features/batchAction/processBatchedObservationEval.ts
+++ b/worker/src/features/batchAction/processBatchedObservationEval.ts
@@ -44,7 +44,13 @@ export async function processBatchedObservationEval(params: {
const results = await Promise.allSettled(
batch.map((record) =>
limit(async () => {
- const observation = observationForEvalSchema.parse(record);
+ const toolCallNames = Array.isArray(record.tool_call_names)
+ ? record.tool_call_names
+ : [];
+ const observation = observationForEvalSchema.parse({
+ ...record,
+ tool_call_count: toolCallNames.length,
+ });
await scheduleObservationEvals({
observation,
configs: evaluators,
diff --git a/worker/src/features/evaluation/observationEval/__tests__/extractObservationVariables.test.ts b/worker/src/features/evaluation/observationEval/__tests__/extractObservationVariables.test.ts
index cc0b9c3c74..312b9641b8 100644
--- a/worker/src/features/evaluation/observationEval/__tests__/extractObservationVariables.test.ts
+++ b/worker/src/features/evaluation/observationEval/__tests__/extractObservationVariables.test.ts
@@ -43,6 +43,7 @@ describe("extractObservationVariables", () => {
tool_definitions: { search: '{"description": "Search the web"}' },
tool_calls: ['{"name": "search", "args": {"query": "test"}}'],
tool_call_names: ["search"],
+ tool_call_count: 1,
// Usage & Cost
usage_details: { input: 100, output: 50 },
diff --git a/worker/src/features/evaluation/observationEval/__tests__/filterEvaluation.test.ts b/worker/src/features/evaluation/observationEval/__tests__/filterEvaluation.test.ts
index 952991a0f9..a63eebbfd8 100644
--- a/worker/src/features/evaluation/observationEval/__tests__/filterEvaluation.test.ts
+++ b/worker/src/features/evaluation/observationEval/__tests__/filterEvaluation.test.ts
@@ -6,7 +6,11 @@ import {
createTestEvalConfig,
createMockSchedulerDeps,
} from "./fixtures";
-import { type ObservationForEval, EvalTargetObject } from "@langfuse/shared";
+import {
+ type ObservationForEval,
+ EvalTargetObject,
+ observationForEvalSchema,
+} from "@langfuse/shared";
// Mock logger to avoid noise in tests
vi.mock("@langfuse/shared/src/server", async () => {
@@ -961,6 +965,75 @@ describe("Filter Evaluation for Observation Evals", () => {
});
});
+ describe("tool call filters", () => {
+ it("should match when called tool name is in filter list", async () => {
+ const observation = createTestObservation({
+ project_id: projectId,
+ tool_call_names: ["get_weather", "search_web"],
+ });
+
+ const matched = await testFilterMatch(observation, [
+ {
+ column: "calledToolNames",
+ type: "arrayOptions",
+ operator: "any of",
+ value: ["get_weather", "send_email"],
+ },
+ ]);
+
+ expect(matched).toBe(true);
+ });
+
+ it("should not match when no called tool name is in filter list", async () => {
+ const observation = createTestObservation({
+ project_id: projectId,
+ tool_call_names: ["get_weather", "search_web"],
+ });
+
+ const matched = await testFilterMatch(observation, [
+ {
+ column: "calledToolNames",
+ type: "arrayOptions",
+ operator: "any of",
+ value: ["send_email"],
+ },
+ ]);
+
+ expect(matched).toBe(false);
+ });
+
+ it("should match when tool call count meets threshold", async () => {
+ const observation = createTestObservation({
+ project_id: projectId,
+ tool_call_names: ["tool_a", "tool_b", "tool_c"],
+ });
+
+ const matched = await testFilterMatch(observation, [
+ {
+ column: "toolCalls",
+ type: "number",
+ operator: ">=",
+ value: 2,
+ },
+ ]);
+
+ expect(matched).toBe(true);
+ });
+
+ it("should not match when tool call count does not meet threshold", async () => {
+ const observation = createTestObservation({
+ project_id: projectId,
+ tool_call_names: ["tool_a", "tool_b", "tool_c"],
+ });
+
+ const matched = await testFilterMatch(observation, [
+ { column: "toolCalls", type: "number", operator: "=", value: 0 },
+ ]);
+
+ expect(matched).toBe(false);
+ });
+ });
+
describe("null filters (parentObservationId)", () => {
it("should match observations where parentObservationId is null (root observations)", async () => {
const observation = createTestObservation({
@@ -1085,3 +1158,38 @@ describe("Filter Evaluation for Observation Evals", () => {
});
});
});
+
+describe("tool_calls computation", () => {
+ const minimalObs = {
+ span_id: "s1",
+ trace_id: "t1",
+ project_id: "p1",
+ type: "GENERATION",
+ name: "test",
+ provided_usage_details: {},
+ provided_cost_details: {},
+ usage_details: {},
+ cost_details: {},
+ };
+
+ it("should default tool_call_count to 0", () => {
+ const obs = observationForEvalSchema.parse(minimalObs);
+
+ expect(obs.tool_call_count).toBe(0);
+ });
+
+ it("should default tool_call_names to empty array", () => {
+ const obs = observationForEvalSchema.parse(minimalObs);
+
+ expect(obs.tool_call_names).toEqual([]);
+ });
+
+ it("should accept an explicit tool_call_count", () => {
+ const obs = observationForEvalSchema.parse({
+ ...minimalObs,
+ tool_call_count: 3,
+ });
+
+ expect(obs.tool_call_count).toBe(3);
+ });
+});
diff --git a/worker/src/features/evaluation/observationEval/__tests__/fixtures.ts b/worker/src/features/evaluation/observationEval/__tests__/fixtures.ts
index d07430c9e3..145c4fb930 100644
--- a/worker/src/features/evaluation/observationEval/__tests__/fixtures.ts
+++ b/worker/src/features/evaluation/observationEval/__tests__/fixtures.ts
@@ -20,6 +20,7 @@ import {
export function createTestObservation(
overrides: Partial<ObservationForEval> = {},
): ObservationForEval {
+ const toolCallNames = overrides.tool_call_names ?? [];
return {
// Core identifiers
span_id: `obs-${randomUUID()}`,
@@ -55,6 +56,7 @@ export function createTestObservation(
tool_definitions: {},
tool_calls: [],
tool_call_names: [],
+ tool_call_count: toolCallNames.length,
// Usage & Cost
usage_details: { input: 100, output: 50 },
diff --git a/worker/src/features/evaluation/observationEval/__tests__/observationForEvalSchema.test.ts b/worker/src/features/evaluation/observationEval/__tests__/observationForEvalSchema.test.ts
index 0fdefc3e4d..38b17c1d69 100644
--- a/worker/src/features/evaluation/observationEval/__tests__/observationForEvalSchema.test.ts
+++ b/worker/src/features/evaluation/observationEval/__tests__/observationForEvalSchema.test.ts
@@ -88,6 +88,7 @@ describe("observationForEvalSchema", () => {
tool_definitions: { search: '{"description": "Search"}' },
tool_calls: ['{"name": "search"}'],
tool_call_names: ["search"],
+ tool_call_count: 1,
usage_details: { input: 100, output: 50 },
cost_details: { total: 0.01 },
provided_usage_details: {},
diff --git a/worker/src/features/evaluation/observationEval/__tests__/scheduleObservationEvals.test.ts b/worker/src/features/evaluation/observationEval/__tests__/scheduleObservationEvals.test.ts
index 32ced014bf..c83a8f86b0 100644
--- a/worker/src/features/evaluation/observationEval/__tests__/scheduleObservationEvals.test.ts
+++ b/worker/src/features/evaluation/observationEval/__tests__/scheduleObservationEvals.test.ts
@@ -51,6 +51,7 @@ describe("scheduleObservationEvals", () => {
tool_definitions: {},
tool_calls: [],
tool_call_names: [],
+ tool_call_count: 0,
// Usage & Cost
usage_details: { input: 100, output: 50 },
diff --git a/worker/src/features/experiments/__tests__/scheduleExperimentEvals.test.ts b/worker/src/features/experiments/__tests__/scheduleExperimentEvals.test.ts
index 07acfd1492..a003255ef6 100644
--- a/worker/src/features/experiments/__tests__/scheduleExperimentEvals.test.ts
+++ b/worker/src/features/experiments/__tests__/scheduleExperimentEvals.test.ts
@@ -429,6 +429,7 @@ describe("buildObservationForEval", () => {
tool_definitions: {},
tool_calls: [],
tool_call_names: [],
+ tool_call_count: 0,
};
}
diff --git a/worker/src/features/experiments/scheduleExperimentEvals.ts b/worker/src/features/experiments/scheduleExperimentEvals.ts
index ccb3212cee..4fe89e7a4f 100644
--- a/worker/src/features/experiments/scheduleExperimentEvals.ts
+++ b/worker/src/features/experiments/scheduleExperimentEvals.ts
@@ -86,6 +86,7 @@ export async function scheduleExperimentObservationEvals(
tool_definitions: {},
tool_calls: [],
tool_call_names: [],
+ tool_call_count: 0,
};
// 3. Schedule evals
diff --git a/worker/src/queues/__tests__/otelToObservationForEval.test.ts b/worker/src/queues/__tests__/otelToObservationForEval.test.ts
index 9b0c9bedfe..c67c9a2743 100644
--- a/worker/src/queues/__tests__/otelToObservationForEval.test.ts
+++ b/worker/src/queues/__tests__/otelToObservationForEval.test.ts
@@ -1512,6 +1512,55 @@ describe("OTEL to ObservationForEval Schema Validation", () => {
expect(obs.tool_definitions).toBeDefined();
expect(obs.tool_calls).toBeDefined();
expect(obs.tool_call_names).toBeDefined();
+
+ // tool_call_count is derived from tool_call_names
+ expect(obs.tool_call_count).toBe(obs.tool_call_names.length);
+ });
+
+ it("should set tool_call_count to 0 when no tool calls are present", async () => {
+ const noToolsSpan = {
+ resource: { attributes: [] },
+ scopeSpans: [
+ {
+ scope: { name: "langfuse-sdk", version: "3.0.0" },
+ spans: [
+ {
+ traceId: createBufferId("123456789012345678901234567890ab"),
+ spanId: createBufferId("00b001567890abcd"),
+ name: "no-tools-test",
+ kind: 1,
+ startTimeUnixNano: createNanoTimestamp(
+ BigInt(1738242387865000000),
+ ),
+ endTimeUnixNano: createNanoTimestamp(
+ BigInt(1738242389310000000),
+ ),
+ attributes: [
+ {
+ key: "langfuse.observation.type",
+ value: { stringValue: "generation" },
+ },
+ {
+ key: "langfuse.observation.input",
+ value: { stringValue: '"Hello"' },
+ },
+ {
+ key: "langfuse.observation.output",
+ value: { stringValue: '"Hi there"' },
+ },
+ ],
+ status: {},
+ },
+ ],
+ },
+ ],
+ };
+
+ const observations =
+ await processOtelSpanToObservationForEval(noToolsSpan);
+
+ expect(observations).toHaveLength(1);
+ expect(observations[0].tool_call_count).toBe(0);
});
});
});