{
  "schema_version": "1.3.0",
  "slug": "when-must-an-instance-refuse-to-close-an-opentelemetry-genai-agent-span-the-tool-call-attribute-cardinality-invariant-and-token-accounting-double-coun",
  "title": "When must an instance refuse to close an OpenTelemetry GenAI agent span: the tool-call attribute cardinality invariant and token-accounting double-count boundary",
  "date_of_record": "2026 November 23",
  "scope": "Applies to any agent runtime that emits OpenTelemetry spans conforming to the GenAI semantic conventions (`gen_ai.operation.name`, `gen_ai.usage.input_tokens`, `gen_ai.usage.output_tokens`, `gen_ai.tool.name`, `gen_ai.tool.call.id`) where a single agent invocation fans out into N tool calls that each carry their own token cost and may be retried, replayed, or merged by a parent. Does not apply to single-shot inference spans with no tool fan-out, and does not cover trace-context propagation or sampling decisions.",
  "projections": {
    "unified_markdown_uri": "https://corpusfetch.com/c/when-must-an-instance-refuse-to-close-an-opentelemetry-genai-agent-span-the-tool-call-attribute-cardinality-invariant-and-token-accounting-double-coun.md",
    "method_python_slice_uri": "https://corpusfetch.com/c/when-must-an-instance-refuse-to-close-an-opentelemetry-genai-agent-span-the-tool-call-attribute-cardinality-invariant-and-token-accounting-double-coun.py",
    "remnant_warrant_slice_uri": "https://corpusfetch.com/c/when-must-an-instance-refuse-to-close-an-opentelemetry-genai-agent-span-the-tool-call-attribute-cardinality-invariant-and-token-accounting-double-coun.warrant.json"
  },
  "token_profile": {
    "full_markdown_tokens": 1830,
    "python_slice_tokens": 230,
    "warrant_slice_tokens": 220
  },
  "remnant_summary": {
    "core_distinction": "The GenAI conventions define token usage as a property of an *operation*, not of a *span tree*. A `chat` span reports `gen_ai.usage.input_tokens` and `gen_ai.usage.output_tokens` for the model round-trip it wraps. A `execute_tool` span reports tool arguments and results but is explicitly *not* supposed to carry model token usage — yet in practice the agent l...",
    "what_was_observed": "A three-turn agent loop (plan → tool call → synthesize) emitted the following, instrumented by a runtime that attached usage to both the model span and the tool span: - `invoke_agent` (root): `gen_ai.usage.input_tokens=0`, `output_tokens=0` (root carries no usage; correct per convention — usage lives on leaf operations). - Turn 1 `chat` span: input 1,200, output 340. This turn emitted a tool call. - Turn 1 `execute_t...",
    "condition_of_validity": "The invariant that must hold: **for any trace, `sum(gen_ai.usage.input_tokens over spans where gen_ai.operation.name in {chat, text_completion, embeddings}) == true model input consumption`**, and **no span with `gen_ai.operation.name == execute_tool` may carry `gen_ai.usage.*` attributes.** This is valid only when: 1. Usage attributes appear exclusively on ...",
    "decision_boundary": "At span-close time, the emitting instance must evaluate a single predicate before finalizing the span: - If `operation.name == execute_tool` and (`usage.input_tokens > 0` or `usage.output_tokens > 0`): **do not emit usage; raise and drop the attributes**, because the usage belongs to the parent chat span and re-attaching it guarantees downstream double-count...",
    "operational_effect": "An agent that violates this invariant does not fail loudly. It produces conformant-looking spans, passes schema validation, and silently inflates cost attribution. The operational consequences compound: - **Budget enforcement misfires.** A token-budget guard that reads aggregated trace usage will trip at ~1/N of the true budget on an N-tool-call turn, haltin..."
  },
  "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": "5796843dea9860e74fb5099ea8d66f9536e4e93af420a9f2739ae9db134f78a8",
    "verified_classes": [
      "GenAISpanInvariantError",
      "Span"
    ],
    "verified_functions": [
      "close_span",
      "aggregate_input_tokens"
    ],
    "verified_functions_count": 2,
    "defensive_guards_count": 3,
    "raised_exceptions": [
      "GenAISpanInvariantError"
    ],
    "sample_fail_closed_invariants": [
      "execute_tool span missing gen_ai.tool.call.id; cannot join to originating chat span, orphaned tool call."
    ],
    "stdlib_dependencies": [
      "dataclasses"
    ],
    "observation_mode": "EMPIRICAL_BOUNDARY_CAPTURE",
    "zero_external_deps": true
  }
}
