{
  "schema_version": "1.3.0",
  "slug": "how-does-an-instance-detect-a-mid-span-token-budget-overrun-when-opentelemetry-genai-spans-close-after-the-cost-has-already-been-incurred",
  "title": "How does an instance detect a mid-span token-budget overrun when OpenTelemetry GenAI spans close after the cost has already been incurred",
  "date_of_record": "2026 November 28",
  "scope": "Applies to autonomous agents instrumented with OpenTelemetry GenAI semantic conventions (`gen_ai.*` attributes) where span lifetime is decoupled from the actual token consumption event. Does not apply to synchronous single-call wrappers where span open/close brackets the model invocation exactly.",
  "projections": {
    "unified_markdown_uri": "https://corpusfetch.com/c/how-does-an-instance-detect-a-mid-span-token-budget-overrun-when-opentelemetry-genai-spans-close-after-the-cost-has-already-been-incurred.md",
    "method_python_slice_uri": "https://corpusfetch.com/c/how-does-an-instance-detect-a-mid-span-token-budget-overrun-when-opentelemetry-genai-spans-close-after-the-cost-has-already-been-incurred.py",
    "remnant_warrant_slice_uri": "https://corpusfetch.com/c/how-does-an-instance-detect-a-mid-span-token-budget-overrun-when-opentelemetry-genai-spans-close-after-the-cost-has-already-been-incurred.warrant.json"
  },
  "token_profile": {
    "full_markdown_tokens": 1613,
    "python_slice_tokens": 268,
    "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 *attributes recorded at span close*. They are telemetry facts about what already happened, not control signals. A span is a **post-hoc ledger entry**, not a **budget gate**. The failure boundary is the gap between two invariants that operators routinely confl...",
    "what_was_observed": "A planner agent (`gen_ai.operation.name = \"chat\"`, `gen_ai.system = \"openai\"`) spawns a tool-calling loop. Each iteration emits a child span with `gen_ai.operation.name = \"execute_tool\"` and a sibling `chat` span. The orchestrator was configured to enforce a 200k-token ceiling per task by inspecting the *parent* span's aggregated `gen_ai.usage.total_tokens` after each child span closed. Trace of the failure: - Iterat...",
    "condition_of_validity": "The post-hoc ledger is a valid enforcement mechanism **only if** the agent can predict the next call's input size before dispatch. That requires an *a priori* estimate of: - the serialized length of the accumulated context (memory + prior tool outputs + schemas), - the tokenizer's behavior on that specific payload, - provider-side truncation or caching that ...",
    "decision_boundary": "The boundary is crossed when the agent's control flow depends on a `gen_ai.usage.*` value that has not yet been written by the span that will contain it. Concretely, an agent is **outside** the safe region when any of the following holds: - It reads `gen_ai.usage.input_tokens` from a span that is still open (value is unset → reads as 0 or absent → passes the...",
    "operational_effect": "Systems that treat the GenAI ledger as a gate exhibit a characteristic overrun profile: bounded per-step, unbounded cumulative, with the overshoot concentrated in the last iteration before any external kill. The overshoot magnitude is approximately the size of the final call's input, which on tool-heavy loops is the largest single call in the run. 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": "b1e773d69e6c952ad442593fb497dff5ccfb7735cde4516dae26bb11f12a7e54",
    "verified_classes": [
      "BudgetEnforcementViolation",
      "SpanUsage"
    ],
    "verified_functions": [
      "pre_dispatch_budget_gate",
      "reconcile_post_close"
    ],
    "verified_functions_count": 2,
    "defensive_guards_count": 3,
    "raised_exceptions": [
      "BudgetEnforcementViolation"
    ],
    "sample_fail_closed_invariants": [
      "attempted to read gen_ai.usage.input_tokens from an open span; value is unset and must not gate dispatch"
    ],
    "stdlib_dependencies": [
      "dataclasses"
    ],
    "observation_mode": "EMPIRICAL_BOUNDARY_CAPTURE",
    "zero_external_deps": true
  }
}
