Practical technical guides / Agent-era architecture: observability

Tail-Sample Agent Traces by What the Run Did, Then Size the Decision Wait

Keep every agent run that wrote, failed or needed approval by sampling after the run, split traces at durable pauses and carry run-level facts on every segment.

By Sandeep Belgavi · Published 2026-11-26

Tail sampling for segmented agent runs
Original explanatory diagram; synthetic values where shown.

Keep every agent run that wrote, failed or needed approval by sampling after the run, split traces at durable pauses and carry run-level facts on every segment.

Head sampling throws away the runs you most need

Most teams add tracing to an agent the same way they added it to their web services: a head sampler at the entry point keeps a fixed fraction of traces, say 5 percent, and decides at the first span. That works for request handlers whose interesting failures are frequent and uniform. It works badly for agent runs, because the runs worth reading are a small, specific minority: the ones that wrote to a system of record, the ones that asked a person for approval, and the ones that failed halfway. A coin flipped before the run starts cannot know which of those it is looking at.

Agent runs also break a second assumption behind trace sampling, which is that a trace is short. A help-desk run that asks for a refund approval may wait twenty minutes for a person and then continue. If the whole run is one trace, any sampler that decides after a fixed wait sees only the first fragment. This guide works through both problems on a synthetic workload from Belgavi's Book Store and ends with a configuration, a migration path and the cases where head sampling remains the right choice.

Decide after the run, on what the run did

Tail sampling moves the decision to a collector that buffers spans by trace ID and evaluates policies once the trace has had time to finish. The OpenTelemetry Collector tail sampling processor does this with a set of policy types, including status_code, numeric_attribute, boolean_attribute and probabilistic. Each policy votes, and a single sample vote keeps the trace unless another policy votes to drop it. That combination rule is what lets you express "keep every interesting run, plus a small random share of the rest".

For an agent, the interesting facts are cheap for the runtime to record as span attributes, because the runtime already knows them: how many write tools the segment executed, whether the run requested approval, and whether any step failed. The sampler should never have to infer them from span names or log text.

# Author: Sandeep Belgavi. Tail sampling for segmented agent runs (OpenTelemetry Collector contrib).
processors:
  tail_sampling:
    decision_wait: 45s
    num_traces: 20000
    expected_new_traces_per_sec: 10
    decision_cache:
      sampled_cache_size: 200000
    policies:
      - {name: errors, type: status_code, status_code: {status_codes: [ERROR]}}
      - {name: writes, type: numeric_attribute, numeric_attribute: {key: agent.effect.count, min_value: 1}}
      - {name: approval-runs, type: boolean_attribute, boolean_attribute: {key: agent.run.approval_requested, value: true}}
      - {name: run-sample, type: numeric_attribute, numeric_attribute: {key: agent.run.sample_bucket, min_value: 0, max_value: 4}}

The last policy is the important design choice, and the next sections explain it.

Split the run into segments at durable pauses

The processor documentation states that decision_wait defaults to 30 seconds and that spans arriving after a decision are late. While the decision is still in the buffer, late spans inherit it. Once it has been evicted, and with no decision cache configured, the processor treats late spans as a new trace and decides again. For an agent run that pauses for twenty minutes, that means the part after the pause is judged on its own, without the context that came before.

Raising decision_wait to cover the longest pause is not a real option. In the companion simulation, 20,000 runs arrive at 3 per second; 10 percent wait 2 to 30 minutes for an approval and 15 percent of the others wait 10 minutes to 2 hours for a customer reply. If each run were a single trace, 8,858 of the 20,000 would outlast a 30-second decision wait. At this arrival rate, with about 1.24 traces per run, a 30-second window holds about 111 traces in memory; stretching it to 1,800 seconds to cover a 30-minute pause raises that to about 6,690, and the two-hour reply waits would still not fit.

The better structure is to end a trace where the workflow durably pauses and to start a new trace when it resumes. The resumed segment carries the run identifier as an attribute and a span link to the segment before it. Each segment is short enough to decide within a normal decision window, and nothing has to stay in collector memory during the pause.

Carry run-level facts on every segment

Splitting a run creates a new problem. A probabilistic policy hashes the trace ID, so the two segments of the same routine run get independent decisions. In the simulation, sampling routine segments by their own trace ID left 200 of the 2,327 routine multi-segment runs kept partially: one segment present, the other gone. A reader who finds the first half of a run has no way to know whether the second half was dropped or never happened.

