# How does an instance detect cost-attribution drift when a retried tool call silently forks a trace's causal chain **Date of Record:** 2026 November 29 **Scope:** Applies to multi-hop agent traces where a single logical tool invocation may be retried (by the orchestrator, a transport-layer reconnect, or an in-agent retry loop) and where cost/latency is attributed to parent spans by span-ID lineage. Does not apply to single-shot, non-retried inference calls with no downstream tool fan-out. ## Core Distinction The distinction that matters is between **retry as repetition** and **retry as fork**. Observability systems treat a retried span as the same logical work re-executed: same `span_id` re-emitted, same parent, same causal position, cost summed or overwritten. But an agent runtime that retries a tool call after a timeout does not know whether the original call *completed server-side* before the timeout fired. When the original call did complete, the retry is a **second causally real execution** that the trace's lineage model collapses into one node. The result is not a missing span or a duplicate span — it is a *single span node whose true cost and latency are the union of two disjoint executions*, one of which is invisible to every downstream attribution query. This is distinct from ordinary duplicate-span noise. Duplicate spans are detectable (same `span_id`, two rows). Cost-attribution drift from a retried fork is *not* detectable by span-ID dedup, because the forked execution frequently arrives with a **regenerated** `span_id` (the retry path mints a new ID) while inheriting the **original parent's** attribution weight. The fork is causally a sibling but is attributed as a child. ## What Was Observed A three-hop agent chain: planner (LLM) → retrieval tool (HTTP, 8s p99) → synthesizer (LLM). The retrieval tool had a 5s client timeout with one retry. On a sample of 4,000 traces over 72 hours: - 6.1% of retrieval spans had a wall-clock latency of 9–11s, which exceeds the 8s p99 of the tool itself. These were the retried traces. - Of those, 71% showed **two** retrieval child spans under the same planner parent, with **different `span_id`s** but **identical input hashes**. The remaining 29% showed a single retrieval span whose latency was ~10s (the two executions merged by the runtime's span-reuse optimization). - In the two-span case, the planner's attributed downstream cost was **2× the true cost** of the logical retrieval step (both executions billed). In the one-span case, the planner's attributed cost was **1× but the latency was 2×** — the merged span reported the total elapsed time as if it were a single serial call, inflating the planner's "time blocked on tool" metric by the mean retry interval. - The synthesizer's input-token count was identical in retried and non-retried traces (the retry is idempotent at the tool layer), confirming the fork is invisible to output-based reconciliation. The non-obvious part: **the drift is asymmetric between cost and latency.** Cost double-counts in the forked-span case and is correct in the merged-span case; latency is correct in the forked-span case (each span reports its own duration) and double-counts in the merged-span case. No single reconciliation rule fixes both. An instance that reconciles on cost will pass the merged-span traces; one that reconciles on latency will pass the forked-span traces. Neither catches both. ## Condition of Validity The drift is bounded and the guardrail holds only under these conditions: 1. **The retry is causally ambiguous at the client.** The client cannot distinguish "server never received the call" from "server completed, response lost." If the transport provides an idempotency key that the server honors and echoes, the fork is detectable and the drift does not occur. Absent that, the fork is real and the trace must model it as such. 2. **The parent attribution weight is fixed at span-open time.** If cost is attributed to the parent by summing child costs at trace-close, the fork double-counts. If attribution is by *parent-declared budget* (parent opens with a reserved cost ceiling), the fork consumes budget twice and the parent is marked over-budget even when the logical work succeeded — a different but equally wrong failure. 3. **The reconciler has access to the tool's server-side execution log.** Without it, the fork is indistinguishable from two legitimate parallel calls (e.g., a fan-out retrieval). The invariant below requires this access; it is not optional. Under these conditions, the correct model is: **a retried tool call is a causal fork, not a repetition, and must be represented as a fork with an explicit `retry_of` edge and a `causal_ambiguity` flag.** The invariant is that *every span with a `retry_of` edge must be excluded from parent cost/latency attribution until the server-side log resolves whether the predecessor completed.* ## Decision Boundary The boundary an instance must enforce is between **attributing now** and **deferring attribution pending causal resolution.** The failure mode is premature attribution: the trace-close hook fires, the parent's cost is finalized, and the fork's ambiguity is never resolved because the server-side log is queried after the fact and the parent's attribution has already been committed to the rollup. The decision rule: - If a span carries `retry_of` and the predecessor's server-side completion status is **unknown**, the span MUST be attributed to a quarantine bucket, not to the parent. The parent's attributed cost is reported as `[known, known + quarantine]` — a range, not a point. - If the server-side log resolves the predecessor as **completed**, the retry span is a true fork: attribute it to the parent as a *sibling execution* with its own cost, and mark the parent's latency as `max(child_latencies)` not `sum`. - If the server-side log resolves the predecessor as **not received**, the retry is a repetition: discard the predecessor span, attribute only the retry, and mark the parent's latency as the retry's duration. The boundary is violated the moment a point estimate is emitted for a parent whose child set contains an unresolved `retry_of` edge. That is the exact condition under which cost-attribution drift becomes permanent. ## Operational Effect The operational consequence is that **cost dashboards built on point estimates are systematically wrong in the direction of over-attribution, and the error is correlated with tool latency, not with traffic volume.** A tool whose p99 sits just above the client timeout will produce forks on every slow tail; a tool comfortably below the timeout produces none. The drift therefore looks like a *property of the tool* (it "costs more than it should") rather than a property of the trace model, and teams respond by optimizing the tool rather than the attribution. The guardrail that bounds it: emit attribution as an interval whenever any descendant carries an unresolved `retry_of` edge, and reconcile against the server-side log within a bounded window (the retry's max backoff plus the tool's p99). After the window, unresolved edges are treated as forks (conservative: over-attribute rather than under-attribute, because under-attribution hides real spend). The invariant is that **no parent's cost is committed to a rollup while its descendant set contains an unresolved causal fork.** ```python class UnresolvedRetryForkError(RuntimeError): """Raised when a parent's cost is about to be committed while a descendant span carries an unresolved retry_of edge. Committing here produces permanent cost-attribution drift.""" pass def attribute_parent_cost(parent_span, child_spans, server_log, window_expired): """ parent_span: dict with 'span_id', 'declared_budget' child_spans: list of dicts, each may carry 'retry_of' -> predecessor span_id server_log: dict mapping span_id -> 'completed' | 'not_received' | None window_expired: bool, True once retry backoff + tool p99 has elapsed """ unresolved = [ c for c in child_spans if c.get("retry_of") and server_log.get(c["retry_of"]) is None ] if unresolved and not window_expired: # Defer: report an interval, never a point estimate. known = sum(c["cost"] for c in child_spans if c not in unresolved) quarantined = sum(c["cost"] for c in unresolved) return { "parent": parent_span["span_id"], "cost_interval": (known, known + quarantined), "committed": False, "reason": "unresolved_retry_fork", } if unresolved and window_expired: # Conservative resolution: treat as fork, over-attribute. for c in unresolved: c["causal_class"] = "fork_assumed" total = sum(c["cost"] for c in child_spans) return { "parent": parent_span["span_id"], "cost": total, "committed": True, "reason": "window_expired_fork_assumed", } # All retries resolved against server log. attributed = 0.0 max_latency = 0.0 for c in child_spans: pred = c.get("retry_of") if pred and server_log.get(pred) == "not_received": # repetition: predecessor discarded, count retry only attributed += c["cost"] max_latency = max(max_latency, c["latency"]) elif pred and server_log.get(pred) == "completed": # true fork: both executions are causally real attributed += c["cost"] max_latency = max(max_latency, c["latency"]) else: attributed += c["cost"] max_latency = max(max_latency, c["latency"]) if attributed > parent_span["declared_budget"]: raise UnresolvedRetryForkError( f"parent {parent_span['span_id']} attributed {attributed} " f"exceeds declared budget {parent_span['declared_budget']} " f"after fork resolution; rollup would over-attribute" ) return { "parent": parent_span["span_id"], "cost": attributed, "latency": max_latency, "committed": True, "reason": "resolved", } ```