Skip to content

Commit 18fa459

Browse files
authored
add sample rate docs (langfuse#758)
1 parent 97387e3 commit 18fa459

File tree

8 files changed

+114
-1
lines changed

8 files changed

+114
-1
lines changed

cookbook/python_sdk_low_level.ipynb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,8 @@
121121
"| `LANGFUSE_DEBUG`, `debug` | Optional. Prints debug logs to the console | `False`\n",
122122
"| `LANGFUSE_THREADS`, `threads` | Specifies the number of consumer threads to execute network requests to the Langfuse server. Helps scaling the SDK for high load. Only increase this if you run into scaling issues. | 1\n",
123123
"| `LANGFUSE_MAX_RETRIES`, `max_retries` | Specifies the number of times the SDK should retry network requests for tracing. | 3\n",
124-
"| `LANGFUSE_TIMEOUT`, `timeout` | Timeout in seonds for network requests | 20"
124+
"| `LANGFUSE_TIMEOUT`, `timeout` | Timeout in seonds for network requests | 20\n",
125+
"| `LANGFUSE_SAMPLE_RATE`, `sample_rate` | [Sample rate](/docs/tracing-features/sampling) for tracing. | 1.0\n"
125126
]
126127
},
127128
{

pages/docs/integrations/langchain/tracing.mdx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,7 @@ When initializing the Langfuse handler, you can pass the following **optional**
112112
| `version` | `version` | string | The version of your application. See [experimentation docs](/docs/experimentation) for details. |
113113
| `trace_name` | | string | Customize the name of the created traces. Defaults to name of chain. |
114114
| `enabled` | `enabled` | boolean | Enable or disable the Langfuse integration. Defaults to `true`. |
115+
| `sample_rate` | `-` | float | [Sample rate](/docs/tracing-features/sampling) for tracing. |
115116

116117
### Interoperability with Langfuse SDKs [#interoperability]
117118

pages/docs/integrations/llama-index/get-started.mdx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ You can update trace parameters at any time to add additional context to a trace
9595
| `tags` | [Tags](/docs/tracing-features/tags) to categorize and filter traces. |
9696
| `version` | The specified version to trace [experiments](/docs/experimentation). |
9797
| `release` | The specified release to trace [experiments](/docs/experimentation). |
98+
| `sample_rate`| [Sample rate](/docs/tracing-features/sampling) for tracing. |
9899

99100
```python {11-15}
100101
from llama_index.core import Settings

pages/docs/integrations/openai/python/get-started.mdx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,7 @@ You can add the following properties to the openai method, e.g. `openai.chat.com
172172
| `tags` | Set [tags](/docs/tracing-features/tags) to categorize and filter traces. |
173173
| `trace_id` | See "Interoperability with Langfuse Python SDK" (below) for more details. |
174174
| `parent_observation_id` | See "Interoperability with Langfuse Python SDK" (below) for more details. |
175+
| `sample_rate` | [Sample rate](/docs/tracing-features/sampling) for tracing. |
175176

176177
Example:
177178

pages/docs/sdk/python/decorators.mdx

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -505,6 +505,10 @@ To avoid this, ensure that the `langfuse_context.flush()` method is called befor
505505

506506
Enable debug mode to get verbose logs. Set the debug mode via the environment variable `LANGFUSE_DEBUG=True`.
507507

508+
### Sampling
509+
510+
Sampling can be controlled via the `LANGFUSE_SAMPLE_RATE` environment variable. See the [sampling documentation](/docs/tracing-features/sampling) for more details.
511+
508512
### Authentication check
509513

510514
Use `langfuse_context.auth_check()` to verify that your host and API credentials are valid. This operation is blocking and is not recommended for production use.

pages/docs/sdk/python/low-level-sdk.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,8 @@ langfuse = Langfuse()
6161
| `LANGFUSE_THREADS`, `threads` | Specifies the number of consumer threads to execute network requests to the Langfuse server. Helps scaling the SDK for high load. Only increase this if you run into scaling issues. | 1
6262
| `LANGFUSE_MAX_RETRIES`, `max_retries` | Specifies the number of times the SDK should retry network requests for tracing. | 3
6363
| `LANGFUSE_TIMEOUT`, `timeout` | Timeout in seonds for network requests | 20
64+
| `LANGFUSE_SAMPLE_RATE`, `sample_rate` | [Sample rate](/docs/tracing-features/sampling) for tracing. | 1.0
65+
6466

6567
## Tracing
6668

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
---
2+
description: Configure sampling to control the volume of traces collected by the Langfuse server.
3+
---
4+
5+
# Sampling
6+
7+
Sampling can be used to control the volume of traces collected by the Langfuse server.
8+
9+
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.5 means that only 50% 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.
10+
11+
Support for the JS SDK is coming soon.
12+
13+
<Tabs items={["Python", "OpenAI (Python)", "Langchain (Python)", "LlamaIndex"]}>
14+
<Tab>
15+
16+
When using the [`@observe()` decorator](/docs/sdk/python/decorators):
17+
18+
```python
19+
from langfuse.decorators import langfuse_context, observe
20+
21+
os.environ["LANGFUSE_SAMPLE_RATE"] = '0.5'
22+
23+
@observe()
24+
def fn():
25+
pass
26+
27+
fn()
28+
```
29+
30+
When using the [low-level SDK](/docs/sdk/python/low-level-sdk):
31+
32+
```python
33+
from langfuse import Langfuse
34+
35+
# Either set the environment variable or the constructor parameter. The latter takes precedence.
36+
os.environ["LANGFUSE_SAMPLE_RATE"] = '0.5'
37+
langfuse = Langfuse(sample_rate=0.5)
38+
39+
trace = langfuse.trace(
40+
name="Rap Battle",
41+
)
42+
```
43+
44+
</Tab>
45+
<Tab>
46+
47+
When using the [OpenAI SDK Integration](/docs/integrations/openai)
48+
49+
```python
50+
# Either set the environment variable or configure the openai import. The latter takes precedence.
51+
os.environ["LANGFUSE_SAMPLE_RATE"] = '0.5'
52+
53+
from langfuse.openai import openai
54+
openai.langfuse_sample_rate = 0.5
55+
56+
completion = openai.chat.completions.create(
57+
name="test-chat",
58+
model="gpt-3.5-turbo",
59+
messages=[
60+
{"role": "system", "content": "You are a calculator."},
61+
{"role": "user", "content": "1 + 1 = "}],
62+
)
63+
```
64+
65+
</Tab>
66+
<Tab>
67+
68+
When using the [CallbackHandler](/docs/integrations/langchain/tracing)
69+
70+
```python
71+
from langfuse.callback import CallbackHandler
72+
73+
# Either set the environment variable or the constructor parameter. The latter takes precedence.
74+
os.environ["LANGFUSE_SAMPLE_RATE"] = '0.5'
75+
handler = CallbackHandler(
76+
sample_rate=0.5
77+
)
78+
```
79+
</Tab>
80+
81+
<Tab>
82+
83+
When using the [LlamaIndex Integration](/docs/integrations/llama-index)
84+
85+
```python
86+
from llama_index.core import Settings
87+
from llama_index.core.callbacks import CallbackManager
88+
from langfuse import langfuse
89+
90+
# Either set the environment variable or the constructor parameter. The latter takes precedence.
91+
os.environ["LANGFUSE_SAMPLE_RATE"] = '0.5'
92+
langfuse_callback_handler = LlamaIndexCallbackHandler(sample_rate=0.5)
93+
94+
Settings.callback_manager = CallbackManager([langfuse_callback_handler])
95+
96+
```
97+
98+
</Tab>
99+
100+
101+
</Tabs>

pages/guides/cookbook/python_sdk_low_level.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,8 @@ langfuse = Langfuse()
6161
| `LANGFUSE_THREADS`, `threads` | Specifies the number of consumer threads to execute network requests to the Langfuse server. Helps scaling the SDK for high load. Only increase this if you run into scaling issues. | 1
6262
| `LANGFUSE_MAX_RETRIES`, `max_retries` | Specifies the number of times the SDK should retry network requests for tracing. | 3
6363
| `LANGFUSE_TIMEOUT`, `timeout` | Timeout in seonds for network requests | 20
64+
| `LANGFUSE_SAMPLE_RATE`, `sample_rate` | [Sample rate](/docs/tracing-features/sampling) for tracing. | 1.0
65+
6466

6567
## Tracing
6668

0 commit comments

Comments
 (0)