> ## Documentation Index
> Fetch the complete documentation index at: https://docs.abv.dev/llms.txt
> Use this file to discover all available pages before exploring further.

# Sampling

Sampling can be used to control the volume of traces collected by ABV. Sampling is handled client-side.

You can configure the sample rate by setting the `ABV_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.

## Implementation with Python SDK

Install packages

```bash theme={null}
pip install abvdev, python-dotenv
```

Add credentials and **ABV\_SAMPLE\_RATE** environment variable:

Add your ABV credentials to your environment variables. Make sure that you have a `.env` file in your project root.

```bash title=".env" theme={null}
ABV_API_KEY = "sk-abv-..."
ABV_HOST = "https://app.abv.dev" # US region
# ABV_HOST = "https://eu.app.abv.dev" # EU region
ABV_SAMPLE_RATE = "0.5"
```

load environment variables and initialize client with get\_client:

```python theme={null}
# Import the function to load the .env file
from dotenv import load_dotenv 
from abvdev import observe, get_client

# Load the environment variables from the .env file
load_dotenv() 

@observe
def my_function():
    return "Hello, world!" # Input/output and timings are automatically captured
 
my_function()
 
# Flush events in short-lived applications
abv = get_client()
abv.flush()
```

Also with Python SDK, you can configure sampling when initializing the client:

```python theme={null}
from abvdev import ABV, observe

# ABV client initialization - # Method 2: Initialize with constructor parameter
abv = ABV(
    sample_rate=0.5,  # 50% of traces will be sampled
    api_key="sk-abv-...", # your api key here
    host="https://app.abv.dev", # host="https://eu.app.abv.dev", for EU region
)

@observe
def my_function():
    return "Hello, world!" # Input/output and timings are automatically captured
 
my_function()
 
# Flush events in short-lived applications
abv.flush()
```

If a trace is not sampled, none of its observations (spans or generations) or associated scores will be sent to ABV, which can significantly reduce data volume for high-traffic applications.

## Implementation with JS/TS SDK

**Install packages**

```bash theme={null}
npm install @abvdev/tracing @abvdev/otel @opentelemetry/sdk-node dotenv
```

**Add credentials**

Add your ABV credentials to your environment variables. Make sure that you have a `.env` file in your project root and a package like dotenv to load the variables.

```bash title=".env" theme={null}
ABV_API_KEY = "sk-abv-..."
ABV_BASE_URL = "https://app.abv.dev" # US region
//ABV_BASE_URL = "https://eu.app.abv.dev" # EU region
```

Create`instrumentation.ts` file and use `dotenv` package to load the variables.

Additional parameters are provided to get trace visible in the UI immediately.

ABV respects OpenTelemetry's sampling decisions. You can configure a sampler in your OTEL SDK to control which traces are sent to ABV. This is useful for managing costs and reducing noise in high-volume applications.

Here is an example of how to configure a `TraceIdRatioBasedSampler` to send only 20% of traces:

```typescript title="instrumentation.ts" theme={null}
import { NodeSDK } from "@opentelemetry/sdk-node";
import { ABVSpanProcessor } from "@abvdev/otel";
import { TraceIdRatioBasedSampler } from "@opentelemetry/sdk-trace-base";
import dotenv from "dotenv";
dotenv.config();

const sdk = new NodeSDK({
  // Sample 20% of all traces
  sampler: new TraceIdRatioBasedSampler(0.2),
  spanProcessors: [
    new ABVSpanProcessor({
      apiKey: process.env.ABV_API_KEY,
      baseUrl: process.env.ABV_BASE_URL,
      exportMode: "immediate",
      flushAt: 1,
      flushInterval: 1,
      additionalHeaders: {
        "Content-Type": "application/json",
        "Accept": "application/json"
      }
    })
  ],
});

sdk.start();
```

Import the `instrumentation.ts` file at the top of your application.

See [JS/TS SDK docs](/developer/sdks/js-ts/overview) for more details.
