Skip to content

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

feat(models): sort models table by lastUsed (#4925)

Hassieb Pakzadbeeef222e48f
diff --git a/web/src/components/table/use-cases/models.tsx b/web/src/components/table/use-cases/models.tsx
index c1dc95a4dc..f981fc5ddf 100644
--- a/web/src/components/table/use-cases/models.tsx
+++ b/web/src/components/table/use-cases/models.tsx
@@ -34,6 +34,7 @@ export type ModelTableRow = {
prices?: Record<string, number>;
tokenizerId?: string;
config?: Prisma.JsonValue;
+ lastUsed?: Date | null;
serverResponse: GetModelResult;
};
@@ -51,6 +52,8 @@ const modelConfigDescriptions = {
"Some tokenizers require additional configuration (e.g. openai tiktoken). See docs for details.",
maintainer:
"Maintainer of the model. Langfuse managed models can be cloned, user managed models can be edited and deleted. To supersede a Langfuse managed model, set the custom model name to the Langfuse model name.",
+ lastUsed:
+ "Start time of the latest generation using this model within last 30 days",
} as const;
export default function ModelTable({ projectId }: { projectId: string }) {
@@ -198,6 +201,20 @@ export default function ModelTable({ projectId }: { projectId: string }) {
) : null;
},
},
+ {
+ accessorKey: "lastUsed",
+ id: "lastUsed",
+ header: "Last used (30d)",
+ headerTooltip: {
+ description: modelConfigDescriptions.lastUsed,
+ },
+ enableHiding: true,
+ size: 120,
+ cell: ({ row }) => {
+ const value: Date | null | undefined = row.getValue("lastUsed");
+ return value?.toLocaleString() ?? "";
+ },
+ },
{
accessorKey: "actions",
header: "Actions",
@@ -246,6 +263,7 @@ export default function ModelTable({ projectId }: { projectId: string }) {
prices: model.prices,
tokenizerId: model.tokenizerId ?? undefined,
config: model.tokenizerConfig,
+ lastUsed: model.lastUsed,
serverResponse: model,
};
};
diff --git a/web/src/features/models/validation.ts b/web/src/features/models/validation.ts
index ade67e9b6f..598f34fce6 100644
--- a/web/src/features/models/validation.ts
+++ b/web/src/features/models/validation.ts
@@ -23,6 +23,7 @@ export const GetModelResultSchema = z.object({
]),
tokenizerId: TokenizerSchema,
prices: PriceMapSchema,
+ lastUsed: z.date().nullish(),
});
export type GetModelResult = z.infer<typeof GetModelResultSchema>;
@@ -76,3 +77,10 @@ export enum PriceUnit {
Per1KUnits = "per 1K units",
Per1MUnits = "per 1M units",
}
+
+export const ModelLastUsedQueryResult = z.array(
+ z.object({
+ modelId: z.string(),
+ lastUsed: z.coerce.date(),
+ }),
+);
diff --git a/web/src/server/api/routers/models.ts b/web/src/server/api/routers/models.ts
index 6b58ffe843..6f106fcabe 100644
--- a/web/src/server/api/routers/models.ts
+++ b/web/src/server/api/routers/models.ts
@@ -5,6 +5,7 @@ import { auditLog } from "@/src/features/audit-logs/auditLog";
import { isValidPostgresRegex } from "@/src/features/models/server/isValidPostgresRegex";
import {
GetModelResultSchema,
+ ModelLastUsedQueryResult,
UpsertModelSchema,
} from "@/src/features/models/validation";
import { throwIfNoProjectAccess } from "@/src/features/rbac/utils/checkProjectAccess";
@@ -13,6 +14,7 @@ import {
protectedProjectProcedure,
} from "@/src/server/api/trpc";
import { ModelUsageUnit, paginationZod } from "@langfuse/shared";
+import { queryClickhouse } from "@langfuse/shared/src/server";
import { TRPCError } from "@trpc/server";
const ModelAllOptions = z.object({
@@ -119,6 +121,54 @@ export const modelRouter = createTRPCRouter({
.parse(allModelsQueryResult);
const totalCount = z.coerce.number().parse(totalCountQuery[0].count);
+ // Check whether model was used in last month
+ const lastUsedQuery = `
+ SELECT
+ internal_model_id as modelId,
+ MAX(start_time) as lastUsed
+ FROM
+ observations
+ WHERE
+ project_id = {projectId: String}
+ AND start_time > now() - INTERVAL 30 DAY
+ AND internal_model_id IS NOT NULL
+ GROUP BY
+ internal_model_id
+ `;
+
+ const lastUsedQueryResult = ModelLastUsedQueryResult.safeParse(
+ await queryClickhouse({
+ query: lastUsedQuery,
+ params: { projectId: input.projectId },
+ }),
+ );
+
+ if (lastUsedQueryResult.success) {
+ const lastUsedMap = new Map(
+ lastUsedQueryResult.data.map((res) => [res.modelId, res.lastUsed]),
+ );
+
+ return {
+ models: allModels
+ .map((m) => ({ ...m, lastUsed: lastUsedMap.get(m.id) }))
+ .sort((a, b) => {
+ const lastUsedA = lastUsedMap.get(a.id);
+ const lastUsedB = lastUsedMap.get(b.id);
+
+ if (lastUsedA && lastUsedB) {
+ return lastUsedB.getTime() - lastUsedA.getTime();
+ } else if (lastUsedA) {
+ return -1;
+ } else if (lastUsedB) {
+ return 1;
+ }
+
+ return 0;
+ }),
+ totalCount,
+ };
+ }
+
return {
models: allModels,
totalCount,