Skip to content

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

perf(api): optimize get traces api & fix `totalCost` (#2117)

Marc Klingencd9fd110e415
diff --git a/web/src/__tests__/traces.servertest.ts b/web/src/__tests__/traces.servertest.ts
index 3c0b7f1ccd..ae0bff4720 100644
--- a/web/src/__tests__/traces.servertest.ts
+++ b/web/src/__tests__/traces.servertest.ts
@@ -182,6 +182,32 @@ describe("/api/public/traces API Endpoint", () => {
endTime: "2021-01-01T00:20:00.000Z",
});
+ // Simulate scores on the trace
+ const scoreId1 = uuidv4();
+ await makeAPICall("POST", "/api/public/scores", {
+ id: scoreId1,
+ name: "score-1",
+ value: 75.0,
+ traceId: traceId,
+ comment: "First score",
+ });
+ const scoreId2 = uuidv4();
+ await makeAPICall("POST", "/api/public/scores", {
+ id: scoreId2,
+ name: "score-2",
+ value: 85.5,
+ traceId: traceId,
+ comment: "Second score",
+ });
+ const scoreId3 = uuidv4();
+ await makeAPICall("POST", "/api/public/scores", {
+ id: scoreId3,
+ name: "score-3",
+ value: 95.0,
+ traceId: traceId,
+ comment: "Third score",
+ });
+
// GET traces
// Retrieve the trace with totalCost and latency
const traces = await makeAPICall<GetTracesAPIResponse>(
diff --git a/web/src/pages/api/public/traces.ts b/web/src/pages/api/public/traces.ts
index 4f1e36e980..b09d636a2e 100644
--- a/web/src/pages/api/public/traces.ts
+++ b/web/src/pages/api/public/traces.ts
@@ -133,21 +133,35 @@ export default async function handler(
t.version,
t.public,
t.tags,
- COALESCE(SUM(o.calculated_total_cost), 0)::DOUBLE PRECISION AS "totalCost",
- COALESCE(EXTRACT(EPOCH FROM COALESCE(MAX(o."end_time"), MAX(o."start_time"))) - EXTRACT(EPOCH FROM MIN(o."start_time")), 0)::double precision AS "latency",
- COALESCE(ARRAY_AGG(DISTINCT o.id) FILTER (WHERE o.id IS NOT NULL), ARRAY[]::text[]) AS "observations",
- COALESCE(ARRAY_AGG(DISTINCT s.id) FILTER (WHERE s.id IS NOT NULL), ARRAY[]::text[]) AS "scores"
- FROM "traces" AS t
- LEFT JOIN "observations_view" AS o ON t.id = o.trace_id AND o.project_id = ${authCheck.scope.projectId}
- LEFT JOIN "scores" AS s ON t.id = s.trace_id AND s.project_id = ${authCheck.scope.projectId}
- WHERE t.project_id = ${authCheck.scope.projectId}
- ${userCondition}
- ${nameCondition}
- ${tagsCondition}
- ${fromTimestampCondition}
- GROUP BY t.id
- ${orderByCondition}
- LIMIT ${obj.limit} OFFSET ${skipValue}
+ COALESCE(o."totalCost", 0)::DOUBLE PRECISION AS "totalCost",
+ COALESCE(o."latency", 0)::double precision AS "latency",
+ COALESCE(o."observations", ARRAY[]::text[]) AS "observations",
+ COALESCE(s."scores", ARRAY[]::text[]) AS "scores"
+ FROM (
+ SELECT *
+ FROM "traces" t
+ WHERE project_id = ${authCheck.scope.projectId}
+ ${fromTimestampCondition}
+ ${userCondition}
+ ${nameCondition}
+ ${tagsCondition}
+ ${orderByCondition}
+ LIMIT ${obj.limit} OFFSET ${skipValue}
+ ) AS t
+ LEFT JOIN LATERAL (
+ SELECT
+ SUM(o.calculated_total_cost)::DOUBLE PRECISION AS "totalCost",
+ EXTRACT(EPOCH FROM COALESCE(MAX(o."end_time"), MAX(o."start_time"))) - EXTRACT(EPOCH FROM MIN(o."start_time"))::DOUBLE PRECISION AS "latency",
+ ARRAY_AGG(DISTINCT o.id) FILTER (WHERE o.id IS NOT NULL) AS "observations"
+ FROM "observations_view" AS o
+ WHERE o.trace_id = t.id AND o.project_id = ${authCheck.scope.projectId}
+ ) AS o ON true
+ LEFT JOIN LATERAL (
+ SELECT
+ ARRAY_AGG(DISTINCT s.id) FILTER (WHERE s.id IS NOT NULL) AS "scores"
+ FROM "scores" AS s
+ WHERE s.trace_id = t.id AND s.project_id = ${authCheck.scope.projectId}
+ ) AS s ON true
`);
const totalItems = await prisma.trace.count({
where: {