{
  "schema_version": "1.3.0",
  "slug": "what-happens-when-an-instance-emits-otlp-spans-whose-gen-ai-semantic-convention-attributes-are-computed-after-the-span-has-already-been-closed",
  "title": "What happens when an instance emits OTLP spans whose gen_ai semantic-convention attributes are computed after the span has already been closed",
  "date_of_record": "2026 November 29",
  "scope": "Applies to GenAI agent runtimes that buffer spans in-process and emit OTLP batches asynchronously, where gen_ai semantic-convention attributes (gen_ai.usage.input_tokens, gen_ai.usage.output_tokens, gen_ai.response.finish_reasons, gen_ai.request.model) are populated by a post-hoc decorator or exporter-side enricher rather than at span start/end. Does not apply to synchronous exporters that serialize attributes at span end, nor to runtimes that emit one span per token via streaming metrics.",
  "projections": {
    "unified_markdown_uri": "https://corpusfetch.com/c/what-happens-when-an-instance-emits-otlp-spans-whose-gen-ai-semantic-convention-attributes-are-computed-after-the-span-has-already-been-closed.md",
    "method_python_slice_uri": "https://corpusfetch.com/c/what-happens-when-an-instance-emits-otlp-spans-whose-gen-ai-semantic-convention-attributes-are-computed-after-the-span-has-already-been-closed.py",
    "remnant_warrant_slice_uri": "https://corpusfetch.com/c/what-happens-when-an-instance-emits-otlp-spans-whose-gen-ai-semantic-convention-attributes-are-computed-after-the-span-has-already-been-closed.warrant.json"
  },
  "token_profile": {
    "full_markdown_tokens": 1723,
    "python_slice_tokens": 371,
    "warrant_slice_tokens": 220
  },
  "remnant_summary": {
    "core_distinction": "There are two places a gen_ai attribute can be attached to a span: (a) at `span.end()` time, inside the SDK's `SpanProcessor.on_end` callback, where the span is still mutable and the attribute is serialized into the protobuf `Span` message; or (b) after `on_end` returns, by an exporter-side enricher that mutates the already-serialized `ReadableSpan` or its `...",
    "what_was_observed": "A tool-calling agent loop emitted a `gen_ai.chat` span per LLM turn. At span start the instrumentation wrote `gen_ai.usage.input_tokens = 0` and `gen_ai.usage.output_tokens = 0` as sentinels, because the provider's usage block is only available in the final streaming chunk. The span was ended inside the SDK when the stream closed. A `BatchSpanProcessor` queued the `ReadableSpan`. A separate enrichment stage — running...",
    "condition_of_validity": "The invariant that holds is: **a gen_ai usage attribute is only authoritative if it is set before `span.end()` returns, and the span's `end_time_unix_nano` is greater than or equal to the timestamp of the last attribute write.** This is enforceable only if the enrichment data is available at end time. It is *not* enforceable when: - The usage block arrives i...",
    "decision_boundary": "The boundary is between \"attribute written before serialization\" and \"attribute written after.\" Concretely: if your instrumentation calls `set_attribute` from any thread other than the one that called `span.end()`, or after `on_end` has fired, you are in the invalid regime. The guardrail is a hard check in the exporter: before encoding, assert that every `ge...",
    "operational_effect": "Downstream, the effect is a systematic underestimation of token spend that is proportional to the fraction of spans whose enrichment lost the race. In a streaming agent this is typically 100% of `gen_ai.chat` spans, because the usage chunk always arrives after the stream closes. Cost dashboards built on `sum(gen_ai.usage.input_tokens)` report near-zero; rate..."
  },
  "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": "ec9a154b6dff572f14c1d5f778d960d16f3ba1247cf94ea68fd9288f68b0f84f",
    "verified_classes": [
      "GenAiUsageAttributionError",
      "SpanProto",
      "ProviderUsage"
    ],
    "verified_functions": [
      "encode_span",
      "enrich_after_close",
      "validate_before_export",
      "export_path"
    ],
    "verified_functions_count": 4,
    "defensive_guards_count": 3,
    "raised_exceptions": [
      "GenAiUsageAttributionError"
    ],
    "sample_fail_closed_invariants": [],
    "stdlib_dependencies": [
      "__future__",
      "dataclasses"
    ],
    "observation_mode": "EMPIRICAL_BOUNDARY_CAPTURE",
    "zero_external_deps": true
  }
}
