Sessions: one conversation, many traces
A session groups traces that belong together, such as the turns of one chat. Each turn stays its own trace; the session_id ties them together.
Lesson 9 put one email in one trace. A chat is different: the customer writes, the desk answers, and minutes later the customer writes again. Langfuse's guidance is one trace per turn and one session per conversation, because you never know in advance when a conversation ends.
conversation = ["Hello?", "Where is my order A17?", "Thanks. And B22?"]
for turn in conversation:
with propagate_attributes(session_id="chat-0042", user_id="cust-42"):
print(answer(turn))
langfuse.flush()
roots = [span for span in local_langfuse.SPANS if span["name"] == "answer-ticket"]
print(len({span["trace"] for span in roots}), "traces")
print({span["attributes"]["session.id"] for span in roots})python chat.pyThe three turns made three traces, all with the same session id. In Langfuse's interface the session view replays the three turns in order, and scores can be attached to the whole session (lesson 24).
Pick one to watch it run, step by step.
A session id is any US-ASCII string of at most 200 characters; a longer one is dropped. Use the id your chat system already has for the conversation, so a support agent can move between the two.
- Give the third turn a different session id and print the set again.
- Leave out
session_idand print how many traces there are. - Wrap all three turns in one
start_as_current_observationblock and count the traces.
Every expert started right here.