@launchdarkly/server-sdk-ai - v2.0.21
    Preparing search index...

    Interface LDAIConfigTracker

    The LDAIConfigTracker records metrics for a single AI run.

    All events a tracker emits share a runId (a UUIDv4) so LaunchDarkly can correlate them in metrics views. See individual track methods for their specific semantics. Call createTracker on the AI Config to start a new run. A resumption token preserves the runId, so events emitted by a tracker reconstructed in another process correlate with the original run.

    interface LDAIConfigTracker {
        resumptionToken: string;
        getSummary(): LDAIMetricSummary;
        getTrackData(): {
            configKey: string;
            graphKey?: string;
            modelKey?: string;
            modelName: string;
            modelVersion: number;
            providerName: string;
            runId: string;
            variationKey: string;
            version: number;
        };
        trackDuration(durationMs: number): void;
        trackDurationOf(func: () => Promise<any>): Promise<any>;
        trackError(): void;
        trackFeedback(feedback: { kind: LDFeedbackKind }): void;
        trackJudgeResult(result: LDJudgeResult): void;
        trackMetricsOf<TRes>(
            metricsExtractor: (result: TRes) => LDAIMetrics,
            func: () => Promise<TRes>,
        ): Promise<TRes>;
        trackStreamMetricsOf<TStream>(
            streamCreator: () => TStream,
            metricsExtractor: (stream: TStream) => Promise<LDAIMetrics>,
        ): TStream;
        trackSuccess(): void;
        trackTimeToFirstToken(timeToFirstTokenMs: number): void;
        trackTokens(tokens: LDTokenUsage): void;
        trackToolCall(toolKey: string): void;
        trackToolCalls(toolKeys: string[]): void;
    }
    Index
    resumptionToken: string

    A URL-safe Base64-encoded token that encodes the tracker's runId, configKey, variationKey, and version. Pass this to AIClient.createTracker() to reconstruct the tracker across process boundaries (e.g. for associating deferred feedback with the original AI run).

    • Get the data for tracking.

      Returns {
          configKey: string;
          graphKey?: string;
          modelKey?: string;
          modelName: string;
          modelVersion: number;
          providerName: string;
          runId: string;
          variationKey: string;
          version: number;
      }

    • Track the duration of generation.

      Ideally this would not include overhead time such as network communication.

      Parameters

      • durationMs: number

        The duration in milliseconds.

      Returns void

      Records at most once per Tracker; further calls are ignored.

    • Track the duration of the provided function.

      If the provided function throws, then this method will also throw. In the case the provided function throws, this function will still record the duration.

      This function does not automatically record an error when the function throws.

      Parameters

      • func: () => Promise<any>

        The function to track the duration of.

      Returns Promise<any>

      The result of the function.

      Because each inner metric is at-most-once per Tracker, calling this twice on the same Tracker will run the inner function again but produce no additional metric events.

    • An error was encountered during generation.

      Returns void

      Records at most once per Tracker. trackSuccess and trackError share state; only one of the two can record per Tracker, and subsequent calls are ignored.

    • Track a judge evaluation result.

      No event is emitted when the result was not sampled (result.sampled is false).

      Parameters

      • result: LDJudgeResult

        Judge result containing score, reasoning, and metadata

      Returns void

      May be called multiple times per Tracker; each call records the scores from the given response.

    • Track metrics for a generic AI operation.

      This function will track the duration of the AI run, extract metrics using the provided metrics extractor function, and track success or error status accordingly.

      If the provided function throws, then this method will also throw. In the case the provided function throws, this function will record the duration and an error. A failed AI run will not have any token usage data.

      Type Parameters

      • TRes

      Parameters

      • metricsExtractor: (result: TRes) => LDAIMetrics

        Function that extracts LDAIMetrics from the AI run result

      • func: () => Promise<TRes>

        Function which executes the AI run

      Returns Promise<TRes>

      The result of the AI run

      Subsequent calls re-run the inner function but emit only metrics not already recorded on this Tracker. Call createTracker on the AI Config to start a new run.

    • Track metrics for a streaming AI operation.

      This function will track the duration of the AI run, extract metrics using the provided metrics extractor function, and track success or error status accordingly.

      Unlike trackMetricsOf, this method is designed for streaming AI runs where:

      • The stream is created and returned immediately (synchronously)
      • Metrics are extracted asynchronously in the background once the stream completes
      • Duration is tracked from stream creation to metrics extraction completion

      The stream is returned immediately so the caller can begin consuming it without waiting. Metrics extraction happens in the background and does not block stream consumption.

      If the stream creator throws, then this method will also throw and record an error. If metrics extraction fails, the error is logged but does not affect stream consumption.

      Type Parameters

      • TStream

      Parameters

      • streamCreator: () => TStream

        Function that creates and returns the stream (synchronous)

      • metricsExtractor: (stream: TStream) => Promise<LDAIMetrics>

        Function that asynchronously extracts metrics from the stream

      Returns TStream

      The stream result (returned immediately, not a Promise)

      Subsequent calls re-run the inner function but emit only metrics not already recorded on this Tracker. Call createTracker on the AI Config to start a new run.

    • Generation was successful.

      Returns void

      Records at most once per Tracker. trackSuccess and trackError share state; only one of the two can record per Tracker, and subsequent calls are ignored.

    • Track the time to first token for this generation.

      Parameters

      • timeToFirstTokenMs: number

        The duration in milliseconds.

      Returns void

      Records at most once per Tracker; further calls are ignored.

    • Track a single tool invocation.

      Parameters

      • toolKey: string

        The identifier of the tool that was invoked.

      Returns void

      May be called multiple times per Tracker; each call records the given tool call.

    • Track multiple tool invocations.

      Parameters

      • toolKeys: string[]

        The identifiers of the tools that were invoked.

      Returns void

      May be called multiple times per Tracker; each call records the given tool calls.