Lesson 6 / 31
Sessions, Users, Tags and Metadata
Group traces by conversation and user, and add tags and metadata that make filtering useful.
By Rahul Agarwal · 15 years in web development
Context that turns traces into answers
A single trace answers "what happened on this request". Product questions need grouping. A session groups the traces of one conversation (for example all turns of a chat thread), so you can replay the whole dialogue and see where it went wrong. A user ID links traces to a user, which enables per-user cost, rate-abuse detection and following up on a complaint. Tags are short labels for filtering, such as beta, hindi or checkout-bot. Metadata is a free-form key-value object for things like release version, prompt version, tenant or experiment variant. In Langfuse, session ID, user ID and tags are first-class trace attributes; in LangSmith you add tags and metadata to runs, and threads are grouped using a metadata key such as session_id or thread_id. Choose identifiers that are stable and not personal data: an internal user ID, not an email address or phone number.
Setting trace attributes in Langfuse
Inside an active span you can set trace-level attributes. Values here are illustrative.
from langfuse import get_client
langfuse = get_client()
with langfuse.start_as_current_span(name="support-chat") as span:
langfuse.update_current_trace(
user_id="u-1042", # internal ID, not an email
session_id="chat-77", # all turns of one conversation
tags=["support", "hindi"],
metadata={"release": "2024-w32", "prompt_version": "v4"},
)
span.update(output="...")
langfuse.flush()Railway PNR, passengers and quotas
One ticket check is a trace. The PNR groups all passengers of one booking (a session), the passenger name is the user, and labels like "Tatkal" or "senior citizen" are tags you filter by when something goes wrong.
Quick check: Which value is the best choice for a trace's user ID?
- The user's email address
- A stable internal user identifier
- The user's phone number
- A new random value for every request
Answer
A stable internal user identifier — A stable internal ID lets you group a user's traces without sending personal data to the observability backend. A random per-request value defeats grouping.