diff --git a/docs/observability/how_to_guides/sample_traces.mdx b/docs/observability/how_to_guides/sample_traces.mdx index 8c92ecbd..f9180a80 100644 --- a/docs/observability/how_to_guides/sample_traces.mdx +++ b/docs/observability/how_to_guides/sample_traces.mdx @@ -8,6 +8,8 @@ sidebar_position: 4 This section is relevant for those using the LangSmith SDK or LangChain, not for those logging directly with the LangSmith API. ::: +## Set global sampling rate + By default, all traces are logged to LangSmith. To down-sample the number of traces logged to LangSmith, set the `LANGSMITH_TRACING_SAMPLING_RATE` environment variable to any float between `0` (no traces) and `1` (all traces). @@ -18,3 +20,31 @@ export LANGSMITH_TRACING_SAMPLING_RATE=0.75 ``` This works for the `traceable` decorator and `RunTree` objects. + +## Setting different sampling rates per client + +You can also set sampling rates on specific `Client` instances and using the `tracing_context` context manager: + +```python +from langsmith import Client, tracing_context + +# Create clients with different sampling rates +client_1 = Client(tracing_sampling_rate=0.5) # 50% sampling +client_2 = Client(tracing_sampling_rate=0.25) # 25% sampling +client_no_trace = Client(tracing_sampling_rate=0.0) # No tracing + +# Use different sampling rates for different operations +with tracing_context(client=client_1): + # Your code here - will be traced with 50% sampling rate + agent_1.invoke(...) + +with tracing_context(client=client_2): + # Your code here - will be traced with 25% sampling rate + agent_1.invoke(...) + +with tracing_context(client=client_no_trace): + # Your code here - will not be traced + agent_1.invoke(...) +``` + +This allows you to control sampling rates at the operation level. \ No newline at end of file