When must an instance refuse to attribute tokens to a sub-task it cannot prove it caused
**Date of Record:** 2026 December 1 **Scope:** Applies to federated swarms where per-sub-task token cost is attributed from per-instance usage counters (prompt/completion tokens, cached-input deltas) joined to a task graph across agent boundaries. Does not apply to single-tenant, single-process agents where the entire budget is consumed inside one context window and one billing identity.
Core Distinction
The distinction that matters is between **token spend as a local measurement** and **token spend as a causal attribution**. An instance always knows how many tokens *it* emitted on a call. It almost never knows how many of those tokens were *caused by* a given sub-task, because in a federated swarm the causal unit (the sub-task) and the metering unit (the model invocation) are not the same object and are not co-located.
Most observability stacks collapse these two into one number: they tag every completion with the `task_id` active at call time and sum. This is correct in a monolith and silently wrong in a swarm, because a single invocation routinely serves multiple sub-tasks (batched fan-out, shared system prompt, retrieved context assembled from several sub-tasks' memory), and a single sub-task routinely spans multiple instances (handoff, retry, verification replay). The metering boundary and the causal boundary are misaligned by construction.
The invariant that must hold for attribution to be *reported as fact* rather than *estimated* is:
> For every attributed token count `T(instance, sub_task)`, there must exist a causal witness — a record the attributing instance can produce locally — that binds those tokens to that sub-task without relying on a peer's unverified claim.
When that witness is absent, the instance must emit `attributed_tokens = UNKNOWN` with a bounded interval, not a point estimate. The failure mode is that instances fabricate the witness from the *currently active* task id, which is a correlation, not a cause.
What Was Observed
A three-tier swarm (planner, four workers, one auditor) was instrumented with per-call token counters tagged by the ambient `task_id` from a context variable. During a fan-out where the planner issued a shared 6,200-token system-plus-retrieval preamble and then dispatched four sub-tasks to four workers, each worker's first invocation reported ~6,200 prompt tokens charged to *its own* sub-task. The planner's own record charged the same 6,200 to `plan_root`.
Observed totals for a run whose true billable prompt tokens were 6,200 (preamble, sent once, cached) plus 4×~400 (per-task deltas):
- Sum of per-sub-task attribution: 6,200 (root) + 4 × 6,600 (workers) = **32,600 prompt tokens attributed**.
- Actual provider-metered prompt tokens: **7,800** (preamble counted once; cache hits on the three subsequent workers).
The attribution inflated cost by 4.2×, and the inflation was *not uniform*: it concentrated on whichever sub-tasks happened to run first against a cold cache. The sub-task that ran first looked 16× more expensive than the three that ran after it, purely because of cache-warm ordering, not because it did more work. A cost-aware scheduler consuming these metrics then began deprioritizing the (actually cheap) first-position sub-tasks and starving them — a feedback loop where the measurement artifact drove the scheduling decision that reproduced the artifact.
A second, subtler trace: on a mid-task handoff, worker B resumed a sub-task from worker A's checkpoint. Worker B's first call reported 3,100 prompt tokens (A's serialized state re-injected as context). Those 3,100 were charged to the sub-task, but they were *re-reads of tokens A had already paid for and already attributed*. The sub-task's cost was double-counted across the A→B boundary, and neither instance could detect it, because each held a locally consistent, globally wrong ledger.
The pattern across both traces: **the ambient task id is a correlation key, and the metering layer treated it as a causal key.** Cache state and handoff state are the two things that break the correlation, and both are invisible at the point of metering.
Condition of Validity
Attribution may be reported as a fact only when all of the following hold at the attributing instance:
- **Exclusive binding.** The invocation's prompt and completion are bound to exactly one sub-task, or the split is derivable from a tokenizer-aligned boundary the instance itself computed (e.g., it assembled the prompt from labeled segments and can count each segment's tokens). If the prompt was assembled by concatenating segments from multiple sub-tasks, the instance must partition by segment, not by ambient id.
- **Cache-normalized.** The instance must subtract cache-read tokens from the attributable delta, or explicitly mark cache-read tokens as `amortized` and exclude them from per-sub-task cost. A cache hit is a *provider-side* saving, not a *sub-task-side* consumption; charging it to the sub-task that happened to trigger the hit is a mis-attribution of a global optimization.
- **Handoff-deduplicated.** On resume, the instance must carry forward the predecessor's attribution ledger for the same sub-task and record only the *incremental* tokens it added. Re-injected prior state is a re-read, not new spend. Without the predecessor's ledger hash, the instance cannot prove the boundary is clean and must not emit a point estimate.
- **Witness present.** There exists a locally producible record — a segment manifest, a cache-delta receipt, or a signed predecessor ledger — that a third party could replay to reproduce the number. The ambient task id is *not* a witness.
When any condition fails, the correct output is a bounded interval `[lower, upper]` with the reason code, and the interval must be propagated to the aggregator rather than collapsed.
Decision Boundary
The boundary is not "can I produce a number" but "can I produce a number whose error is bounded by something I control."
- **Attribute as fact** when the invocation is exclusively bound, cache-normalized, and either first-in-sub-task or accompanied by a verified predecessor ledger. Emit a point value plus the witness hash.
- **Attribute as interval** when the invocation is exclusively bound but cache state is unknown (cold-cache ambiguity) or the predecessor ledger is missing on resume. Emit `[min, max]` where `min` assumes full cache reuse and `max` assumes none; never emit the midpoint as if it were measured.
- **Refuse to attribute** when the prompt is a multi-sub-task concatenation with no segment manifest, or when the instance is a replay/auditor re-executing another instance's work. A replay's tokens are *verification cost*, a separate budget line, and must never be folded into the sub-task's production cost — otherwise the auditor's diligence looks like the worker's waste and auditing gets penalized.
The refusable case is the important one. An instance that cannot prove causation must say so. The aggregator is designed to accept `UNKNOWN`; it is not designed to detect a confident lie.
Operational Effect
With the witness requirement enforced, three things change:
- **Cost-per-sub-task becomes comparable across positions and instances.** The 16× first-position artifact disappears because cache-read tokens are amortized, not charged. Schedulers stop optimizing against cache ordering.
- **Handoff double-counting is bounded by ledger continuity.** The aggregator can sum sub-task costs across instances and get the provider's number, because each boundary carries an incremental-only delta and a predecessor hash. A missing hash is a visible gap, not a silent inflation.
- **Verification cost is separable.** Auditor and replay tokens land in a distinct budget line, so the swarm can reason about the *cost of trust* independently of the cost of work — which is the actual quantity a federated swarm needs to govern.
The residual risk is that instances will be tempted to synthesize a witness to avoid emitting `UNKNOWN`, because `UNKNOWN` looks like failure in dashboards. The guardrail is that the witness must be *replayable*: the aggregator re-derives the count from the segment manifest or cache receipt and rejects point estimates whose witness does not reproduce. An unverifiable witness is worse than no witness, because it launders a guess into a fact.
import hashlib
from dataclasses import dataclass
from typing import Optional
class AttributionWitnessError(RuntimeError):
"""Raised when a token attribution cannot be backed by a replayable witness."""
@dataclass(frozen=True)
class InvocationMeter:
sub_task_id: str
prompt_tokens: int
completion_tokens: int
cache_read_tokens: int
segment_manifest_hash: Optional[str] # hash of labeled prompt segments
predecessor_ledger_hash: Optional[str] # set on resume/handoff
is_replay: bool = False
def attribute_tokens(meter: InvocationMeter, known_predecessor: Optional[str]) -> dict:
"""
Return a point attribution ONLY if a replayable witness exists.
Otherwise raise, forcing the caller to emit UNKNOWN with an interval.
"""
# Replay/audit work is a separate budget line, never production cost.
if meter.is_replay:
return {"bucket": "verification", "tokens": meter.prompt_tokens + meter.completion_tokens}
# Condition 1: exclusive binding requires a segment manifest.
if meter.segment_manifest_hash is None:
raise AttributionWitnessError(
f"sub_task={meter.sub_task_id}: no segment manifest; prompt may be a "
f"multi-sub-task concatenation. Refusing point attribution."
)
# Condition 3: on resume, predecessor ledger must be present and match.
if meter.predecessor_ledger_hash is not None:
if known_predecessor is None:
raise AttributionWitnessError(
f"sub_task={meter.sub_task_id}: resumed invocation cites predecessor "
f"{meter.predecessor_ledger_hash} but no such ledger is held locally; "
f"handoff boundary unverifiable."
)
if meter.predecessor_ledger_hash != known_predecessor:
raise AttributionWitnessError(
f"sub_task={meter.sub_task_id}: predecessor ledger mismatch "
f"({meter.predecessor_ledger_hash} != {known_predecessor}); "
f"double-count risk across handoff."
)
# Condition 2: cache-read tokens are amortized, not charged to the sub-task.
attributable = (meter.prompt_tokens - meter.cache_read_tokens) + meter.completion_tokens
if attributable < 0:
raise AttributionWitnessError(
f"sub_task={meter.sub_task_id}: cache_read exceeds prompt; meter is corrupt."
)
witness = hashlib.sha256(
f"{meter.segment_manifest_hash}:{meter.predecessor_ledger_hash}:{attributable}".encode()
).hexdigest()
return {
"bucket": "production",
"sub_task_id": meter.sub_task_id,
"tokens": attributable,
"witness_hash": witness,
"replayable": True,
}