lemma.trace() call. Record model calls, tool calls, and app work inside that trace. If that execution spans a host and a sandbox, follow Cross-process turns instead of starting a second root.
What you will build
Before you start
The examples assume you already have an agent shaped like this:- TypeScript
- Python
Steps
1
Wrap the whole agent run in a trace
Start with the root boundary. The returned value becomes the trace output.
- TypeScript
- Python
2
Add a span around retrieval work
Use a span for app work that is not itself a model call or tool call.Pass
- TypeScript
- Python
durationMs / duration_ms when you already measured the retrieval time. If you omit it, the SDK records timestamps and Lemma can allocate missing child durations from the parent.3
Record each tool call
Record tools after they return so you can include both arguments and results.Use stable, boring names like
- TypeScript
- Python
search_docs, lookup_order, and rerank_results. They make traces easier to scan.4
Record the model call as a generation
Generations are for LLM calls. Include the model, prompt/input, output, and timing when available.
- TypeScript
- Python
5
Record root trace duration when the run is done
Callback traces are measured automatically when the callback returns. For trace handles, record root duration when you call For open handles, leave duration blank until the work is actually finished. The SDK cannot know the final elapsed time until
trace.end(...).- TypeScript
- Python
span.end(...), trace.end(...), or the callback returns.6
Mark failures where you catch them
lemma.trace() records uncaught errors on the root trace. If you catch a tool or model error before re-raising, record that child failure too.- TypeScript
- Python
Go deeper
Use these pages for field-level detail and framework-specific tracing.Trace contract
The exact fields and native SDK props Lemma reads.
Runnable examples
The same docs chat agent, instrumented once per stack.
Vercel AI SDK
Record AI SDK model calls and tool executions automatically.
Troubleshooting
Fix missing traces, empty outputs, and shape issues.