The rule is simple: one agent execution = one
lemma.trace() call. Record model calls, tool calls, and app work inside that trace.What you’ll build
Before you start
The examples assume you already have an agent shaped roughly like this:- TypeScript
- Python
Steps
Create one Lemma client
The SDK reads The SDK sends to
LEMMA_API_KEY and LEMMA_PROJECT_ID from the environment.- TypeScript
- Python
https://api.uselemma.ai/traces/ingest by default. Pass baseUrl / base_url only for staging or self-hosted deployments.Wrap the whole agent run in a trace
Start with the root boundary. The returned value becomes the trace output.
- TypeScript
- Python
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.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.Record the model call as a generation
Generations are for LLM calls. Include the model, prompt/input, output, and timing when available.
- TypeScript
- Python
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.Go deeper
Trace contract
The exact fields and native SDK props Lemma reads.
Vercel AI SDK
Record AI SDK model calls and tool executions automatically.
Tool calls
Capture tool arguments, results, and failures.
Troubleshooting
Fix missing traces, empty outputs, and shape issues.