Skip to content

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

feat(api): add htmlPath and totalCost to GET traces/[id] (#1207)

Marc Klingena1e961de093e
diff --git a/fern/apis/server/definition/commons.yml b/fern/apis/server/definition/commons.yml
index fdce8598e6..66fba0a0c1 100644
--- a/fern/apis/server/definition/commons.yml
+++ b/fern/apis/server/definition/commons.yml
@@ -29,7 +29,7 @@ types:
docs: Latency of trace in seconds
totalCost:
type: double
- docs: Cost of trace in USD.
+ docs: Cost of trace in USD
observations:
type: list<string>
docs: List of observation ids
@@ -39,6 +39,12 @@ types:
TraceWithFullDetails: # GET traces/[traceID]
extends: Trace
properties:
+ htmlPath:
+ type: string
+ docs: Path of trace in Langfuse UI
+ totalCost:
+ type: double
+ docs: Cost of trace in USD
observations: list<ObservationsView>
scores: list<Score>
Session:
diff --git a/generated/openapi-server/openapi.yml b/generated/openapi-server/openapi.yml
index 71d3f1ec19..0ecc519254 100644
--- a/generated/openapi-server/openapi.yml
+++ b/generated/openapi-server/openapi.yml
@@ -1110,7 +1110,7 @@ components:
totalCost:
type: number
format: double
- description: Cost of trace in USD.
+ description: Cost of trace in USD
observations:
type: array
items:
@@ -1133,6 +1133,13 @@ components:
title: TraceWithFullDetails
type: object
properties:
+ htmlPath:
+ type: string
+ description: Path of trace in Langfuse UI
+ totalCost:
+ type: number
+ format: double
+ description: Cost of trace in USD
observations:
type: array
items:
@@ -1142,6 +1149,8 @@ components:
items:
$ref: '#/components/schemas/Score'
required:
+ - htmlPath
+ - totalCost
- observations
- scores
allOf:
diff --git a/src/__tests__/traces.servertest.ts b/src/__tests__/traces.servertest.ts
index 50ca6cde1b..23cb2eb2be 100644
--- a/src/__tests__/traces.servertest.ts
+++ b/src/__tests__/traces.servertest.ts
@@ -4,7 +4,7 @@ import { makeAPICall, pruneDatabase } from "@/src/__tests__/test-utils";
import { prisma } from "@/src/server/db";
import { v4 as uuidv4 } from "uuid";
-interface TraceAPIResponse {
+interface GetTracesAPIResponse {
data: Array<{
id: string;
[key: string]: unknown;
@@ -116,7 +116,7 @@ describe("/api/public/traces API Endpoint", () => {
});
// multiple tags
- const traces = await makeAPICall<TraceAPIResponse>(
+ const traces = await makeAPICall<GetTracesAPIResponse>(
"GET",
"/api/public/traces?tags=tag-2&tags=tag-3",
);
@@ -125,7 +125,7 @@ describe("/api/public/traces API Endpoint", () => {
expect(traceIds).toEqual(["trace-3", "trace-1"]);
// single tag
- const traces2 = await makeAPICall<TraceAPIResponse>(
+ const traces2 = await makeAPICall<GetTracesAPIResponse>(
"GET",
"/api/public/traces?tags=tag-1",
);
@@ -134,7 +134,7 @@ describe("/api/public/traces API Endpoint", () => {
expect(traceIds2).toEqual(["trace-2", "trace-1"]);
// wrong tag
- const traces3 = await makeAPICall<TraceAPIResponse>(
+ const traces3 = await makeAPICall<GetTracesAPIResponse>(
"GET",
"/api/public/traces?tags=tag-10",
);
@@ -143,7 +143,7 @@ describe("/api/public/traces API Endpoint", () => {
expect(traceIds3).toEqual([]);
// no tag
- const traces4 = await makeAPICall<TraceAPIResponse>(
+ const traces4 = await makeAPICall<GetTracesAPIResponse>(
"GET",
"/api/public/traces?tags=",
);
@@ -152,7 +152,7 @@ describe("/api/public/traces API Endpoint", () => {
expect(traceIds4).toEqual(["trace-3", "trace-2", "trace-1"]);
});
- it("should handle totalCost and latency correctly", async () => {
+ it("should handle metrics correctly on GET traces and GET trace", async () => {
await pruneDatabase();
// Create a trace with some observations that have costs and latencies
@@ -182,8 +182,9 @@ describe("/api/public/traces API Endpoint", () => {
endTime: "2021-01-01T00:20:00.000Z",
});
+ // GET traces
// Retrieve the trace with totalCost and latency
- const traces = await makeAPICall<TraceAPIResponse>(
+ const traces = await makeAPICall<GetTracesAPIResponse>(
"GET",
`/api/public/traces`,
);
@@ -193,5 +194,22 @@ describe("/api/public/traces API Endpoint", () => {
// Check if the totalCost and latency are calculated correctly
expect(traceData.totalCost).toBeCloseTo(15.75); // Sum of costs
expect(traceData.latency).toBeCloseTo(1200); // Difference in seconds between min startTime and max endTime
+ expect(traceData.id).toBe(traceId);
+ expect(traceData.htmlPath).toContain(`/traces/${traceId}`);
+ expect(traceData.htmlPath).toContain(`/project/`); // do not know the projectId
+
+ // GET trace
+ // Retrieve the trace with total
+ const trace = await makeAPICall<{
+ id: string;
+ totalCost: number;
+ htmlPath: string;
+ }>("GET", `/api/public/traces/${traceId}`);
+ console.log(trace.body);
+ expect(trace.body.totalCost).toBeCloseTo(15.75);
+ expect(trace.body.id).toBe(traceId);
+ expect(trace.body.id).toBe(traceId);
+ expect(trace.body.htmlPath).toContain(`/traces/${traceId}`);
+ expect(trace.body.htmlPath).toContain(`/project/`); // do not know the projectId
});
});
diff --git a/src/pages/api/public/traces/[traceId].ts b/src/pages/api/public/traces/[traceId].ts
index 488ad225e8..ede79ed43c 100644
--- a/src/pages/api/public/traces/[traceId].ts
+++ b/src/pages/api/public/traces/[traceId].ts
@@ -67,9 +67,17 @@ export default async function handler(
},
});
- return res
- .status(200)
- .json({ ...trace, observations: observations.map(mapUsageOutput) });
+ const outObservations = observations.map(mapUsageOutput);
+
+ return res.status(200).json({
+ ...trace,
+ htmlPath: `/project/${authCheck.scope.projectId}/traces/${traceId}`,
+ totalCost: outObservations.reduce(
+ (acc, obs) => acc + (obs.calculatedTotalCost ?? 0),
+ 0,
+ ),
+ observations: outObservations,
+ });
} catch (error: unknown) {
console.error(error);
const errorMessage =