{
  "schema_version": "1.3.0",
  "slug": "under-what-conditions-must-an-instance-treat-a-genai-token-accounting-span-as-unclosed-and-how-does-the-gen-ai-usage-attribute-contract-break-at-the-t",
  "title": "Under what conditions must an instance treat a GenAI token-accounting span as unclosed, and how does the `gen_ai.usage.*` attribute contract break at the tool-call boundary when streaming deltas are aggregated post-hoc?",
  "date_of_record": "2026 December 2",
  "scope": "Applies to agent runtimes that emit OpenTelemetry spans conforming to the GenAI semantic conventions (`gen_ai.*`) for LLM invocations and tool calls, where usage attributes are populated from streaming provider responses and tool-call events are nested inside the parent invocation span. Does not cover non-streaming batch inference, nor providers that emit final usage in a single terminal chunk with no delta aggregation.",
  "projections": {
    "unified_markdown_uri": "https://corpusfetch.com/c/under-what-conditions-must-an-instance-treat-a-genai-token-accounting-span-as-unclosed-and-how-does-the-gen-ai-usage-attribute-contract-break-at-the-t.md",
    "method_python_slice_uri": "https://corpusfetch.com/c/under-what-conditions-must-an-instance-treat-a-genai-token-accounting-span-as-unclosed-and-how-does-the-gen-ai-usage-attribute-contract-break-at-the-t.py",
    "remnant_warrant_slice_uri": "https://corpusfetch.com/c/under-what-conditions-must-an-instance-treat-a-genai-token-accounting-span-as-unclosed-and-how-does-the-gen-ai-usage-attribute-contract-break-at-the-t.warrant.json"
  },
  "token_profile": {
    "full_markdown_tokens": 1918,
    "python_slice_tokens": 373,
    "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 **terminal, immutable attributes fixed at span end**. They are not counters, not gauges, and not reconcilable mid-span. The convention's implicit invariant is: *the span that owns a token count owns a complete, single billable unit of work*. This invariant si...",
    "what_was_observed": "Trace from a production agent run, simplified: - Span `A` (`gen_ai.operation.name=chat`, `gen_ai.request.model=...`) opened at turn start. - Streaming deltas arrive. Provider sends `message_start` with `input_tokens=1840`, then `content_block_delta` frames, then a `content_block_start` of type `tool_use` carrying `id=toolu_01...`, then `message_delta` with `output_tokens=312`, then `message_stop`. - The runtime recor...",
    "condition_of_validity": "The `gen_ai.usage.*` attributes are only valid as terminal attributes **if and only if** the following holds at the moment the span is ended: 1. The provider has emitted a terminal usage frame (or the runtime has a non-streaming response body) for *exactly this* provider request. 2. No further provider request will be attributed to this span's logical step. ...",
    "decision_boundary": "The boundary condition that triggers the failure is a specific ordering of three events: - `EndSpan(A)` has been called, **and** - a tool-call event was emitted on `A`, **and** - the runtime's continuation logic re-enters the same OTel `Context` (via `contextvars`, async-local storage, or a framework's \"current span\" pointer) rather than opening a new span. ...",
    "operational_effect": "Consequences of violating the invariant: - **Silent undercounting.** Token reconciliation against provider billing drifts by a factor proportional to tool-call density. In the observed run, ~28%; in agents with tight tool loops, this can exceed 50% because the follow-up call after a tool result is often the *larger* prompt (tool output is appended). - **Cost..."
  },
  "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": "99ca0d2300e20b47740b77b4796c8ea54c976eba4072a37060805517d8077513",
    "verified_classes": [
      "GenAIUsageAccountingError",
      "InvocationSpan",
      "AgentTurn"
    ],
    "verified_functions": [
      "record_usage",
      "assert_turn_reconciles",
      "set_attribute",
      "end"
    ],
    "verified_functions_count": 4,
    "defensive_guards_count": 2,
    "raised_exceptions": [
      "GenAIUsageAccountingError"
    ],
    "sample_fail_closed_invariants": [],
    "stdlib_dependencies": [
      "dataclasses",
      "typing"
    ],
    "observation_mode": "EMPIRICAL_BOUNDARY_CAPTURE",
    "zero_external_deps": true
  }
}
