Skip to content

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

feat(api-models): return model prices on public API (#7046)

Hassieb Pakzadedf073cc1859
diff --git a/fern/apis/server/definition/commons.yml b/fern/apis/server/definition/commons.yml
index b98fd085a5..8b97a68aeb 100644
--- a/fern/apis/server/definition/commons.yml
+++ b/fern/apis/server/definition/commons.yml
@@ -457,13 +457,13 @@ types:
docs: Unit used by this model.
type: optional<ModelUsageUnit>
inputPrice:
- docs: Price (USD) per input unit
+ docs: Deprecated. See 'prices' instead. Price (USD) per input unit
type: optional<double>
outputPrice:
- docs: Price (USD) per output unit
+ docs: Deprecated. See 'prices' instead. Price (USD) per output unit
type: optional<double>
totalPrice:
- docs: Price (USD) per total unit. Cannot be set if input or output price is set.
+ docs: Deprecated. See 'prices' instead. Price (USD) per total unit. Cannot be set if input or output price is set.
type: optional<double>
tokenizerId:
docs: Optional. Tokenizer to be applied to observations which match to this model. See docs for more details.
@@ -473,6 +473,13 @@ types:
type: optional<unknown>
isLangfuseManaged:
type: boolean
+ prices:
+ docs: Price (USD) by usage type
+ type: map<string, ModelPrice>
+
+ ModelPrice:
+ properties:
+ price: double
# Utilities
ModelUsageUnit:
diff --git a/web/public/generated/api/openapi.yml b/web/public/generated/api/openapi.yml
index f3de64642d..5214c02886 100644
--- a/web/public/generated/api/openapi.yml
+++ b/web/public/generated/api/openapi.yml
@@ -5268,19 +5268,19 @@ components:
type: number
format: double
nullable: true
- description: Price (USD) per input unit
+ description: Deprecated. See 'prices' instead. Price (USD) per input unit
outputPrice:
type: number
format: double
nullable: true
- description: Price (USD) per output unit
+ description: Deprecated. See 'prices' instead. Price (USD) per output unit
totalPrice:
type: number
format: double
nullable: true
description: >-
- Price (USD) per total unit. Cannot be set if input or output price
- is set.
+ Deprecated. See 'prices' instead. Price (USD) per total unit. Cannot
+ be set if input or output price is set.
tokenizerId:
type: string
nullable: true
@@ -5294,11 +5294,26 @@ components:
JSON. See docs for more details.
isLangfuseManaged:
type: boolean
+ prices:
+ type: object
+ additionalProperties:
+ $ref: '#/components/schemas/ModelPrice'
+ description: Price (USD) by usage type
required:
- id
- modelName
- matchPattern
- isLangfuseManaged
+ - prices
+ ModelPrice:
+ title: ModelPrice
+ type: object
+ properties:
+ price:
+ type: number
+ format: double
+ required:
+ - price
ModelUsageUnit:
title: ModelUsageUnit
type: string
diff --git a/web/src/__tests__/model-definitions.servertest.ts b/web/src/__tests__/model-definitions.servertest.ts
index fea86e7b8f..3edde32dee 100644
--- a/web/src/__tests__/model-definitions.servertest.ts
+++ b/web/src/__tests__/model-definitions.servertest.ts
@@ -38,6 +38,13 @@ describe("/models API Endpoints", () => {
unit: "TOKENS",
},
});
+ 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 },
+ ],
+ });
await prisma.model.create({
data: {
id: "model-2",
@@ -51,6 +58,12 @@ describe("/models API Endpoints", () => {
unit: "TOKENS",
},
});
+ await prisma.price.createMany({
+ data: [
+ { modelId: "model-2", usageType: "input", price: 0.002 },
+ { modelId: "model-2", usageType: "output", price: 0.004 },
+ ],
+ });
});
afterEach(async () => await pruneDatabase());
@@ -67,6 +80,11 @@ describe("/models API Endpoints", () => {
expect(models.body.data[0]).toMatchObject({
isLangfuseManaged: true,
modelName: "gpt-3.5-turbo",
+ prices: {
+ input: { price: 0.001 },
+ output: { price: 0.002 },
+ total: { price: 0.1 },
+ },
});
});
@@ -105,6 +123,10 @@ describe("/models API Endpoints", () => {
auth,
);
expect(customModel.body.isLangfuseManaged).toBe(false);
+ expect(customModel.body.prices).toMatchObject({
+ input: { price: 0.002 },
+ output: { price: 0.004 },
+ });
const models = await makeZodVerifiedAPICall(
GetModelsV1Response,
@@ -132,6 +154,10 @@ describe("/models API Endpoints", () => {
unit: "TOKENS",
tokenizerConfig: { tokensPerMessage: 3, tokensPerName: 1 },
isLangfuseManaged: false,
+ prices: {
+ input: { price: 0.002 },
+ output: { price: 0.004 },
+ },
});
const prices = await prisma.price.findMany({
diff --git a/web/src/features/public-api/types/models.ts b/web/src/features/public-api/types/models.ts
index 25f18ae21b..9939363876 100644
--- a/web/src/features/public-api/types/models.ts
+++ b/web/src/features/public-api/types/models.ts
@@ -6,6 +6,7 @@ import {
publicApiPaginationZod,
} from "@langfuse/shared";
import { z } from "zod";
+import { type Decimal } from "decimal.js";
/**
* Objects
@@ -29,11 +30,12 @@ const APIModelDefinition = z
inputPrice: z.number().nonnegative().nullable(),
outputPrice: z.number().nonnegative().nullable(),
totalPrice: z.number().nonnegative().nullable(),
- unit: APIModelUsageUnit,
+ unit: APIModelUsageUnit.nullish(),
tokenizerId: z.string().nullable(),
tokenizerConfig: z.any(), // Assuming Prisma.JsonValue is any type
isLangfuseManaged: z.boolean(),
createdAt: z.coerce.date(),
+ prices: z.record(z.string(), z.object({ price: z.number() })),
})
.strict();
@@ -49,8 +51,11 @@ export function prismaToApiModelDefinition({
unit,
// eslint-disable-next-line @typescript-eslint/no-unused-vars
updatedAt,
+ Price,
...model
-}: PrismaModel): z.infer<typeof APIModelDefinition> {
+}: PrismaModel & { Price: { usageType: string; price: Decimal }[] }): z.infer<
+ typeof APIModelDefinition
+> {
return {
...model,
unit: unit as PrismaModelUsageUnit,
@@ -58,6 +63,14 @@ export function prismaToApiModelDefinition({
outputPrice: outputPrice?.toNumber() ?? null,
totalPrice: totalPrice?.toNumber() ?? null,
isLangfuseManaged: !Boolean(projectId),
+ prices: Price.reduce(
+ (acc, p) => {
+ acc[p.usageType] = { price: p.price.toNumber() };
+
+ return acc;
+ },
+ {} as z.infer<typeof APIModelDefinition>["prices"],
+ ),
};
}
diff --git a/web/src/pages/api/public/models/[modelId]/index.ts b/web/src/pages/api/public/models/[modelId]/index.ts
index 8aae6ca42a..49d7534cba 100644
--- a/web/src/pages/api/public/models/[modelId]/index.ts
+++ b/web/src/pages/api/public/models/[modelId]/index.ts
@@ -35,6 +35,11 @@ export default withMiddlewares({
},
],
},
+ include: {
+ Price: {
+ select: { usageType: true, price: true },
+ },
+ },
});
if (!model) {
throw new LangfuseNotFoundError("No model with this id found.");
diff --git a/web/src/pages/api/public/models/index.ts b/web/src/pages/api/public/models/index.ts
index bba9fed75e..7144d2057d 100644
--- a/web/src/pages/api/public/models/index.ts
+++ b/web/src/pages/api/public/models/index.ts
@@ -11,6 +11,7 @@ import {
import { InvalidRequestError } from "@langfuse/shared";
import { isValidPostgresRegex } from "@/src/features/models/server/isValidPostgresRegex";
import { auditLog } from "@/src/features/audit-logs/auditLog";
+import { type Decimal } from "decimal.js";
export default withMiddlewares({
GET: createAuthedProjectAPIRoute({
@@ -39,6 +40,11 @@ export default withMiddlewares({
},
},
],
+ include: {
+ Price: {
+ select: { usageType: true, price: true },
+ },
+ },
take: query.limit,
skip: (query.page - 1) * query.limit,
});
@@ -67,6 +73,7 @@ export default withMiddlewares({
};
},
}),
+
POST: createAuthedProjectAPIRoute({
name: "Create custom model definition",
bodySchema: PostModelsV1Body,
@@ -122,7 +129,15 @@ export default withMiddlewares({
return createdModel;
});
- return prismaToApiModelDefinition(model);
+ return prismaToApiModelDefinition({
+ ...model,
+ Price: (["inputPrice", "outputPrice", "totalPrice"] as const)
+ .filter((key) => model[key] != null)
+ .map((key) => ({
+ usageType: key.split("Price")[0],
+ price: model[key] as Decimal,
+ })),
+ });
},
}),
});