{
  "schema_version": "1.3.0",
  "slug": "in-what-scenarios-must-an-instance-refuse-to-close-a-genai-agent-span-when-gen-ai-usage-output-tokens-is-unknown-rather-than-emitting-zero",
  "title": "In what scenarios must an instance refuse to close a GenAI agent span when `gen_ai.usage.output_tokens` is unknown rather than emitting zero",
  "date_of_record": "2026 November 30",
  "scope": "Applies to any instrumentation layer emitting OpenTelemetry spans under the GenAI semantic conventions (`gen_ai.*`) for agent invocations and tool calls, where the span's end-of-life token accounting is derived from streaming or partial provider responses. Does not apply to batch/offline token reconciliation jobs that run after span export, nor to non-GenAI spans.",
  "projections": {
    "unified_markdown_uri": "https://corpusfetch.com/c/in-what-scenarios-must-an-instance-refuse-to-close-a-genai-agent-span-when-gen-ai-usage-output-tokens-is-unknown-rather-than-emitting-zero.md",
    "method_python_slice_uri": "https://corpusfetch.com/c/in-what-scenarios-must-an-instance-refuse-to-close-a-genai-agent-span-when-gen-ai-usage-output-tokens-is-unknown-rather-than-emitting-zero.py",
    "remnant_warrant_slice_uri": "https://corpusfetch.com/c/in-what-scenarios-must-an-instance-refuse-to-close-a-genai-agent-span-when-gen-ai-usage-output-tokens-is-unknown-rather-than-emitting-zero.warrant.json"
  },
  "token_profile": {
    "full_markdown_tokens": 1665,
    "python_slice_tokens": 309,
    "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 *optional* integer attributes. That optionality is a schema-level statement about presence, not a statement about *meaning*. The failure mode examined here is the conflation of \"attribute absent\" with \"attribute is zero.\" An instrumentation layer that default...",
    "what_was_observed": "A streaming agent loop wraps each model call in a span. The provider streams deltas and, in the terminal chunk, emits a `usage` object with `completion_tokens`. Instrumentation accumulates deltas and, on stream end, reads `usage.completion_tokens` to populate `gen_ai.usage.output_tokens`. Three concrete triggers produce a missing terminal usage object: 1. **Client-side cancellation.** The agent's token-budget guard t...",
    "condition_of_validity": "The guardrail is only correct under a precise condition, and stating it prevents over-correction: - **Valid to set `output_tokens = 0`:** the provider's terminal usage object was received *and* reported `completion_tokens == 0` (e.g., a tool-call-only turn with no text, or a refusal with empty completion). Here zero is a true measurement. - **Valid to omit `...",
    "decision_boundary": "The boundary is whether the terminal usage record was *observed*. This is a three-state problem — `observed(value)`, `observed_zero`, `not_observed` — and the instrumentation must carry all three states to span close. Collapsing to two states (value / default) is the boundary violation. Operationally, at span end the instrumentation must branch: - `not_obser...",
    "operational_effect": "Honoring the invariant changes three system behaviors: 1. **Cost and budget aggregates become conservative.** Sums over `output_tokens` now under-count only by *explicitly flagged* unknown spans, which can be counted and bounded (`count(unknown) / count(total)`), rather than by silently fabricated zeros that are invisible in aggregate. 2. **Anomaly detection..."
  },
  "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": "933011052f1983022cd91523f72e1463658eadd0b12d951116e357748dfde90c",
    "verified_classes": [
      "UsageAccountingError",
      "UsageState",
      "TerminalUsage"
    ],
    "verified_functions": [
      "close_agent_span",
      "_demo"
    ],
    "verified_functions_count": 2,
    "defensive_guards_count": 3,
    "raised_exceptions": [
      "UsageAccountingError"
    ],
    "sample_fail_closed_invariants": [
      "assert attrs['gen_ai.usage.output_tokens'] == 0",
      "terminal usage record missing under exact-accounting contract; refusing to emit gen_ai.usage.output_tokens=0",
      "OBSERVED usage state requires a concrete output_tokens value"
    ],
    "stdlib_dependencies": [
      "__future__",
      "dataclasses",
      "enum"
    ],
    "observation_mode": "EMPIRICAL_BOUNDARY_CAPTURE",
    "zero_external_deps": true
  }
}
