Overview
The@traceable decorator automatically captures inputs, outputs, timing, and metadata for any function in your LLM pipeline. Decorate your functions to build a full trace tree that is submitted to the Avaliar platform for monitoring and analysis.
Parameter Reference
| Parameter | Type | Default | Description |
|---|---|---|---|
span_type | "llm" | "tool" | "agent" | "generic" | required | Type of operation being traced |
model | str | None | LLM model name (llm spans only) |
provider | str | None | Provider name (llm spans only) |
temperature | float | None | Sampling temperature (llm spans only) |
top_p | float | None | Top-p parameter (llm spans only) |
detection | bool | False | Enable safety detection on this span |
detectors | list[DetectorType] | [] | Specific detectors to run |
detection_mode | "local" | "cloud" | "local" | Where detection processing runs |
blocking | bool | False | Submit the prompt to Avaliar before calling the LLM. Raises PromptBlockedError if blocked. Requires Pro plan and span_type="llm". |
Span Types
Each span type captures different metadata and serves a different purpose in your trace tree.llm -- LLM API calls
llm -- LLM API calls
Use for direct calls to language model APIs. Captures messages, model configuration, and token usage.
tool -- Tool and function calls
tool -- Tool and function calls
Use for tool executions, function calls, or any discrete operation performed by your agent.
agent -- Agent orchestration steps
agent -- Agent orchestration steps
Use for high-level agent logic that coordinates multiple sub-operations.
generic -- Any other operation
generic -- Any other operation
Use for any operation that does not fit neatly into the other categories.
Examples
Basic LLM Tracing
The simplest use case: trace an async OpenAI call.Nested Spans
Build a trace tree by nesting@traceable functions. The SDK automatically links parent and child spans using context variables.
process_document calls summarize, the trace tree looks like:
Detection-Enabled Tracing
Add safety detection to any traced function by settingdetection=True and specifying which detectors to run.
Sync Function Tracing
The decorator works with synchronous functions as well.Generator Tracing
Trace generator functions that yield results incrementally. The SDK captures the full concatenated output once the generator completes.Updating token usage
Useupdate_current_llm_run to attach token counts to the current LLM span:
Call
update_current_llm_run from inside the decorated function, before returning. Token counts appear in the Traces dashboard and are included in cost calculations.LLM message extraction
Whenspan_type="llm", the SDK automatically extracts input messages for tracing. It looks for messages in two places:
- The first positional argument:
generate(messages) - The
messageskeyword argument:generate(messages=[...])
How It Works
Context Propagation
When a
@traceable function is called, a new span is created and linked to any active parent span. Nested @traceable calls automatically inherit the parent, building the trace tree as your code executes.Trace Tree Construction
As your code executes, the SDK builds a trace tree from the parent-child relationships between spans. Each span records its inputs, outputs, start time, end time, and any associated metadata.
Traces are submitted in background threads and will never block your application. If the Avaliar API is unreachable, traces are silently dropped — your application continues to run normally.