Version: JS SDK v4

TypeScript SDK - Evaluation

Custom scores

You can score traces and observations via the langfuse.score methods on the Langfuse client. This is useful for automated evaluations or for capturing user feedback. See the custom scores documentation for a detailed reference.

import { LangfuseClient } from "@langfuse/client";
import { startObservation, startActiveObservation } from "@langfuse/tracing";
 
const langfuse = new LangfuseClient();
 
// First, create a trace and an observation
const rootSpan = startObservation("my-trace");
const generation = rootSpan.startObservation(
  "my-generation",
  {},
  { asType: "generation" }
);
generation.end();
rootSpan.end();
 
// Score the specific generation (an observation)
langfuse.score.observation(generation, {
  name: "accuracy",
  value: 1,
  comment: "The answer was factually correct.",
});
 
// Score the entire trace
langfuse.score.trace(rootSpan, {
  name: "user-satisfaction",
  value: 0.9,
  comment: "User was happy with the overall result.",
});
 
// You can also score the currently active observation or trace
startActiveObservation("another-trace", (span) => {
  langfuse.score.activeObservation({
    name: "latency-score",
    value: 0.95,
  });
  langfuse.score.activeTrace({
    name: "quality-score",
    value: 0.88,
  });
});
 
// Finally, ensure scores are sent to the server
await langfuse.flush();

Learn more

Was this page helpful?