Skip to content

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

chore: add projectId to prices table (#7817)

Steffen Schmitz35171cc77d8e
diff --git a/packages/shared/prisma/generated/types.ts b/packages/shared/prisma/generated/types.ts
index 4c49baa5cd..d2c8fba75e 100644
--- a/packages/shared/prisma/generated/types.ts
+++ b/packages/shared/prisma/generated/types.ts
@@ -637,6 +637,7 @@ export type Price = {
created_at: Generated<Timestamp>;
updated_at: Generated<Timestamp>;
model_id: string;
+ project_id: string | null;
usage_type: string;
price: string;
};
diff --git a/packages/shared/prisma/migrations/20250711105322_prices_add_project_id/migration.sql b/packages/shared/prisma/migrations/20250711105322_prices_add_project_id/migration.sql
new file mode 100644
index 0000000000..adf09862a4
--- /dev/null
+++ b/packages/shared/prisma/migrations/20250711105322_prices_add_project_id/migration.sql
@@ -0,0 +1,13 @@
+-- AlterTable
+ALTER TABLE "prices"
+ ADD COLUMN "project_id" TEXT;
+
+-- AddForeignKey
+ALTER TABLE "prices"
+ ADD CONSTRAINT "prices_project_id_fkey" FOREIGN KEY ("project_id") REFERENCES "projects" ("id") ON DELETE CASCADE ON UPDATE CASCADE;
+
+-- BackfillData
+UPDATE "prices"
+SET "project_id" = (SELECT "models"."project_id"
+ FROM "models"
+ WHERE "models"."id" = "prices"."model_id");
diff --git a/packages/shared/prisma/schema.prisma b/packages/shared/prisma/schema.prisma
index 6390123915..e14680d8b0 100644
--- a/packages/shared/prisma/schema.prisma
+++ b/packages/shared/prisma/schema.prisma
@@ -162,6 +162,7 @@ model Project {
automationExecutions AutomationExecution[]
Automation Automation[]
DefaultLlmModel DefaultLlmModel[]
+ Price Price[]
@@index([orgId])
@@map("projects")
@@ -755,6 +756,8 @@ model Price {
updatedAt DateTime @default(now()) @updatedAt @map("updated_at")
modelId String @map("model_id") // Model is already linked to project (or default), so we don't need projectId here
Model Model @relation(fields: [modelId], references: [id], onDelete: Cascade)
+ projectId String? @map("project_id")
+ project Project? @relation(fields: [projectId], references: [id], onDelete: Cascade)
usageType String @map("usage_type")
price Decimal
diff --git a/web/src/__tests__/model-definitions.servertest.ts b/web/src/__tests__/model-definitions.servertest.ts
index 3edde32dee..e14719db74 100644
--- a/web/src/__tests__/model-definitions.servertest.ts
+++ b/web/src/__tests__/model-definitions.servertest.ts
@@ -40,9 +40,19 @@ describe("/models API Endpoints", () => {
});
await prisma.price.createMany({
data: [
- { modelId: "model-1", usageType: "input", price: 0.001 },
- { modelId: "model-1", usageType: "output", price: 0.002 },
- { modelId: "model-1", usageType: "total", price: 0.1 },
+ {
+ modelId: "model-1",
+ projectId: null,
+ usageType: "input",
+ price: 0.001,
+ },
+ {
+ modelId: "model-1",
+ projectId: null,
+ usageType: "output",
+ price: 0.002,
+ },
+ { modelId: "model-1", projectId: null, usageType: "total", price: 0.1 },
],
});
await prisma.model.create({
@@ -60,8 +70,18 @@ describe("/models API Endpoints", () => {
});
await prisma.price.createMany({
data: [
- { modelId: "model-2", usageType: "input", price: 0.002 },
- { modelId: "model-2", usageType: "output", price: 0.004 },
+ {
+ modelId: "model-2",
+ projectId: null,
+ usageType: "input",
+ price: 0.02,
+ },
+ {
+ modelId: "model-2",
+ projectId: null,
+ usageType: "output",
+ price: 0.04,
+ },
],
});
});
diff --git a/web/src/pages/api/public/models/index.ts b/web/src/pages/api/public/models/index.ts
index 7144d2057d..5aaa71243b 100644
--- a/web/src/pages/api/public/models/index.ts
+++ b/web/src/pages/api/public/models/index.ts
@@ -109,6 +109,7 @@ export default withMiddlewares({
tx.price.create({
data: {
modelId: createdModel.id,
+ projectId: createdModel.projectId,
usageType,
price: price as number, // type guard checked in array filter
},
diff --git a/web/src/server/api/routers/models.ts b/web/src/server/api/routers/models.ts
index 1404ead95a..b72fbb2385 100644
--- a/web/src/server/api/routers/models.ts
+++ b/web/src/server/api/routers/models.ts
@@ -292,6 +292,7 @@ export const modelRouter = createTRPCRouter({
)
.map(([usageType, price]) => ({
modelId: upsertedModel.id,
+ projectId: upsertedModel.projectId,
usageType,
price,
})),
diff --git a/worker/src/scripts/upsertDefaultModelPrices.ts b/worker/src/scripts/upsertDefaultModelPrices.ts
index efcdbbf5ad..417e4d8993 100644
--- a/worker/src/scripts/upsertDefaultModelPrices.ts
+++ b/worker/src/scripts/upsertDefaultModelPrices.ts
@@ -178,6 +178,7 @@ export const upsertDefaultModelPrices = async (force = false) => {
},
create: {
modelId: defaultModelPrice.id,
+ projectId: null,
usageType,
price,
createdAt: defaultModelPrice.created_at,
diff --git a/worker/src/services/IngestionService/tests/IngestionService.integration.test.ts b/worker/src/services/IngestionService/tests/IngestionService.integration.test.ts
index 7f1c567af9..be433ae74d 100644
--- a/worker/src/services/IngestionService/tests/IngestionService.integration.test.ts
+++ b/worker/src/services/IngestionService/tests/IngestionService.integration.test.ts
@@ -1247,6 +1247,7 @@ describe("Ingestion end-to-end tests", () => {
data: {
id: "cm2uio8ef006mh6qlzc2mqa0e",
modelId: "clyrjpbe20000t0mzcbwc42rg",
+ projectId: null,
price: 0.00000015,
usageType: "input",
},
@@ -1256,6 +1257,7 @@ describe("Ingestion end-to-end tests", () => {
data: {
id: "cm2uio8ef006oh6qlldn36376",
modelId: "clyrjpbe20000t0mzcbwc42rg",
+ projectId: null,
price: 0.0000006,
usageType: "output",
},
@@ -1363,6 +1365,7 @@ describe("Ingestion end-to-end tests", () => {
data: {
id: "cm2uio8ef006mh6qlzc2mqa0e",
modelId: "clyrjpbe20000t0mzcbwc42rg",
+ projectId: null,
price: 0.00000015,
usageType: "input",
},
@@ -1372,6 +1375,7 @@ describe("Ingestion end-to-end tests", () => {
data: {
id: "cm2uio8ef006oh6qlldn36376",
modelId: "clyrjpbe20000t0mzcbwc42rg",
+ projectId: null,
price: 0.0000006,
usageType: "output",
},
diff --git a/worker/src/services/IngestionService/tests/calculateTokenCost.unit.test.ts b/worker/src/services/IngestionService/tests/calculateTokenCost.unit.test.ts
index 908b572fef..a64a037476 100644
--- a/worker/src/services/IngestionService/tests/calculateTokenCost.unit.test.ts
+++ b/worker/src/services/IngestionService/tests/calculateTokenCost.unit.test.ts
@@ -81,6 +81,7 @@ describe("Token Cost Calculation", () => {
prisma.price.createMany({
data: modelPrices.map((price) => ({
modelId,
+ projectId: null,
usageType: price.usageType,
price: price.price,
})),