Langfuse JS/TS SDKs
    Preparing search index...

    Manager for creating and batching score events in Langfuse.

    The ScoreManager handles automatic batching and flushing of score events to optimize API usage. Scores are automatically sent when the queue reaches a certain size or after a time interval.

    Index

    Constructors

    • Internal

      Creates a new ScoreManager instance.

      Parameters

      • params: { apiClient: LangfuseAPIClient }

        Configuration object containing the API client

      Returns ScoreManager

    Accessors

    • get logger(): Logger

      Returns Logger

    Methods

    • Creates a score for the currently active observation.

      This method automatically detects the active OpenTelemetry span and creates an observation-level score. If no active span is found, a warning is logged and the operation is skipped.

      Parameters

      • data: Omit<ScoreBody, "traceId" | "sessionId" | "observationId" | "datasetRunId">

        Score data (traceId and observationId will be auto-populated)

      Returns void

      import { startActiveSpan } from '@langfuse/tracing';

      startActiveSpan({ name: "my-operation" }, (span) => {
      // Inside the active span
      langfuse.score.activeObservation({
      name: "relevance",
      value: 0.95
      });
      });
    • Creates a score for the currently active trace.

      This method automatically detects the active OpenTelemetry span and creates a trace-level score. If no active span is found, a warning is logged and the operation is skipped.

      Parameters

      • data: Omit<ScoreBody, "traceId" | "sessionId" | "observationId" | "datasetRunId">

        Score data (traceId will be auto-populated)

      Returns void

      import { startActiveSpan } from '@langfuse/tracing';

      startActiveSpan({ name: "my-operation" }, (span) => {
      // Inside the active span
      langfuse.score.activeTrace({
      name: "user_satisfaction",
      value: 4,
      comment: "User rated 4 out of 5 stars"
      });
      });
    • Creates a new score event and adds it to the processing queue.

      Scores are queued and sent in batches for efficiency. The score will be automatically sent when the queue reaches the flush threshold or after the flush interval expires.

      Parameters

      • data: ScoreBody

        The score data to create

      Returns void

      langfuse.score.create({
      name: "quality",
      value: 0.85,
      traceId: "trace-123",
      comment: "High quality response"
      });
    • Flushes all pending score events to the Langfuse API.

      This method ensures all queued scores are sent immediately rather than waiting for the automatic flush interval or batch size threshold.

      Returns Promise<void>

      Promise that resolves when all pending scores have been sent

      langfuse.score.create({ name: "quality", value: 0.8 });
      await langfuse.score.flush(); // Ensures the score is sent immediately
    • Creates a score for a specific observation using its OpenTelemetry span.

      This method automatically extracts the trace ID and observation ID from the provided span context.

      Parameters

      • observation: { otelSpan: Span }

        Object containing the OpenTelemetry span

      • data: Omit<ScoreBody, "traceId" | "sessionId" | "observationId" | "datasetRunId">

        Score data (traceId and observationId will be auto-populated)

      Returns void

      import { startSpan } from '@langfuse/tracing';

      const span = startSpan({ name: "my-operation" });
      langfuse.score.observation(
      { otelSpan: span },
      { name: "accuracy", value: 0.92 }
      );
    • Gracefully shuts down the score manager by flushing all pending scores.

      This method should be called before your application exits to ensure all score data is sent to Langfuse.

      Returns Promise<void>

      Promise that resolves when shutdown is complete

      // Before application exit
      await langfuse.score.shutdown();
    • Creates a score for a trace using an OpenTelemetry span.

      This method automatically extracts the trace ID from the provided span context and creates a trace-level score.

      Parameters

      • observation: { otelSpan: Span }

        Object containing the OpenTelemetry span

      • data: Omit<ScoreBody, "traceId" | "sessionId" | "observationId" | "datasetRunId">

        Score data (traceId will be auto-populated)

      Returns void

      import { startSpan } from '@langfuse/tracing';

      const span = startSpan({ name: "my-operation" });
      langfuse.score.trace(
      { otelSpan: span },
      { name: "overall_quality", value: 0.88 }
      );