Skip to content

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

refactor(blob-export): gate pricing fields on model group, not usage (#13716)

Niklas Semmlerfb2d74f259e2
diff --git a/packages/shared/src/domain/observation-field-groups.ts b/packages/shared/src/domain/observation-field-groups.ts
index 5405b992e5..ba7e6de606 100644
--- a/packages/shared/src/domain/observation-field-groups.ts
+++ b/packages/shared/src/domain/observation-field-groups.ts
@@ -21,9 +21,8 @@
* `outputPrice`, `totalPrice`.
* - For the v2 public API, the Postgres lookup that populates them only runs
* when `"model"` is in `fields`; otherwise the fields are returned as null.
- * - For the blob export path, the gate is broader: `"model"` OR `"usage"`
- * triggers selection of the `model_export` SQL field set so pricing can be
- * enriched into the usage payload.
+ * - For the blob export path `"model"` triggers selection of the `model_export`
+ * SQL field set so pricing can be enriched into the usage payload.
* - Code references:
* - packages/shared/src/server/repositories/events.ts:
* `enrichObservationsWithModelData` (v2 read path) and the export
diff --git a/packages/shared/src/features/analytics-integrations/index.ts b/packages/shared/src/features/analytics-integrations/index.ts
index 82e8b1cf45..2a173cc709 100644
--- a/packages/shared/src/features/analytics-integrations/index.ts
+++ b/packages/shared/src/features/analytics-integrations/index.ts
@@ -64,12 +64,13 @@ const EXPORT_FIELD_GROUP_LABELS = {
},
model: {
label: "Model",
- description: "provided_model_name, model_id, model_parameters",
+ description:
+ "provided_model_name, model_id, model_parameters, input_price, output_price, total_price",
},
usage: {
label: "Usage",
description:
- "usage_details, cost_details, total_cost, input_price, output_price, total_price, usage_pricing_tier_name",
+ "usage_details, cost_details, total_cost, usage_pricing_tier_id, usage_pricing_tier_name",
},
prompt: {
label: "Prompt",
diff --git a/packages/shared/src/server/repositories/events.ts b/packages/shared/src/server/repositories/events.ts
index 6aee2823ee..5e23c196e7 100644
--- a/packages/shared/src/server/repositories/events.ts
+++ b/packages/shared/src/server/repositories/events.ts
@@ -3015,26 +3015,16 @@ export const getEventsForBlobStorageExport = function (
? fieldGroups
: (["core", ...fieldGroups] as ObservationFieldGroupFull[]);
- // model_export must be selected whenever model or usage is requested:
- // - model group: include model identification fields in the output
- // - usage group (without model): pricing enrichment needs model_id for the
- // lookup; the field is dropped afterward by enrichObservationStream
- const needsModelFields =
- fieldGroups.includes("model") || fieldGroups.includes("usage");
-
for (const group of effectiveGroups) {
if (group === "io") {
queryBuilder.selectIO(false); // Full I/O, no truncation
- } else if (group !== "model") {
- // model_export is selected below via needsModelFields to avoid double-selecting
+ } else if (group === "model") {
+ queryBuilder.selectFieldSet("model_export"); // "model_export" is the SQL field set name for the "model" group
+ } else {
queryBuilder.selectFieldSet(group);
}
}
- if (needsModelFields) {
- queryBuilder.selectFieldSet("model_export");
- }
-
queryBuilder
.whereRaw(
"e.start_time >= {minTimestamp: DateTime64(3)} AND e.start_time <= {maxTimestamp: DateTime64(3)}",
diff --git a/worker/src/__tests__/enrichObservationStream.unit.test.ts b/worker/src/__tests__/enrichObservationStream.unit.test.ts
index a27e995f9c..f90a58e69a 100644
--- a/worker/src/__tests__/enrichObservationStream.unit.test.ts
+++ b/worker/src/__tests__/enrichObservationStream.unit.test.ts
@@ -71,7 +71,7 @@ describe("enrichObservationStream field group filtering", () => {
expect(results[0]).not.toHaveProperty("metadata");
});
- it("does not add pricing fields when usage group is not selected", async () => {
+ it("does not add pricing fields when model group is not selected", async () => {
const rows = [{ id: "obs-1" }];
const results = await collect(
enrichObservationStream(rowStream(rows), "project-1", "model_id", false, [
@@ -85,10 +85,26 @@ describe("enrichObservationStream field group filtering", () => {
expect(results[0]).not.toHaveProperty("total_price");
});
- it("adds pricing fields but drops all model_export columns when usage is selected without model", async () => {
- // ClickHouse fetches model_export (model_id, provided_model_name,
- // model_parameters) whenever usage is requested so the pricing lookup has
- // data. All three must be removed from the output when model is not selected.
+ it("does not add pricing fields when usage is selected without model", async () => {
+ // model_export columns are not fetched when only usage is requested,
+ // so there is no model_id to look up and no pricing to enrich.
+ const rows = [{ id: "obs-1" }];
+ const results = await collect(
+ enrichObservationStream(rowStream(rows), "project-1", "model_id", false, [
+ "core" as ObservationFieldGroupFull,
+ "usage" as ObservationFieldGroupFull,
+ ]),
+ );
+
+ expect(results[0]).not.toHaveProperty("model_id");
+ expect(results[0]).not.toHaveProperty("provided_model_name");
+ expect(results[0]).not.toHaveProperty("model_parameters");
+ expect(results[0]).not.toHaveProperty("input_price");
+ expect(results[0]).not.toHaveProperty("output_price");
+ expect(results[0]).not.toHaveProperty("total_price");
+ });
+
+ it("adds pricing fields and preserves model_export columns when model group is selected", async () => {
const rows = [
{
id: "obs-1",
@@ -100,19 +116,19 @@ describe("enrichObservationStream field group filtering", () => {
const results = await collect(
enrichObservationStream(rowStream(rows), "project-1", "model_id", false, [
"core" as ObservationFieldGroupFull,
- "usage" as ObservationFieldGroupFull,
+ "model" as ObservationFieldGroupFull,
]),
);
- expect(results[0]).not.toHaveProperty("model_id");
- expect(results[0]).not.toHaveProperty("provided_model_name");
- expect(results[0]).not.toHaveProperty("model_parameters");
+ expect(results[0]).toHaveProperty("model_id");
+ expect(results[0]).toHaveProperty("provided_model_name");
+ expect(results[0]).toHaveProperty("model_parameters");
expect(results[0]).toHaveProperty("input_price");
expect(results[0]).toHaveProperty("output_price");
expect(results[0]).toHaveProperty("total_price");
});
- it("preserves model_id when both model and usage groups are selected", async () => {
+ it("adds pricing fields when both model and usage groups are selected", async () => {
const rows = [{ id: "obs-1", model_id: "gpt-4" }];
const results = await collect(
enrichObservationStream(rowStream(rows), "project-1", "model_id", false, [
diff --git a/worker/src/features/blobstorage/handleBlobStorageIntegrationProjectJob.ts b/worker/src/features/blobstorage/handleBlobStorageIntegrationProjectJob.ts
index dd7c36d395..4588b022e8 100644
--- a/worker/src/features/blobstorage/handleBlobStorageIntegrationProjectJob.ts
+++ b/worker/src/features/blobstorage/handleBlobStorageIntegrationProjectJob.ts
@@ -46,13 +46,12 @@ export async function* enrichObservationStream(
): AsyncGenerator<Record<string, unknown>> {
const { getModel } = createModelCache(projectId);
- const includePricing = !fieldGroups || fieldGroups.includes("usage");
const includeModelId = !fieldGroups || fieldGroups.includes("model");
for await (const row of stream) {
const enriched: Record<string, unknown> = { ...row };
- if (includePricing) {
+ if (includeModelId) {
const modelId = row[modelIdField] as string | null | undefined;
const model = await getModel(modelId);
const pricing = enrichObservationWithModelData(model);
@@ -61,16 +60,6 @@ export async function* enrichObservationStream(
enriched.total_price = pricing.totalPrice;
}
- // model_export (provided_model_name, model_id, model_parameters) is fetched
- // whenever usage OR model is requested — usage needs model_id for the
- // pricing lookup. Drop all three when the model group was not requested so
- // they don't leak into a usage-only export.
- if (!includeModelId) {
- delete enriched[modelIdField];
- delete enriched.provided_model_name;
- delete enriched.model_parameters;
- }
-
// ClickHouse returns {} for Map columns even when not SELECTed — drop it
// when the metadata group was not requested.
if (fieldGroups && !fieldGroups.includes("metadata")) {