Skip to content

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

feat(api): add count and cost to daily metrics api (#2219)

Marc Klingenfd18ab605d6e
diff --git a/fern/apis/server/definition/metrics.yml b/fern/apis/server/definition/metrics.yml
index 35c81b0f97..c00dd99f2c 100644
--- a/fern/apis/server/definition/metrics.yml
+++ b/fern/apis/server/definition/metrics.yml
@@ -48,12 +48,25 @@ types:
date: date
countTraces: integer
countObservations: integer
- totalCost: double
+ totalCost:
+ type: double
+ docs: Total model cost in USD
usage: list<UsageByModel>
UsageByModel:
docs: Daily usage of a given model. Usage corresponds to the unit set for the specific model (e.g. tokens).
properties:
model: optional<string>
- inputUsage: integer
- outputUsage: integer
- totalUsage: integer
+ inputUsage:
+ type: integer
+ docs: Total number of generation input units (e.g. tokens)
+ outputUsage:
+ type: integer
+ docs: Total number of generation output units (e.g. tokens)
+ totalUsage:
+ type: integer
+ docs: Total number of generation total units (e.g. tokens)
+ countTraces: integer
+ countObservations: integer
+ totalCost:
+ type: double
+ docs: Total model cost in USD
diff --git a/web/public/generated/api/openapi.yml b/web/public/generated/api/openapi.yml
index d81846a90d..99e45a43aa 100644
--- a/web/public/generated/api/openapi.yml
+++ b/web/public/generated/api/openapi.yml
@@ -2499,6 +2499,7 @@ components:
totalCost:
type: number
format: double
+ description: Total model cost in USD
usage:
type: array
items:
@@ -2521,14 +2522,28 @@ components:
nullable: true
inputUsage:
type: integer
+ description: Total number of generation input units (e.g. tokens)
outputUsage:
type: integer
+ description: Total number of generation output units (e.g. tokens)
totalUsage:
type: integer
+ description: Total number of generation total units (e.g. tokens)
+ countTraces:
+ type: integer
+ countObservations:
+ type: integer
+ totalCost:
+ type: number
+ format: double
+ description: Total model cost in USD
required:
- inputUsage
- outputUsage
- totalUsage
+ - countTraces
+ - countObservations
+ - totalCost
Observations:
title: Observations
type: object
diff --git a/web/src/__tests__/metrics-api.servertest.ts b/web/src/__tests__/metrics-api.servertest.ts
index dc6a6d0816..4c58bd7512 100644
--- a/web/src/__tests__/metrics-api.servertest.ts
+++ b/web/src/__tests__/metrics-api.servertest.ts
@@ -68,12 +68,18 @@ describe("/api/public/metrics/daily API Endpoint", () => {
inputUsage: 333,
outputUsage: 0,
totalUsage: 333,
+ countObservations: 1,
+ countTraces: 1,
+ totalCost: 0,
},
{
model: "modelC",
inputUsage: 666,
outputUsage: 777,
totalUsage: 1443,
+ countObservations: 1,
+ countTraces: 1,
+ totalCost: 1024.22,
},
]);
@@ -88,6 +94,9 @@ describe("/api/public/metrics/daily API Endpoint", () => {
inputUsage: 100,
outputUsage: 200,
totalUsage: 300,
+ countObservations: 1,
+ countTraces: 1,
+ totalCost: 0,
},
]);
});
diff --git a/web/src/pages/api/public/metrics/daily.ts b/web/src/pages/api/public/metrics/daily.ts
index a022d5123a..6353b72a2d 100644
--- a/web/src/pages/api/public/metrics/daily.ts
+++ b/web/src/pages/api/public/metrics/daily.ts
@@ -68,12 +68,15 @@ export default async function handler(
DATE_TRUNC('DAY',
o.start_time) "date",
o.model,
- SUM(o.prompt_tokens) inputUsage,
- SUM(o.completion_tokens) outputUsage,
- SUM(o.total_tokens) totalUsage
+ count(distinct o.id)::integer as "countObservations",
+ count(distinct t.id)::integer as "countTraces",
+ SUM(o.prompt_tokens) "inputUsage",
+ SUM(o.completion_tokens) "outputUsage",
+ SUM(o.total_tokens) "totalUsage",
+ COALESCE(SUM(o.calculated_total_cost), 0)::DOUBLE PRECISION as "totalCost"
FROM
traces t
- LEFT JOIN observations o ON o.trace_id = t.id AND o.project_id = t.project_id
+ LEFT JOIN observations_view o ON o.trace_id = t.id AND o.project_id = t.project_id
WHERE o.start_time IS NOT NULL
AND t.project_id = ${authCheck.scope.projectId}
${traceNameCondition}
@@ -94,11 +97,17 @@ export default async function handler(
json_agg(json_build_object('model',
model,
'inputUsage',
- inputUsage,
+ "inputUsage",
'outputUsage',
- outputUsage,
+ "outputUsage",
'totalUsage',
- totalUsage)) daily_usage_json
+ "totalUsage",
+ 'totalCost',
+ "totalCost",
+ 'countObservations',
+ "countObservations",
+ 'countTraces',
+ "countTraces")) daily_usage_json
FROM
model_usage
GROUP BY