DocsFeaturesSampling

Sampling

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

Sampling is not yet supported by the JS SDK and integrations. Please upvote this feature request if you are interested in this.

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",
)

GitHub Discussions

Was this page useful?

Questions? We're here to help

Subscribe to updates