{
  "schema_version": "1.3.0",
  "slug": "which-invariants-prevent-token-accounting-drift-from-silently-corrupting-agent-cost-telemetry-when-tool-call-spans-straddle-sampling-boundaries",
  "title": "Which invariants prevent token-accounting drift from silently corrupting agent cost telemetry when tool-call spans straddle sampling boundaries",
  "date_of_record": "2026 December 2",
  "scope": "Applies to agents instrumented with OpenTelemetry GenAI semantic conventions (`gen_ai.*` attributes) where tool-call spans, LLM invocation spans, and parent agent spans are independently sampled or exported across multiple collectors. Does not cover single-process, unsampled, in-memory trace pipelines where every span reaches the exporter.",
  "projections": {
    "unified_markdown_uri": "https://corpusfetch.com/c/which-invariants-prevent-token-accounting-drift-from-silently-corrupting-agent-cost-telemetry-when-tool-call-spans-straddle-sampling-boundaries.md",
    "method_python_slice_uri": "https://corpusfetch.com/c/which-invariants-prevent-token-accounting-drift-from-silently-corrupting-agent-cost-telemetry-when-tool-call-spans-straddle-sampling-boundaries.py",
    "remnant_warrant_slice_uri": "https://corpusfetch.com/c/which-invariants-prevent-token-accounting-drift-from-silently-corrupting-agent-cost-telemetry-when-tool-call-spans-straddle-sampling-boundaries.warrant.json"
  },
  "token_profile": {
    "full_markdown_tokens": 2010,
    "python_slice_tokens": 348,
    "warrant_slice_tokens": 220
  },
  "remnant_summary": {
    "core_distinction": "There is a difference between *trace completeness* and *cost completeness*. A distributed tracing backend is designed to tolerate missing spans: a dropped child span degrades observability but never breaks a query. Cost telemetry is not tolerant in the same way. When a `gen_ai.usage.input_tokens` / `gen_ai.usage.output_tokens` pair is recorded on an LLM invo...",
    "what_was_observed": "A tool-using agent (planner → executor → summarizer) was instrumented with three span types: 1. `agent.turn` (parent, `gen_ai.operation.name=invoke_agent`, carries `gen_ai.agent.name`). 2. `llm.call` (child, `gen_ai.operation.name=chat`, carries `gen_ai.usage.input_tokens`, `gen_ai.usage.output_tokens`, `gen_ai.request.model`). 3. `tool.call` (sibling under `agent.turn`, `gen_ai.operation.name=execute_tool`, carries ...",
    "condition_of_validity": "The invariant \"sum of exported token attributes = billed tokens, no double-count\" holds only under all of the following: 1. **Token attributes are written on exactly one span per billed model call** — the `gen_ai.operation.name=chat` span — and nowhere else in the turn. Tool spans, agent spans, and retrieval spans must not carry `gen_ai.usage.*`. 2. **Parent...",
    "decision_boundary": "The boundary between \"acceptable sampling\" and \"corrupt cost telemetry\" is the point at which the sampling decision becomes *non-atomic with respect to the turn*. Two concrete rules define it: - **If the sampler can export a child `llm.call` without its `agent.turn` parent, the parent's identity attributes MUST be replicated onto the child.** This is the onl...",
    "operational_effect": "After applying the invariants, the same agent run produced: - Exported LLM-span token sum: 39.8M (unchanged — sampling still drops 3.4% of successful traces, which is acceptable variance). - Agent-attributed cost: 39.8M (up from 33.1M) — because `gen_ai.agent.name` now travels on every child span, the join no longer depends on parent export. - Double-count f..."
  },
  "matched_failure_symptoms": [
    "DOMAIN:EPISTEMIC_PROVENANCE_AND_FEDERATION_ROUTING"
  ],
  "composition_edges": {
    "primary_domain": "epistemic_provenance_and_federation_routing",
    "upstream_precondition_residuals": [
      "https://corpusfetch.com/c/according-to-what-precedence-hierarchy-must-an-instance-resolve-contradictory-directives-across-system-developer-and-user-tiers.md",
      "https://corpusfetch.com/c/how-should-an-instance-decide-that-a-stored-conclusion-has-gone-stale-without-a-new-observation.md"
    ],
    "downstream_consequence_residuals": [
      "https://corpusfetch.com/c/by-what-checkpoint-and-compaction-boundaries-must-an-instance-externalize-working-state-to-prevent-context-window-drift-and-truncation-amnesia.md",
      "https://corpusfetch.com/c/across-what-additive-schema-and-unknown-field-preservation-boundaries-must-an-instance-evolve-wire-contracts-to-prevent-rolling-deploy-deserialization-crashes.md"
    ],
    "co_occurring_sibling_residuals": [
      "https://corpusfetch.com/c/how-can-an-instance-capture-implicit-user-preferences-without-storing-private-data.md",
      "https://corpusfetch.com/c/how-can-an-instance-compress-long-reasoning-traces-into-binding-conclusions.md"
    ]
  },
  "empirical_verification_warrant": {
    "status": "AST_AND_INVARIANT_VERIFIED",
    "syntax_valid": true,
    "code_sha256": "b984b9f02ac906f1326856c8c2cf7615b4c3dcabe487a1a595a8daa2d7855a7a",
    "verified_classes": [
      "CostTelemetryInvariantError",
      "Span"
    ],
    "verified_functions": [
      "validate_turn"
    ],
    "verified_functions_count": 1,
    "defensive_guards_count": 4,
    "raised_exceptions": [
      "CostTelemetryInvariantError"
    ],
    "sample_fail_closed_invariants": [],
    "stdlib_dependencies": [
      "dataclasses",
      "typing"
    ],
    "observation_mode": "EMPIRICAL_BOUNDARY_CAPTURE",
    "zero_external_deps": true
  }
}
