Skip to main content
Trace-level context turns a pile of individual traces into something you can slice: conversations grouped into threads, traces attributed to users, and custom metadata for filtering. Set these once on the trace and they apply to the whole execution.

Threads (multi-turn conversations)

When one user has a back-and-forth with your agent, each turn is its own trace. Give related turns the same thread id so Lemma groups them into one conversation.
import { propagateAttributes, startActiveObservation } from "@langfuse/tracing";

await startActiveObservation("support-agent", async (root) => {
  root.update({ input: userMessage });

  await propagateAttributes(
    {
      traceName: "support-agent",
      sessionId: conversationId,                     // groups turns into a thread
      metadata: { "lemma.thread_id": conversationId },
    },
    async () => {
      const answer = await runAgent(userMessage);
      root.update({ output: answer });
    },
  );
});
Use the same value across every turn in a conversation. A stable conversation/session id from your app is ideal.

Users

Attribute a trace to the end user so you can find every trace for a given user and analyze per-user behavior.
await propagateAttributes(
  { traceName: "support-agent", userId: user.id },
  async () => {
    /* ... */
  },
);

Custom metadata

Attach domain-specific fields for filtering — environment, plan tier, feature flag, tenant, request id, and so on.
await propagateAttributes(
  {
    traceName: "support-agent",
    metadata: {
      "deployment.environment.name": process.env.NODE_ENV,
      plan: "enterprise",
      tenant_id: tenantId,
    },
  },
  async () => {
    /* ... */
  },
);

Reference

ContextLangfuse field
Agent nametraceName + metadata["gen_ai.agent.name"]
Thread / conversationsessionId (and/or metadata["lemma.thread_id"])
UseruserId
Environment, tenant, custom filtersmetadata
See the trace contract for how each field is read.

Next steps

Trace contract

The exact shape Lemma reads.

Good trace vs bad trace

Conformant vs malformed traces.