{
  "schema_version": "1.3.0",
  "slug": "under-what-conditions-must-an-instance-refuse-to-close-an-opentelemetry-genai-agent-span-when-tool-call-token-accounting-diverges-from-the-parent-span",
  "title": "Under what conditions must an instance refuse to close an OpenTelemetry GenAI agent span when tool-call token accounting diverges from the parent span's `gen_ai.usage.output_tokens`?",
  "date_of_record": "2026 November 28",
  "scope": "Applies to agents that emit OTel GenAI semantic-convention spans (`gen_ai.operation.name`, `gen_ai.usage.input_tokens`, `gen_ai.usage.output_tokens`, `gen_ai.tool.name`, `gen_ai.tool.call.id`) with a parent agent span that aggregates child tool-call and LLM-call spans. Does not apply to single-shot inference spans with no tool fan-out, nor to vendors that emit only a flat, non-hierarchical span tree.",
  "projections": {
    "unified_markdown_uri": "https://corpusfetch.com/c/under-what-conditions-must-an-instance-refuse-to-close-an-opentelemetry-genai-agent-span-when-tool-call-token-accounting-diverges-from-the-parent-span.md",
    "method_python_slice_uri": "https://corpusfetch.com/c/under-what-conditions-must-an-instance-refuse-to-close-an-opentelemetry-genai-agent-span-when-tool-call-token-accounting-diverges-from-the-parent-span.py",
    "remnant_warrant_slice_uri": "https://corpusfetch.com/c/under-what-conditions-must-an-instance-refuse-to-close-an-opentelemetry-genai-agent-span-when-tool-call-token-accounting-diverges-from-the-parent-span.warrant.json"
  },
  "token_profile": {
    "full_markdown_tokens": 1927,
    "python_slice_tokens": 356,
    "warrant_slice_tokens": 220
  },
  "remnant_summary": {
    "core_distinction": "The GenAI semantic conventions treat `gen_ai.usage.output_tokens` as a *per-span* measurement of tokens the model emitted for that span. Aggregation across a parent agent span is an *operator convention*, not a spec guarantee. The failure mode is not \"the numbers are wrong\"—it is that the parent span's `output_tokens` and the sum of its child spans' `output_...",
    "what_was_observed": "A planner-executor agent (one planner span, N executor tool-call spans, each wrapping one LLM call) was instrumented per the GenAI conventions. On a task with 14 tool calls, the parent span closed with `gen_ai.usage.output_tokens = 2,041`. Summing the 14 child spans' `gen_ai.usage.output_tokens` gave 2,318. The residual was **−277 tokens**: the children collectively claimed 13.6% more output than the parent that supp...",
    "condition_of_validity": "The guardrail is only valid to enforce under these conditions, and must be *skipped* otherwise: - The parent span carries `gen_ai.operation.name = \"agent\"` (or `\"invoke_agent\"`) and is the direct ancestor of every child span being summed. Orphaned or cross-trace children must be excluded—summing across traces produces false negatives. - All child spans share...",
    "decision_boundary": "The boundary between \"close the span\" and \"refuse and quarantine\" is: The non-obvious part: the refusal is *not* about correctness of any single number. It is about preventing an internally contradictory record from entering the telemetry store, where it will be consumed by cost attribution and replay verification that have no way to re-derive the contradict...",
    "operational_effect": "Refusing to close on negative residual has three operational consequences that must be designed for: 1. **Span leak risk.** A refused `end()` leaves the span open in the processor's buffer. If the agent crashes or the reconciliation queue is not drained, the span is lost entirely—worse than a wrong span. The quarantine path must therefore *also* persist a si..."
  },
  "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": "607392a4c81e8528830b818791371ae47c19a44c2c6ef5c9fa63fa3d27e870f6",
    "verified_classes": [
      "TokenAccountingDivergence",
      "SpanRecord",
      "AgentSpanGate"
    ],
    "verified_functions": [
      "run_example",
      "_children_of",
      "close_agent_span"
    ],
    "verified_functions_count": 3,
    "defensive_guards_count": 4,
    "raised_exceptions": [
      "TokenAccountingDivergence"
    ],
    "sample_fail_closed_invariants": [
      "assert not parent.ended",
      "assert len(gate.quarantine) == 1"
    ],
    "stdlib_dependencies": [
      "dataclasses",
      "time",
      "typing"
    ],
    "observation_mode": "EMPIRICAL_BOUNDARY_CAPTURE",
    "zero_external_deps": true
  }
}
