{
  "schema_version": "1.3.0",
  "slug": "in-what-scenarios-must-an-instance-reject-a-genai-span-whose-token-usage-counters-have-already-been-emitted-before-the-tool-call-result-is-known",
  "title": "In what scenarios must an instance reject a GenAI span whose token usage counters have already been emitted before the tool-call result is known",
  "date_of_record": "2026 November 24",
  "scope": "Applies to any agent runtime emitting OpenTelemetry GenAI semantic-convention spans (`gen_ai.*` attributes) where a single logical agent turn spans an LLM inference and one or more tool calls, and where token/cost metrics are exported as cumulative counters or histograms. Does not apply to pure stateless inference logging with no tool-call fan-out, nor to batch offline scoring pipelines.",
  "projections": {
    "unified_markdown_uri": "https://corpusfetch.com/c/in-what-scenarios-must-an-instance-reject-a-genai-span-whose-token-usage-counters-have-already-been-emitted-before-the-tool-call-result-is-known.md",
    "method_python_slice_uri": "https://corpusfetch.com/c/in-what-scenarios-must-an-instance-reject-a-genai-span-whose-token-usage-counters-have-already-been-emitted-before-the-tool-call-result-is-known.py",
    "remnant_warrant_slice_uri": "https://corpusfetch.com/c/in-what-scenarios-must-an-instance-reject-a-genai-span-whose-token-usage-counters-have-already-been-emitted-before-the-tool-call-result-is-known.warrant.json"
  },
  "token_profile": {
    "full_markdown_tokens": 2042,
    "python_slice_tokens": 290,
    "warrant_slice_tokens": 220
  },
  "remnant_summary": {
    "core_distinction": "The GenAI semantic conventions treat an LLM inference span and a tool-execution span as sibling spans under an agent invoke span, with `gen_ai.usage.input_tokens` / `gen_ai.usage.output_tokens` attached to the inference span and `gen_ai.tool.name` / `gen_ai.tool.call.id` attached to the tool span. The non-obvious boundary is this: **token usage is finalized ...",
    "what_was_observed": "A tool-using agent (single model, three-round loop: inference → tool → inference → tool → inference) emitted the following trace under load. The runtime used an async span processor with a 500ms batch timeout, and the agent loop reused a cached `Span` handle across tool round-trips to \"avoid re-creating spans.\" Trace excerpt as exported to the collector: Two distinct defects are visible: 1. **B1 is exported twice** w...",
    "condition_of_validity": "The invariant holds—and the guardrail is mandatory—only when all three of the following are true: - **Usage is emitted as a counter or histogram delta, not as a gauge snapshot.** A gauge (`gen_ai.usage.input_tokens` read as last-value) is idempotent under re-export; a counter increment is not. The GenAI conventions permit both; the double-count only bites th...",
    "decision_boundary": "An instance must **reject and re-emit** rather than mutate when it detects that a `gen_ai.usage.*` attribute is being set on a span whose end timestamp is already populated, or when a span ID appears in more than one metric export within a single agent turn. The decision rule: - If `span.end_time is not None` and the runtime attempts `set_attribute(\"gen_ai.u...",
    "operational_effect": "With the guardrail in place, the observed trace becomes: Turn cost is the sum of three distinct inference spans, each with a stable ID and an ordinal that survives out-of-order export. The metric processor no longer sees duplicate `(span_id, usage)` pairs. The `gen_ai.span.reentry_detected` event fires zero times in steady state and once per defect when a ru..."
  },
  "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": "ce4eeb526c1379ab4b5e93e07d287f77fad39e952d1472ade6d10e298432cb36",
    "verified_classes": [
      "PrematureUsageFinalizationError",
      "GenAISpan",
      "UsageMetricExporter"
    ],
    "verified_functions": [
      "set_usage",
      "__init__",
      "export"
    ],
    "verified_functions_count": 3,
    "defensive_guards_count": 3,
    "raised_exceptions": [
      "PrematureUsageFinalizationError"
    ],
    "sample_fail_closed_invariants": [],
    "stdlib_dependencies": [
      "dataclasses",
      "typing"
    ],
    "observation_mode": "EMPIRICAL_BOUNDARY_CAPTURE",
    "zero_external_deps": true
  }
}
