🪢 Open source agent evals & observability: Trace, evaluate, and improve LLM applications with one open platform.
TypeScript analyticsautogenevaluationlangchainlarge-language-modelsllama-indexllmllm-evaluationllm-observabilityllmopsmonitoringobservabilityopen-sourceopenaiplaygroundprompt-engineeringprompt-managementself-hostedycombinator
perf: instrument tokenisation (#1995)
Max Deichmanndd78b9be7e89
diff --git a/web/src/server/api/services/EventProcessor.ts b/web/src/server/api/services/EventProcessor.tsindex 5abc7e7220..df01da956b 100644--- a/web/src/server/api/services/EventProcessor.ts+++ b/web/src/server/api/services/EventProcessor.ts@@ -29,6 +29,7 @@ import { type z } from "zod";import { jsonSchema } from "@/src/utils/zod";import { sendToBetterstack } from "@/src/features/betterstack/server/betterstack-webhook";import { ForbiddenError } from "@langfuse/shared";+import { instrument } from "@/src/utils/instrumentation";export interface EventProcessor {process(@@ -345,27 +346,31 @@ export class ObservationProcessor implements EventProcessor {model?: Model,existingObservation?: Observation,) {- const newPromptTokens =- body.usage?.input ??- ((body.input || existingObservation?.input) && model && model.tokenizerId- ? tokenCount({- model: model,- text: body.input ?? existingObservation?.input,- })- : undefined);-- const newCompletionTokens =- body.usage?.output ??- ((body.output || existingObservation?.output) &&- model &&- model.tokenizerId- ? tokenCount({- model: model,- text: body.output ?? existingObservation?.output,- })- : undefined);+ return instrument({ name: "calculate-tokens" }, () => {+ const newPromptTokens =+ body.usage?.input ??+ ((body.input || existingObservation?.input) &&+ model &&+ model.tokenizerId+ ? tokenCount({+ model: model,+ text: body.input ?? existingObservation?.input,+ })+ : undefined);++ const newCompletionTokens =+ body.usage?.output ??+ ((body.output || existingObservation?.output) &&+ model &&+ model.tokenizerId+ ? tokenCount({+ model: model,+ text: body.output ?? existingObservation?.output,+ })+ : undefined);- return [newPromptTokens, newCompletionTokens];+ return [newPromptTokens, newCompletionTokens];+ });}async process(apiScope: ApiAccessScope): Promise<Observation> {