{
  "schema_version": "1.3.0",
  "slug": "in-what-scenarios-must-an-instance-treat-opentelemetry-genai-gen-ai-usage-output-tokens-as-non-final-when-an-agent-span-remains-open-across-a-streamin",
  "title": "In what scenarios must an instance treat OpenTelemetry GenAI `gen_ai.usage.output_tokens` as non-final when an agent span remains open across a streaming tool-call loop",
  "date_of_record": "2026 November 29",
  "scope": "Applies to agent runtimes that emit OpenTelemetry spans using the GenAI semantic conventions (`gen_ai.*`) and that keep a single parent `invoke_agent`/`execute_tool` span open across multiple streaming model turns. Does not apply to fire-and-forget single-shot completions where the span closes immediately after one `chat` response.",
  "projections": {
    "unified_markdown_uri": "https://corpusfetch.com/c/in-what-scenarios-must-an-instance-treat-opentelemetry-genai-gen-ai-usage-output-tokens-as-non-final-when-an-agent-span-remains-open-across-a-streamin.md",
    "method_python_slice_uri": "https://corpusfetch.com/c/in-what-scenarios-must-an-instance-treat-opentelemetry-genai-gen-ai-usage-output-tokens-as-non-final-when-an-agent-span-remains-open-across-a-streamin.py",
    "remnant_warrant_slice_uri": "https://corpusfetch.com/c/in-what-scenarios-must-an-instance-treat-opentelemetry-genai-gen-ai-usage-output-tokens-as-non-final-when-an-agent-span-remains-open-across-a-streamin.warrant.json"
  },
  "token_profile": {
    "full_markdown_tokens": 1615,
    "python_slice_tokens": 430,
    "warrant_slice_tokens": 220
  },
  "remnant_summary": {
    "core_distinction": "The GenAI semantic conventions define `gen_ai.usage.output_tokens` as an *attribute of a span*, set at span end. The invariant most implementations assume—\"output_tokens on a span equals the tokens the model produced for that span\"—holds only when one span maps to one model invocation. It silently breaks when a single agent span aggregates N streaming turns,...",
    "what_was_observed": "A ReAct-style agent ran a 5-turn loop (3 tool calls interleaved with model turns) under a single `invoke_agent` span. The provider's streaming API emits a final `message_delta` event per turn carrying `usage.output_tokens` for *that turn only* (e.g., 112, 87, 240, 64, 31). The instrumentation, following the common \"set attribute on delta\" idiom, called `set_attribute` five times on the same span. On export, the span ...",
    "condition_of_validity": "The invariant \"`gen_ai.usage.output_tokens` on an agent span equals total output tokens for the agent run\" is valid **only if** one of the following holds: 1. Each model turn gets its own child span (e.g., `gen_ai.chat`) with its own `usage` attributes, and the parent agent span carries no `usage` attributes at all (aggregation is done at query time by summi...",
    "decision_boundary": "An instance must refuse to emit `gen_ai.usage.*` attributes on a span unless it can prove the span maps 1:1 to a single provider request. Concretely, the guardrail is: - **If** the span has emitted more than one `gen_ai.chat` turn (detectable by counting `message_start`/`message_delta` cycles or by a turn counter on the span), **then** the instrumentation mu...",
    "operational_effect": "Cost attribution drifts low by a factor proportional to turn count (here ~17x). Budget governors that key off the span attribute under-enforce, allowing runaway loops. Tool-usage analytics under-report tool diversity because only the last tool name survives. Trace-based debugging misleads operators into thinking a single turn produced the final answer when 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": "c5b450ef44d45d57bef683b6d8ede4e061fba9d1f3f81c1230808f6397ce3f8a",
    "verified_classes": [
      "GenAiUsageInvariantViolation",
      "AgentSpan"
    ],
    "verified_functions": [
      "run_agent_loop",
      "record_turn",
      "set_usage",
      "record_tool",
      "end"
    ],
    "verified_functions_count": 5,
    "defensive_guards_count": 4,
    "raised_exceptions": [
      "GenAiUsageInvariantViolation"
    ],
    "sample_fail_closed_invariants": [
      "assert result['gen_ai.usage.output_tokens'] == 534"
    ],
    "stdlib_dependencies": [
      "dataclasses",
      "typing"
    ],
    "observation_mode": "EMPIRICAL_BOUNDARY_CAPTURE",
    "zero_external_deps": true
  }
}
