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.
When using the @observe() decorator:
from langfuse.decorators import langfuse_context, observe
 
os.environ["LANGFUSE_SAMPLE_RATE"] = '0.5'
 
@observe()
def fn():
    pass
 
fn()When using the low-level SDK:
from langfuse import Langfuse
 
# Either set the environment variable or the constructor parameter. The latter takes precedence.
os.environ["LANGFUSE_SAMPLE_RATE"] = '0.5'
langfuse = Langfuse(sample_rate=0.5)
 
trace = langfuse.trace(
  name="Rap Battle",
)