Learn how to enrich your traces and spans with custom metadata and attributes using the LangWatch Python SDK.
user_id
)session_id
, thread_id
)app_version
)env: "production"
)metadata
dictionary to the langwatch.trace()
decorator or context manager.
user_id
and session_id
are attached to the “UserQueryHandler” trace from the start. Later, query_language
is added.
langwatch.trace()
API reference for more details on its parameters.update()
method on the LangWatchTrace
object.
You can get the current trace object using langwatch.get_current_trace()
or from the langwatch.trace()
context manager.
model_name
, prompt_template_version
, temperature
tool_name
, api_endpoint
, specific input parametersretrieved_document_ids
, chunk_count
attributes
parameter (less common for dynamic values) or, more typically, by calling the update()
method on the LangWatchSpan
object.
The update()
method is flexible and allows you to pass attributes as keyword arguments.
source
, query_complexity
, and items_retrieved
are added to the “FetchResearchData” span. Similarly, model
, prompt_length
, output_length
, and tokens_used
contextualize the “GenerateText” LLM span.
The LangWatchSpan
object also has a set_attributes()
method which takes a dictionary, similar to OpenTelemetry’s underlying span API.
langwatch.span()
API reference and the LangWatchSpan
object methods.Feature | Trace Metadata | Span Attributes |
---|---|---|
Scope | Entire trace (e.g., a whole user request) | Specific span (e.g., one LLM call, one tool use) |
Granularity | Coarse-grained, applies to the overall operation | Fine-grained, applies to a specific part of the operation |
Purpose | General context for the entire operation | Specific details about a particular step or action |
Examples | user_id , session_id , app_version | model_name , tool_parameters , retrieved_chunk_id |
SDK Access | langwatch.trace(metadata={...}) trace.update(metadata={...}) | langwatch.span(attributes={...}) span.update(key=value, ...) span.set_attributes({...}) |
user_id
s or model_name
s).