Trace Sampling
Sending only part of your traces to Confident AI
Overview
Sampling allows you to control what percentage of traces are sent to Confident's observatory.
Sampling is decided once, at the start of each trace. That means a trace is either fully exported or not at all — you'll never see half a trace in the Observatory.
For dropping based on conditions you discover during a request, see dropping traces.
Configure Sample Rate
Configure the sampling rate by setting the CONFIDENT_SAMPLE_RATE environment variable, which represents the proportion of traces that will be sent to the observatory. The value is a ratio between 0 and 1; the default 1.0 exports every trace.
export CONFIDENT_SAMPLE_RATE=0.5Alternatively, you can set the sampling rate directly in code by passing sample_rate / sampleRate to init():
from openai import OpenAI
from confident_trace import init, span, shutdown
init(sample_rate=0.5)
client = OpenAI()
@span(type="agent")
def llm_app(query: str):
return client.chat.completions.create(
model="gpt-4o",
messages=[{"role": "user", "content": query}]
).choices[0].message.content
try:
for _ in range(10):
llm_app("Write me a poem.") # roughly half of these traces will be sent
finally:
shutdown()import OpenAI from "openai";
import { init, span } from "confident-trace";
const runtime = init({ sampleRate: 0.5 });
const openai = new OpenAI();
const llmApp = span({ name: "llm_app", type: "agent" }, async (query: string) => {
const res = await openai.chat.completions.create({
model: "gpt-4o",
messages: [{ role: "user", content: query }],
});
return res.choices[0].message.content;
});
try {
for (let i = 0; i < 10; i++) {
await llmApp("Write me a poem."); // roughly half of these traces will be sent
}
} finally {
await runtime.shutdown();
}Remember to launch your entry point with the Node preload.
Next Steps
Sampling controls volume across the board. For finer control over individual requests, or over what's inside each trace, see:
Drop Traces
Skip tracing entirely for health checks, internal test requests, and other noise you don't want in the Observatory.
Mask Sensitive Data
Redact PII and cap payload sizes before traces leave your process.
Last updated on