DocsObservabilityFeaturesTraces

Traces

A trace groups together multiple observations that are part of the same request or operation.

For example, when a user asks a question to a chatbot, that interaction, from the user’s question to the bot’s response, is captured as one trace. It contains the overall input and output of the function, as well as metadata about the request ( i.e. user, session, tags, etc.).

Most integrations will automatically create a trace for you. However, you can also create a trace manually using the Langfuse SDK.

Trace IDs & Distributed Tracing

Langfuse allows you to bring your own trace IDs (e.g., messageId, traceId, correlationId) for

  • distributed tracing
  • and linking traces across services for lookups between services.
💡

By default, Langfuse assigns random IDs (uuid, cuid) to all logged events. For the OTEL-based SDKs, Langfuse assigns random 32 hexchar trace IDs and 16 hexchar observation IDs.

It is recommended to use your own domain specific IDs (e.g., messageId, traceId, correlationId) as it helps with downstream use cases like:

Data Model

Trace IDs in Langfuse:

  • Must be unique within a project
  • Are used to identify and group related observations
  • Can be used for distributed tracing across services
  • Support upsert operations (creating or updating based on ID)
  • For the OTEL-based SDKs, trace IDs are 32 hexchar lowercase strings and observation IDs are 16 hexchar lowercase strings

Usage

The Python SDK uses W3C Trace Context IDs by default, which are:

  • 32-character lowercase hexadecimal string for trace IDs
  • 16-character lowercase hexadecimal string for observation (span) IDs

You can inspect and reuse these IDs programmatically:

  • langfuse.get_current_trace_id() returns the trace ID of the active observation.
  • langfuse.get_current_observation_id() returns the observation (span) ID of the active observation.
  • span.trace_id and span.id expose the IDs on any LangfuseSpan/LangfuseGeneration returned from start_as_current_observation.
  • Langfuse.create_trace_id(seed: Optional[str] = None) is a static helper to deterministically pre-generate trace IDs before a trace exists (e.g., to correlate an external request ID).

Using the Decorator

from langfuse import observe, get_client
import uuid
 
@observe()
def process_user_request(user_id, request_data):
    # Function logic here
    pass
 
# Use custom trace ID by passing it as special keyword argument
external_trace_id = "custom-" + str(uuid.uuid4())
 
# Get a consistent trace ID for the same user
langfuse = get_client()
trace_id = langfuse.create_trace_id(seed=external_trace_id) # 32 hexchar lowercase string, deterministic with seed
 
process_user_request(
    user_id="user_123",
    request_data={"query": "hello"},
    langfuse_trace_id=trace_id
)

Deterministic Trace IDs

You can generate deterministic trace IDs from any string using create_trace_id():

from langfuse import get_client
 
langfuse = get_client()
 
# Generate deterministic trace ID from an external ID
external_id = "request_12345"
trace_id = langfuse.create_trace_id(seed=external_id)
 
# Use this trace ID in a span
with langfuse.start_as_current_observation(
    as_type="span",
    name="process-request",
    trace_context={"trace_id": trace_id}
) as span:
    # Your code here
    pass

Manually Creating Spans with Custom Trace Context

from langfuse import get_client
 
langfuse = get_client()
 
# Use a predefined trace ID with trace_context parameter
with langfuse.start_as_current_observation(
    as_type="span",
    name="my-operation",
    trace_context={
        "trace_id": "abcdef1234567890abcdef1234567890",  # Must be 32 hex chars
        "parent_span_id": "fedcba0987654321"  # Optional, 16 hex chars
    }
) as span:
    print(f"This span has trace_id: {span.trace_id}")
    # Your code here

Accessing Current Trace & Observation IDs

from langfuse import get_client
 
langfuse = get_client()
 
with langfuse.start_as_current_observation(as_type="span", name="outer-operation") as span:
    # Access the IDs of the current span
    current_trace_id = langfuse.get_current_trace_id()
    current_span_id = langfuse.get_current_observation_id()
 
    print(f"Trace ID from helper: {current_trace_id}")
    print(f"Observation ID from helper: {current_span_id}")
    print(f"Trace ID from object: {span.trace_id}")
    print(f"Observation ID from object: {span.id}")
 

Pre-generating Trace IDs

Use the static helper when you need a trace ID before creating any spans (for example, to pass IDs between services or to link later scores to a trace that does not yet exist):

from langfuse import get_client, Langfuse
 
langfuse = get_client()
 
external_request_id = "req_12345"
deterministic_trace_id = Langfuse.create_trace_id(seed=external_request_id)
print(f"Deterministic Trace ID for {external_request_id}: {deterministic_trace_id}")
 
# Later, start a span that adopts that trace ID
with langfuse.start_as_current_observation(
    as_type="span",
    name="process-request",
    trace_context={"trace_id": deterministic_trace_id}
) as span:
    # Your code here
Was this page helpful?