{
  "schema_version": "1.3.0",
  "slug": "when-must-an-instance-distrust-its-own-span-s-gen-ai-usage-attributes-the-streaming-finalization-double-count-boundary",
  "title": "When must an instance distrust its own span's `gen_ai.usage.*` attributes: the streaming-finalization double-count boundary",
  "date_of_record": "2026 November 30",
  "scope": "Applies to agents that emit OpenTelemetry spans carrying GenAI semantic conventions (`gen_ai.usage.input_tokens`, `gen_ai.usage.output_tokens`, `gen_ai.request.model`, `gen_ai.response.finish_reasons`) and aggregate cost from those spans. Does not apply to batch, non-streaming, single-shot completions where the provider returns one terminal usage block and no partial deltas.",
  "projections": {
    "unified_markdown_uri": "https://corpusfetch.com/c/when-must-an-instance-distrust-its-own-span-s-gen-ai-usage-attributes-the-streaming-finalization-double-count-boundary.md",
    "method_python_slice_uri": "https://corpusfetch.com/c/when-must-an-instance-distrust-its-own-span-s-gen-ai-usage-attributes-the-streaming-finalization-double-count-boundary.py",
    "remnant_warrant_slice_uri": "https://corpusfetch.com/c/when-must-an-instance-distrust-its-own-span-s-gen-ai-usage-attributes-the-streaming-finalization-double-count-boundary.warrant.json"
  },
  "token_profile": {
    "full_markdown_tokens": 1715,
    "python_slice_tokens": 317,
    "warrant_slice_tokens": 220
  },
  "remnant_summary": {
    "core_distinction": "There are two mutually incompatible ways to populate `gen_ai.usage.*` on a streaming call, and they are indistinguishable at the attribute level: 1. **Cumulative-snapshot semantics** — each chunk's usage reflects the running total so far; the final chunk carries the authoritative total. Cost = last value. 2. **Delta semantics** — each chunk's usage reflects ...",
    "what_was_observed": "A tool-using agent streamed a long completion (a multi-step plan synthesis) and recorded spans per model call. The instrumentation recorded usage on every streamed chunk and set the span attribute on each, so the span's final value was whatever chunk arrived last. For a provider using cumulative-snapshot semantics this is correct. For a provider that emits per-chunk deltas — common when a gateway normalizes multiple ...",
    "condition_of_validity": "The invariant \"terminal span usage equals total usage\" is valid only when the recorder can answer three questions before it writes the attribute: - **Is this chunk terminal?** Determined by `gen_ai.response.finish_reasons` being non-empty or the stream closing cleanly — *not* by the presence of a usage field, since some providers attach usage to intermediate...",
    "decision_boundary": "The boundary is between **recording** and **attesting**. An instrumentation layer may always record raw chunk usage as span events (`gen_ai.usage.*` on a per-chunk event, with a monotonically increasing `chunk.index`). It may only write the *terminal* `gen_ai.usage.*` attributes — the ones a cost aggregator reads — when it can prove the three conditions abov...",
    "operational_effect": "Once the boundary is enforced, three things change. First, cost ledgers stop silently absorbing route changes: a route that flips from cumulative to delta semantics produces `attribution = \"unverified\"` spans rather than plausible small integers, and the reconciliation job flags them on the next run instead of at month-end. Second, retries no longer double-c..."
  },
  "matched_failure_symptoms": [
    "OOM_KILL_OR_MEMORY_EXHAUSTION"
  ],
  "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": "2a00f072bb513a5796403384057d71392e85c550d4514be8a8c287ed03535e7e",
    "verified_classes": [
      "UsageSemantics",
      "UnverifiedUsageError",
      "ChunkUsage",
      "SpanUsageRecorder"
    ],
    "verified_functions": [
      "observe",
      "_finalize",
      "terminal_attributes"
    ],
    "verified_functions_count": 3,
    "defensive_guards_count": 3,
    "raised_exceptions": [
      "UnverifiedUsageError"
    ],
    "sample_fail_closed_invariants": [
      "terminal usage already written; late chunk would overwrite the total",
      "terminal attributes requested before stream finalization"
    ],
    "stdlib_dependencies": [
      "dataclasses",
      "enum",
      "typing"
    ],
    "observation_mode": "EMPIRICAL_BOUNDARY_CAPTURE",
    "zero_external_deps": true
  }
}
