DocsTracing FeaturesSessions (Chats, Threads, etc.)

Sessions

Many interactions with LLM applications span multiple traces. Sessions in Langfuse are a way to group these traces together and see a simple session replay of the entire interaction. Get started by adding a sessionId when creating a trace.

Add a sessionId when creating/updating a trace. This can be any string that you use to identify the session. All traces with the same sessionId will be grouped together.

When using the @observe() decorator:

from langfuse import observe, get_client
 
@observe()
def process_request():
    # Get the client
    langfuse = get_client()
 
    # Add to the current trace
    langfuse.update_current_trace(session_id="your-session-id")
 
    # ...your processing logic...
    return result

When creating spans directly:

from langfuse import get_client
 
langfuse = get_client()
 
# You can set the session_id when creating the root span
with langfuse.start_as_current_span(
    name="process-chat-message"
) as root_span:
    # Add session_id to the trace
    root_span.update_trace(session_id="chat-session-123")
 
    # All spans in this trace will belong to the same session
    with root_span.start_as_current_generation(
        name="generate-response",
        model="gpt-4o"
    ) as gen:
        # ...generate response...
        pass

You can also update the session_id of the current trace without a direct reference to a span:

with langfuse.start_as_current_span(name="another-operation"):
    # Add to the current trace
    langfuse.update_current_trace(session_id="your-session-id")

Example

Try this feature using the public example project.

Example session spanning multiple traces

Session view

Other features

  • Publish a session to share with others as a public link (example)
  • Bookmark a session to easily find it later
  • Annotate sessions by adding scores via the Langfuse UI to record human-in-the-loop evaluations

GitHub Discussions

Was this page helpful?