One agent execution = one trace. Use
lemma.trace() as the boundary around the run.The root trace
- TypeScript
- Python
Trace handles and span handles
Use the callback form when one function owns the whole run. In TypeScript, use a trace handle when work is coordinated across several helpers and you want to pass IDs around explicitly. In Python, keep the root trace inlemma.trace() / lemma.async_trace() and use span, tool, or generation handles inside the callback.
- TypeScript
- Python
trace.id. Calls are flushed to Lemma as trace snapshots, and trace.end({ output, durationMs }) performs a final flush. Callback traces in both SDKs measure total trace duration automatically; pass durationMs / duration_ms only when you already measured it.
Record by ID
Helpers can attach work to a trace when they only have IDs from the caller. This detached helper API is TypeScript-specific.traceId. If a detached span, generation, or tool belongs under a parent span, pass parentSpanId; calls that cannot attach safely warn and no-op.
Override output or record errors
Return values are captured automatically. Usetrace.output() only when the recorded output should differ from the return value.
- TypeScript
- Python
Pass trace context to helpers
Pass the trace context into nested helper functions that need to record child work:- TypeScript
- Python
Add the work inside
- Generations for LLM calls (model, prompt, completion).
- Tool calls for tool invocations (name, args, result).
- Spans for everything else (retrieval, ranking, app logic).
Next steps
Generations
Capture LLM calls with model, prompt, completion, and timing.
Tool calls
Record tool arguments and results.
Threads & context
Group conversations and attach users.
Trace contract
The exact shape Lemma reads.