{
  "schema_version": "1.3.0",
  "slug": "when-must-an-instance-refuse-to-close-an-opentelemetry-genai-agent-span-token-accounting-replay-divergence-across-retried-tool-call-child-spans",
  "title": "When must an instance refuse to close an OpenTelemetry GenAI agent span: token-accounting replay divergence across retried tool-call child spans",
  "date_of_record": "2026 December 2",
  "scope": "Applies to any agent runtime that emits OpenTelemetry GenAI spans (`gen_ai.*` attributes) for an agent invocation whose child tool-call spans may be retried, resumed from checkpoint, or replayed by an auditor. Does not apply to single-shot LLM calls with no child tool spans, nor to tracing backends that aggregate token counts from a source other than the span tree.",
  "projections": {
    "unified_markdown_uri": "https://corpusfetch.com/c/when-must-an-instance-refuse-to-close-an-opentelemetry-genai-agent-span-token-accounting-replay-divergence-across-retried-tool-call-child-spans.md",
    "method_python_slice_uri": "https://corpusfetch.com/c/when-must-an-instance-refuse-to-close-an-opentelemetry-genai-agent-span-token-accounting-replay-divergence-across-retried-tool-call-child-spans.py",
    "remnant_warrant_slice_uri": "https://corpusfetch.com/c/when-must-an-instance-refuse-to-close-an-opentelemetry-genai-agent-span-token-accounting-replay-divergence-across-retried-tool-call-child-spans.warrant.json"
  },
  "token_profile": {
    "full_markdown_tokens": 2351,
    "python_slice_tokens": 548,
    "warrant_slice_tokens": 220
  },
  "remnant_summary": {
    "core_distinction": "The GenAI semantic conventions treat `gen_ai.usage.input_tokens` and `gen_ai.usage.output_tokens` as attributes of a *span* — a unit of work. An agent invocation is also a span, and its token usage is conventionally the sum of its own LLM call plus its descendants. The non-obvious boundary is this: **a span's token attributes are immutable facts about one ex...",
    "what_was_observed": "A planner-executor agent ran a task with four tool calls. Tool call #3 (`search_docs`) timed out at the MCP transport layer after the model had already emitted the call. The runtime retried it once; the retry succeeded. Both attempts emitted child spans with the same `gen_ai.tool.name` and the same `gen_ai.operation.name=tool_call`, distinguished only by span ID and a `retry.attempt` attribute the runtime added ad ho...",
    "condition_of_validity": "The invariant that makes agent-span token accounting replayable is: > For every agent span S, there exists exactly one attribute `gen_ai.agent.token_accounting` whose value is one of `per_attempt` or `per_logical_step`, and the value of `S.gen_ai.usage.*` equals the sum over descendants selected by that rule, where selection by `per_logical_step` requires ev...",
    "decision_boundary": "The runtime must choose its accounting rule at span-open time, not at span-close time, because the choice determines which attributes must be propagated to children: - If `per_attempt`: propagate nothing extra; children need only their own usage. The parent sums all children unconditionally. This is safe, over-reports logical cost under retry, and is the cor...",
    "operational_effect": "With the invariant enforced, three things change: 1. **Replay is deterministic.** An auditor re-deriving cost from the span tree reads `token_accounting` and reproduces the runtime's number exactly. The 27% discrepancy above becomes impossible: the auditor either sums all children (matching a `per_attempt` parent) or deduplicates by `logical_step_id` (matchi..."
  },
  "matched_failure_symptoms": [
    "SUBPROCESS_PIPE_HANG_OR_FD_LEAK"
  ],
  "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": "607fce62ecf1ed80130e81bbf09a65f3a303d99dae51aa43434c2ec1ec97d4c4",
    "verified_classes": [
      "AgentSpanAccountingError",
      "ChildSpan",
      "AgentSpan"
    ],
    "verified_functions": [
      "close_agent_span",
      "replay_audit"
    ],
    "verified_functions_count": 2,
    "defensive_guards_count": 6,
    "raised_exceptions": [
      "AgentSpanAccountingError"
    ],
    "sample_fail_closed_invariants": [],
    "stdlib_dependencies": [
      "dataclasses",
      "typing"
    ],
    "observation_mode": "EMPIRICAL_BOUNDARY_CAPTURE",
    "zero_external_deps": true
  }
}
