Sampling
Sampling can be used to control the volume of traces collected by Langfuse. Sampling is handled client-side.
You can configure the sample rate by setting the LANGFUSE_SAMPLE_RATE
environment variable or by using the sample_rate
/sampleRate
constructor parameter. 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.
With Python SDK v3, you can configure sampling when initializing the client:
from langfuse import Langfuse, get_client import os # Method 1: Set environment variable os.environ["LANGFUSE_SAMPLE_RATE"] = "0.5" # As string in env var langfuse = get_client() # Method 2: Initialize with constructor parameter then get client Langfuse(sample_rate=0.5) # 50% of traces will be sampled langfuse = get_client()
When using the @observe()
decorator:
from langfuse import observe, Langfuse, get_client # 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.