Tagging Traces

Tags allow you to categorize and filter traces. You can tag traces (1) when they are created using the Langfuse SDKs and integrations or (2) from the Langfuse UI. To tag a trace, add a list of tags to the tags field of the trace object. Tags are strings and a trace may have multiple tags.

The v3 SDK is currently in beta. Please check out the SDK v3 for more details.

When using the @observe() decorator:

from langfuse import observe, get_client
 
langfuse = get_client()
 
@observe()
def my_function():
    # ... processing logic ...
    # Add tags to the trace
    langfuse.update_current_trace(tags=["tag-1", "tag-2"])

When creating spans or generations directly:

from langfuse import Langfuse
langfuse = Langfuse()
 
# Add tags when creating the root span
with langfuse.start_as_current_span(
    name="my-operation"
) as root_span:
    # Add tags to the trace
    root_span.update_trace(tags=["tag-1", "tag-2"])
 
    # You can add more tags later from any span in the same trace
    with root_span.start_as_current_generation(name="llm-call", model="gpt-4o") as gen:
        # Processing...
        gen.update_trace(tags=["llm-gen"])  # Adds another tag to the same trace

You can also update the tags of the current trace without a direct reference to a span:

with langfuse.start_as_current_span(name="another-operation"):
    # ... processing ...
    langfuse.update_current_trace(tags=["processing", "beta-feature"])

Working with tags

Tags enable you to flexibly add metadata to your traces. You can filter for tags in the Langfuse UI and GET API.

When choosing tags, consider what aspects of the traces you might want to filter for or group by in your analysis. You may use tags to indicate specific versions of your app (‘app-v1’, ‘app-v2’), specific LLM techniques you used (‘rag’, ‘one-shot’, ‘few-shot’), or the environment of your app (‘local’, ‘staging’, ‘prod’). See Intent Classification Notebook for an end-to-end example on how tags can be created programmatically.

GitHub Discussions

Was this page useful?

Questions? We're here to help

Subscribe to updates