A trace is one end-to-end unit of work — typically a single user request, a single agent turn, or one invocation of a background job. A trace is made up of one or more spans, each representing a step within that unit of work.Documentation Index
Fetch the complete documentation index at: https://docs.trulayer.ai/llms.txt
Use this file to discover all available pages before exploring further.
When to create a trace
Create a new trace whenever a unit of work starts. Common trace boundaries:- One HTTP request to your app
- One message from a user in a chat session
- One iteration of an agent’s reasoning loop
- One cron run or queue-message handler
When to create a span
Create a span for any discrete step inside a trace whose latency, input, output, or errors you want to see separately. Common span types:| Span type | What it captures |
|---|---|
llm | A call to a language model — prompt, response, tokens, model name |
retrieval | A vector search or lookup — query, top-k results, latency |
tool | A tool/function call — arguments, return value |
custom | Any other code block — business logic, parsing, validation |
custom span for the whole iteration, with llm and tool spans inside.
Example: manual instrumentation
Example: auto-instrumentation
For OpenAI, Anthropic, LangChain, and a growing list of frameworks, you don’t need to wrap calls manually — one call toinstrument_*() patches the provider client and every subsequent call becomes a span automatically.
What gets captured
Every span captures:- Input and output — redacted via your
scrub_fnif configured - Latency — wall-clock time from span start to end
- Model — for
llmspans, the model name - Tokens — prompt / completion / total for
llmspans - Error — if the span raised an exception, the message and type
- Metadata — any custom key-value pairs you attach
Performance
The SDK is non-blocking: spans are buffered in-process and flushed to the TruLayer ingest API in a background thread (Python) or viaqueueMicrotask (TypeScript). Your request path is never blocked on network I/O.
Default batch size is 50 spans or 2 seconds, whichever comes first. Tune via configuration.