Skip to content

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

1.8 KB
Raw Blame
import {
  PostModelsV1Body,
  PostModelsV1Response,
} from "@/src/features/public-api/types/models";
import { createModelForApi } from "@/src/features/models/server/publicApiModelService";
import { defineTool } from "../../../core/define-tool";
import { buildModelUrl } from "@langfuse/shared/src/server";
import { runMcpTool } from "../../../core/run-mcp-tool";
import { z } from "zod";

const CreateModelBaseSchema = z.object({
  modelName: z.string(),
  matchPattern: z.string(),
  startDate: z.string().optional(),
  inputPrice: z.number().nonnegative().optional(),
  outputPrice: z.number().nonnegative().optional(),
  totalPrice: z.number().nonnegative().optional(),
  unit: z.enum([
    "TOKENS",
    "CHARACTERS",
    "MILLISECONDS",
    "SECONDS",
    "REQUESTS",
    "IMAGES",
  ]),
  tokenizerId: z.enum(["openai", "claude"]).optional(),
  tokenizerConfig: z.any().optional(),
  pricingTiers: z.array(z.any()).optional(),
});

export const [createModelTool, handleCreateModel] = defineTool({
  name: "createModel",
  description:
    "Create a custom model definition for cost tracking/tokenization in the current project.",
  action: "models:CUD",
  baseSchema: CreateModelBaseSchema,
  inputSchema: PostModelsV1Body,
  handler: async (input, context) =>
    runMcpTool({
      spanName: "mcp.models.create",
      context,
      attributes: { "mcp.model_name": input.modelName },
      fn: async () => {
        const result = await createModelForApi({
          projectId: context.projectId,
          input,
          auditScope: context,
        });

        const model = PostModelsV1Response.parse(result);

        return {
          ...model,
          url: buildModelUrl({
            projectId: context.projectId,
            modelId: model.id,
          }),
        };
      },
    }),
});