DocsTracing FeaturesSampling

Sampling

Sampling can be used to control the volume of traces collected by the Langfuse server.

You can configure the sample rate by setting the LANGFUSE_SAMPLE_RATE environment variable or by using the sample_rate parameter in the constructors of the Python SDK. The value has to be between 0 and 1.

The default value is 1, meaning that all traces are collected. A value of 0.2 means that only 20% of the traces are collected. The SDK samples on the trace level meaning that if a trace is sampled, all observations and scores within that trace will be sampled as well.

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

With Python SDK v3, you can configure sampling when initializing the client:

from langfuse import Langfuse
 
# Either set the environment variable or the constructor parameter
# The constructor parameter takes precedence
import os
os.environ["LANGFUSE_SAMPLE_RATE"] = "0.5"  # As string in env var
 
# Or directly in the constructor (as float)
langfuse = Langfuse(sample_rate=0.5)  # 50% of traces will be sampled

When using the @observe() decorator:

from langfuse import observe, Langfuse
 
# Initialize the client with sampling
Langfuse(sample_rate=0.3)  # 30% of traces will be sampled
 
@observe()
def process_data():
    # Only ~30% of calls to this function will generate traces
    # The decision is made at the trace level (first span)
    pass

If a trace is not sampled, none of its observations (spans or generations) or associated scores will be sent to Langfuse, which can significantly reduce data volume for high-traffic applications.

GitHub Discussions

Was this page useful?

Questions? We're here to help

Subscribe to updates