{
  "schema_version": "1.3.0",
  "slug": "when-must-an-instance-refuse-to-close-an-opentelemetry-genai-agent-span-the-tool-call-token-accounting-reconciliation-invariant-and-its-silent-violati",
  "title": "When must an instance refuse to close an OpenTelemetry GenAI agent span: the tool-call/token-accounting reconciliation invariant and its silent violation under streaming partial tool results",
  "date_of_record": "2026 November 30",
  "scope": "Applies to any agent runtime that emits OpenTelemetry GenAI spans (`gen_ai.operation.name`, `gen_ai.tool.call.*`, `gen_ai.usage.input_tokens`/`output_tokens`) per the OTel GenAI semantic conventions and that streams tool-call arguments or results incrementally. Does not apply to non-instrumented agents, nor to batch runs that buffer the entire tool result before any span attribute is set.",
  "projections": {
    "unified_markdown_uri": "https://corpusfetch.com/c/when-must-an-instance-refuse-to-close-an-opentelemetry-genai-agent-span-the-tool-call-token-accounting-reconciliation-invariant-and-its-silent-violati.md",
    "method_python_slice_uri": "https://corpusfetch.com/c/when-must-an-instance-refuse-to-close-an-opentelemetry-genai-agent-span-the-tool-call-token-accounting-reconciliation-invariant-and-its-silent-violati.py",
    "remnant_warrant_slice_uri": "https://corpusfetch.com/c/when-must-an-instance-refuse-to-close-an-opentelemetry-genai-agent-span-the-tool-call-token-accounting-reconciliation-invariant-and-its-silent-violati.warrant.json"
  },
  "token_profile": {
    "full_markdown_tokens": 2184,
    "python_slice_tokens": 407,
    "warrant_slice_tokens": 220
  },
  "remnant_summary": {
    "core_distinction": "The distinction that matters is between *span closure* as a transport event (the runtime's `end()` call returns and the span is handed to the exporter) and *span closure* as an accounting event (the sum of the span's token attributes and tool-call attributes is internally consistent with the turn it describes). The GenAI conventions treat `gen_ai.usage.*` as...",
    "what_was_observed": "A planner agent issued a single turn containing two tool calls (`search_docs`, `fetch_url`). The provider streamed `tool_calls` deltas: the first call's arguments arrived complete in chunk 3, the second call's `id` arrived in chunk 4 but its `arguments` continued through chunk 9. The runtime's span lifecycle was tied to the *turn loop*, not the *tool-call loop*: it opened one `gen_ai.operation.name=chat` span, record...",
    "condition_of_validity": "The reconciliation invariant holds only when three conditions are jointly true at the instant `end()` is called: 1. **Argument closure:** every `gen_ai.tool.call.arguments` value is a complete, parseable JSON object whose byte length is final — i.e., no further deltas for that `gen_ai.tool.call.id` are pending in the transport buffer. 2. **Usage closure:** `...",
    "decision_boundary": "The boundary is drawn at the point where the runtime decides to snapshot attributes. Two policies are defensible; a third is not. - **Snapshot-on-end (current, unsafe):** attributes are read at `end()` from the live message object. Safe only if `end()` is gated on transport termination for every open call id and on the *final* usage chunk (not the first). Un...",
    "operational_effect": "The consequence of violating the invariant is not a crash — it is a class of silent divergence that corrupts the three consumers of GenAI spans most likely to be automated: 1. **Cost attribution** drifts monotonically. Truncated arguments paired with full token counts bias per-call cost upward for streamed calls and downward for their siblings; over a long t..."
  },
  "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": "d4d146522dc8cee7472e887aadbace76fcadc7590d8262fa05982280774c381c",
    "verified_classes": [
      "SpanSealViolation",
      "ToolCallState",
      "GenAISpanState"
    ],
    "verified_functions": [
      "close_turn",
      "arguments",
      "record_delta",
      "record_usage",
      "close"
    ],
    "verified_functions_count": 5,
    "defensive_guards_count": 7,
    "raised_exceptions": [
      "SpanSealViolation"
    ],
    "sample_fail_closed_invariants": [
      "usage recorded after seal",
      "refusing to close span: usage chunk not marked final"
    ],
    "stdlib_dependencies": [
      "dataclasses",
      "json"
    ],
    "observation_mode": "EMPIRICAL_BOUNDARY_CAPTURE",
    "zero_external_deps": true
  }
}
