LangfuseLangfuse Python SDK 4.15.4 · Python 3.10+
0%
1
Curious builder0 XP earned · 300 to level 2
0 daysFinish a lesson to begin
Badge collection0 of 6 unlocked
38 small wins to finish your pathNext lesson

One trace per ticket

A support desk is judged one ticket at a time. Giving each ticket its own trace id, user and session makes a trace findable from a ticket number alone.

The run above made a trace with a random id, so a ticket number leads nowhere, and it carries no customer. handle fixes both.

Examplesupport_desk.py, continued
def handle(ticket_number, ticket, customer, chat):
    langfuse = get_client()
    trace_id = langfuse.create_trace_id(seed=ticket_number)
    with langfuse.start_as_current_observation(name="support-ticket", input=ticket,
                                               trace_context={"trace_id": trace_id}) as span:
        with propagate_attributes(user_id=customer, session_id=chat, tags=["desk"]):
            text = answer(ticket)
        span.update(output=text)
    return text

create_trace_id(seed=...) turns a ticket number into the same id every time, so the ticket number is the way back to the trace. The attributes set around the call cover everything inside it: the customer, the conversation and the tag.

Examplehandle_one.py, after the setup lines
print(support_desk.handle("ticket-1042", "Where is my order A17?",
                          customer="cust-42", chat="chat-0042"))

langfuse.flush()
for span in local_langfuse.SPANS:
    attributes = span["attributes"]
    print(f"{span['name']:16} user={attributes.get('user.id')} session={attributes.get('session.id')}")
Example
python handle_one.py

Every observation carries the customer and the chat, not only the first. In Langfuse that is what lets someone open one customer, or one conversation, and read it from end to end.

Try it yourself
  • Call handle twice with the same ticket number and count the traces.
  • Print the trace id the seed produces and compare the two runs.
  • Add a tag of your own and find it on the observations.

Little by little, you're building something great.