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 new OTEL-based SDKs (Python v3), 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:
- deeplinking to the trace from your own ui or logs
- evaluating and adding custom metrics to the trace
- fetching the trace from the API
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 new OTEL-based SDKs (Python v3), trace IDs are 32 hexchar lowercase strings and observation IDs are 16 hexchar lowercase strings
Usage
The v3 SDK is currently in beta. Please check out the SDK v3 for more details.
The Python SDK v3 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
Using the Decorator
from langfuse import observe
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
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 Langfuse
langfuse = Langfuse()
# 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_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 Langfuse
langfuse = Langfuse()
# Use a predefined trace ID with trace_context parameter
with langfuse.start_as_current_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 ID
from langfuse import Langfuse
langfuse = Langfuse()
with langfuse.start_as_current_span(name="outer-operation") as span:
# Access the trace ID of the current span
current_trace_id = langfuse.get_current_trace_id()
current_span_id = langfuse.get_current_observation_id()
print(f"Current trace ID: {current_trace_id}")
print(f"Direct access: {span.trace_id}")