Langfuse JS/TS SDKs
    Preparing search index...

    Function createTraceId

    • Creates a trace ID for OpenTelemetry spans.

      Parameters

      • Optionalseed: string

        A seed string for deterministic trace ID generation. If provided (non-empty), the same seed will always generate the same trace ID. If empty or falsy, generates a random trace ID.

                  Using a seed is especially useful when trying to correlate external,
                  non-W3C compliant IDs with Langfuse trace IDs. This allows you to later
                  have a method available for scoring the Langfuse trace given only the
                  external ID by regenerating the same trace ID from the external ID.
        

      Returns Promise<string>

      A Promise that resolves to a 32-character lowercase hexadecimal string suitable for use as an OpenTelemetry trace ID.

      // Deterministic trace ID from seed
      const traceId1 = await createTraceId("my-session-123");
      const traceId2 = await createTraceId("my-session-123");
      console.log(traceId1 === traceId2); // true

      // Random trace ID
      const randomId1 = await createTraceId("");
      const randomId2 = await createTraceId("");
      console.log(randomId1 === randomId2); // false

      // Use with spans
      const span = startObservation("my-span", {}, {
      parentSpanContext: {
      traceId: await createTraceId("session-456"),
      spanId: "0123456789abcdef",
      traceFlags: 1
      }
      });

      // Correlating external IDs with Langfuse traces
      const externalId = "ext-12345-67890";
      const traceId = await createTraceId(externalId);

      // Later, when you need to score this trace, regenerate the same ID
      const scoringTraceId = await createTraceId(externalId);
      console.log(traceId === scoringTraceId); // true - can now find and score the trace