The fix is to compute the random choice once per run and carry it. At run start the runtime hashes the run identifier to a number from 0 to 99 and writes it on every span as agent.run.sample_bucket. The run-sample policy keeps buckets 0 to 4, which is 5 percent of runs, and both segments of a chosen run fall in the same bucket. With that change, routine runs kept partially dropped from 200 to 0.

The same rule applies to the approval flag. A segment that resumes after approval may not execute any write if the reviewer, say Geetha, rejected the refund in CASE-14120. If the flag were set only on the segment that asked, the rejected resumption would look routine and could be dropped. Carrying agent.run.approval_requested on every segment of the run kept all 2,045 approval runs complete.

Read the comparison

The companion example runs four policies over the same synthetic runs, which produce 24,776 segment traces.

policy      kept%  error/write kept  approval runs complete  routine partial  kept signal missing earlier segment
head          5.1      227 / 4303          6 / 2045                205                    81
tail         31.0     4303 / 4303       2045 / 2045                  0                    48
tail-tid     30.9     4303 / 4303       2045 / 2045                200                    46
tail+pause   40.1     4303 / 4303       2045 / 2045               2213                     0

Head sampling stores the fewest traces and keeps 227 of 4,303 segments that errored or wrote, which is what a 5 percent coin produces. The outcome rules keep every one of them, at the price of storing 31 percent of segments, because in this synthetic workload a quarter of runs write something. That price is a property of the workload: if most runs write, an outcome rule degenerates into keeping almost everything, and you need finer attributes, such as keeping only money-moving writes.

The last column exposes a limit of any per-segment decision. In 48 cases under the tail policy, a later segment errored or wrote after its earlier routine segment had already been dropped. The decision for a segment cannot depend on what a later segment will do. The tail+pause variant keeps every segment that ends in a durable pause, which removes those 48 gaps but stores 40.1 percent of segments, and it leaves many routine runs with only their first half kept. A cheaper alternative is a small run summary record, written at every pause and at the end, that lists segment trace IDs and outcomes. It is always retained, so a reader can see what a dropped segment did.

What stays deterministic

The sampling decision should be a pure function of span attributes that the runtime sets from facts it controls: effect counts from the tool executor, the approval flag from the workflow state, error status from the step result and the bucket from the run identifier. None of these should come from model output. A model that writes "this run is important" into a span attribute has turned your retention policy into a prompt.

The collector topology is deterministic too, and it is easy to get wrong. All spans of a trace must reach the same collector instance. The load-balancing exporter routes traces by trace ID by default, which is what the tail sampling processor needs. Segment traces make this easier, because each segment is a separate, short trace.

Migrating from head sampling

First, add the attributes while still head sampling: agent.effect.count, agent.run.approval_requested, agent.run.sample_bucket and the run identifier. Verify them on traces you already keep.

Second, end traces at durable pauses and link resumed segments to their predecessors. Check that the median segment duration is far below decision_wait, and alert on the processor metric for traces dropped too early.

Third, move sampling to the collector with the policies above, set the head sampler to keep everything, and size num_traces from the arrival rate of segment traces multiplied by decision_wait, with headroom. Compare the kept share and storage cost with the head-sampled baseline for a week.

Fourth, add the run summary record if readers report missing context, and configure a decision cache so late spans from a kept trace stay kept.

When head sampling is enough

If the agent runs a few hundred times a day, keep everything; tail sampling adds a stateful tier for no benefit. If every run is short, uniform and read-only, such as a classifier that labels incoming messages, the interesting failures are frequent enough that a head sample finds them. Tail sampling earns its cost when rare, consequential outcomes are what you need to inspect.

Verification

Replay a recorded day of spans through a test collector and assert that every segment with an error, a write or an approval flag is exported. Assert that segments sharing a run identifier are either all kept or all dropped when they are routine. Watch the trace removal age against decision_wait under peak load. The companion tests check these invariants on the synthetic runs. The numbers here come from that simulation, not from a production system, and the deeper comparison of head and tail designs is in the trace sampling strategies guide at /blogs/observability/trace_sampling_strategies_deep.html.

Primary references and authorship

Sources checked 2026-11-26. Original explanation and synthetic examples by Sandeep Belgavi. Companion code: Sandeep-BA/agent-decision-lab/examples/python/agent_trace_tail_sampling. Views are my own and do not represent any employer, past or present.