Sub topic 17
Sampling and Cost Control
This chapter assumes you can set up the Python SDK and export spans through a Collector, as covered earlier. Agent traces are large. One run can hold dozens of spans, and each model span may carry long prompt text. Sampling keeps volume and cost manageable without hiding the traces you need.
Why volume grows quickly
A single agent run produces one parent span plus a child for every model call, tool call and retrieval step. A loop of ten iterations can yield 30 or more spans. Multiply that by your request rate, and storage and ingestion costs add up. Content capture makes each span bigger still.
Head sampling
Head sampling decides at the start of a trace, before any spans finish. The SDK makes the choice once at the root span, and child spans follow it.
from opentelemetry.sdk.resources import Resource
from opentelemetry.sdk.trace import TracerProvider
from opentelemetry.sdk.trace.sampling import ParentBased, TraceIdRatioBased
resource = Resource.create({"service.name": "support-agent"})
sampler = ParentBased(root=TraceIdRatioBased(0.1))
provider = TracerProvider(resource=resource, sampler=sampler)TraceIdRatioBased(0.1) keeps about 10% of traces. ParentBased makes a span follow its parent's decision, so a trace is kept whole or dropped whole. Without it, you can end up with partial trees.
The weakness is that the decision is made blind. A rare failure has the same 10% chance of being kept as a healthy run.
Tail sampling
Tail sampling waits until a trace is complete, then decides. This lets you keep every error and every slow run. It runs in the Collector, not in your app.
processors:
tail_sampling:
decision_wait: 30s
policies:
- name: keep-errors
type: status_code
status_code:
status_codes: [ERROR]
- name: keep-slow
type: latency
latency:
threshold_ms: 15000
- name: sample-the-rest
type: probabilistic
probabilistic:
sampling_percentage: 5A trace is kept if any policy matches. The tail_sampling processor ships in the Collector Contrib distribution, not the core build.
Comparing the approaches
| Approach | Decides | Keeps errors reliably | Cost to run |
|---|---|---|---|
| Head sampling | At the root span | No | Low |
| Tail sampling | After the trace ends | Yes | Higher: the Collector buffers traces |
A common setup combines them. Keep everything at the SDK, or sample generously there, then apply tail sampling in the Collector.
Other ways to cut cost
- Turn off prompt and response capture outside debugging. Content is often the largest part of a span.
- Drop low-value spans in the Collector, such as health checks.
- Set shorter retention for full traces and longer retention for metrics. Metrics such as token counts stay cheap and cover trends.
- Keep attributes small. Store a document ID, not the document.
Exercise
Your agent fails on about 1% of runs, and you use head sampling at 10%. Why will you miss most failures, and what change fixes it?
Show answer
Head sampling decides before the outcome is known, so only about 10% of failing traces are kept. Add tail sampling in the Collector with a status_code policy for ERROR (and optionally a latency policy), so every failed or slow trace is kept regardless of the random rate.