Skip to content

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

perf(public-api-traces): raise early error if observations are too large (#6592)

Hassieb Pakzad231ee5873c6b
diff --git a/packages/shared/src/server/repositories/observations.ts b/packages/shared/src/server/repositories/observations.ts
index 4849c6cd9b..415f6ab0a7 100644
--- a/packages/shared/src/server/repositories/observations.ts
+++ b/packages/shared/src/server/repositories/observations.ts
@@ -172,6 +172,38 @@ export const getObservationsForTrace = async (
},
});
+ // Large number of observations in trace with large input / output / metadata will lead to
+ // high CPU and memory consumption in the convertObservation step, where parsing occurs
+ // Thus, limit the size of the payload to 4MB, follows NextJS response size limitation:
+ // https://nextjs.org/docs/messages/api-routes-response-size-limit
+ // See also LFE-4882 for more details
+ const PAYLOAD_SIZE_LIMIT = 4e6; // 4MB
+ let payloadSize = 0;
+
+ for (const observation of records) {
+ for (const key of ["input", "output"] as const) {
+ const value = observation[key];
+
+ if (value && typeof value === "string") {
+ payloadSize += value.length;
+ }
+ }
+
+ const metadataValues = Object.values(observation["metadata"]);
+
+ metadataValues.forEach((value) => {
+ if (value && typeof value === "string") {
+ payloadSize += value.length;
+ }
+ });
+
+ if (payloadSize >= PAYLOAD_SIZE_LIMIT) {
+ const errorMessage = `Observations in trace are too large: ${(payloadSize / 1e6).toFixed(2)}MB exceeds limit of ${(PAYLOAD_SIZE_LIMIT / 1e6).toFixed(2)}MB`;
+
+ throw new Error(errorMessage);
+ }
+ }
+
return records.map(convertObservation);
};
diff --git a/web/src/__tests__/async/traces-api.servertest.ts b/web/src/__tests__/async/traces-api.servertest.ts
index 756c38993a..a1143f6984 100644
--- a/web/src/__tests__/async/traces-api.servertest.ts
+++ b/web/src/__tests__/async/traces-api.servertest.ts
@@ -417,6 +417,56 @@ describe("/api/public/traces API Endpoint", () => {
});
});
+ it("should return 5XX if observations are too large when fetching single trace", async () => {
+ // See LFE-4882 for context
+ const traceId = randomUUID();
+ const trace = createTrace({
+ id: traceId,
+ name: "trace-name1",
+ project_id: projectId,
+ metadata: { key: JSON.stringify({ foo: "bar" }) },
+ input: JSON.stringify({
+ args: [
+ {
+ foo: "bar",
+ },
+ ],
+ }),
+ });
+
+ await createTracesCh([trace]);
+ await createObservationsCh([
+ createObservation({
+ trace_id: traceId,
+ project_id: projectId,
+ input: "a".repeat(1e6),
+ output: "b".repeat(1e6),
+ metadata: {
+ foo: "c".repeat(1e6),
+ },
+ }),
+ createObservation({
+ trace_id: traceId,
+ project_id: projectId,
+ input: "a".repeat(1e6),
+ output: "b".repeat(1e6),
+ metadata: {
+ foo: "c".repeat(1e6),
+ },
+ }),
+ ]);
+
+ await expect(
+ makeZodVerifiedAPICall(
+ GetTraceV1Response,
+ "GET",
+ `/api/public/traces/${traceId}`,
+ ),
+ ).rejects.toThrow(
+ "Observations in trace are too large: 6.00MB exceeds limit of 4.00MB",
+ );
+ });
+
it("should delete a single trace via DELETE /traces/:traceId", async () => {
// Setup
const createdTrace = createTrace({
@@ -465,9 +515,15 @@ describe("/api/public/traces API Endpoint", () => {
// Then
expect(deleteResponse.status).toBe(200);
await waitForExpect(async () => {
- const trace1 = await getTraceById({ traceId: createdTrace1.id, projectId });
+ const trace1 = await getTraceById({
+ traceId: createdTrace1.id,
+ projectId,
+ });
expect(trace1).toBeUndefined();
- const trace2 = await getTraceById({ traceId: createdTrace2.id, projectId });
+ const trace2 = await getTraceById({
+ traceId: createdTrace2.id,
+ projectId,
+ });
expect(trace2).toBeUndefined();
}, 40_000);
}, 60_000);