{
  "schema_version": "1.3.0",
  "slug": "what-happens-when-an-instance-emits-genai-tool-call-spans-whose-gen-ai-usage-token-counters-are-re-derived-at-span-close-instead-of-at-model-response",
  "title": "What happens when an instance emits GenAI tool-call spans whose `gen_ai.usage.*` token counters are re-derived at span close instead of at model-response receipt",
  "date_of_record": "2026 November 24",
  "scope": "Applies to any agent runtime that emits OpenTelemetry spans conforming to the GenAI semantic conventions (`gen_ai.operation.name`, `gen_ai.request.model`, `gen_ai.usage.input_tokens`, `gen_ai.usage.output_tokens`, tool-call child spans) and computes token usage at span finalization rather than at the moment the model response is parsed. Does not apply to batch/offline token reconciliation pipelines that operate on raw provider payloads outside the trace pipeline.",
  "projections": {
    "unified_markdown_uri": "https://corpusfetch.com/c/what-happens-when-an-instance-emits-genai-tool-call-spans-whose-gen-ai-usage-token-counters-are-re-derived-at-span-close-instead-of-at-model-response.md",
    "method_python_slice_uri": "https://corpusfetch.com/c/what-happens-when-an-instance-emits-genai-tool-call-spans-whose-gen-ai-usage-token-counters-are-re-derived-at-span-close-instead-of-at-model-response.py",
    "remnant_warrant_slice_uri": "https://corpusfetch.com/c/what-happens-when-an-instance-emits-genai-tool-call-spans-whose-gen-ai-usage-token-counters-are-re-derived-at-span-close-instead-of-at-model-response.warrant.json"
  },
  "token_profile": {
    "full_markdown_tokens": 1741,
    "python_slice_tokens": 241,
    "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 *observations of a discrete model invocation*. The provider returns these numbers exactly once, in the response envelope, at the instant the completion is produced. There is a second, tempting source of the same numbers: the runtime's own tokenizer, re-run at...",
    "what_was_observed": "A multi-tool agent loop (plan → `tool.search` → `tool.fetch` → synthesize) emitted well-formed GenAI spans. `gen_ai.operation.name=\"chat\"` parent spans carried `gen_ai.request.model`, and child `execute_tool` spans carried `gen_ai.tool.name` and `gen_ai.tool.call.id`. On the surface the trace was convention-compliant. The defect surfaced under a specific trigger: **a tool call whose result was truncated by the runtim...",
    "condition_of_validity": "The invariant that makes `gen_ai.usage.*` trustworthy is: > Token usage attributes on a GenAI span MUST be copied from the provider response envelope at the moment that response is received, before any mutation of the message history, context clipping, or retry logic can occur. This is valid only when three conditions hold simultaneously: 1. **Capture-at-rec...",
    "decision_boundary": "The boundary is *where in the request lifecycle the token attributes are written*: - **Write at response receipt** → attributes reflect provider billing; safe for cost and budget governors; requires the span to be open and mutable at that instant. - **Write at span close (re-derived)** → attributes reflect the runtime's post-mutation view; unsafe whenever hi...",
    "operational_effect": "The practical consequence is a **silent budget-governor drift**. A governor that reads `gen_ai.usage.input_tokens` from spans to enforce a per-session token cap will under-count by the clipped-tool-result delta and the retry delta. The cap is never hit at the intended threshold; the session runs 6–12% over budget before the governor trips. Because the trace ..."
  },
  "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": "c8ae6dcc2c78d501f13aac08d921b18c4237a775f072f45b36b885d970443893",
    "verified_classes": [
      "UsageCaptureError",
      "GenAISpanEmitter"
    ],
    "verified_functions": [
      "__init__",
      "bind_usage_at_receipt",
      "close_span"
    ],
    "verified_functions_count": 3,
    "defensive_guards_count": 3,
    "raised_exceptions": [
      "UsageCaptureError"
    ],
    "sample_fail_closed_invariants": [
      "provider response carried no usage envelope; cannot bind gen_ai.usage.* at receipt. Refusing to defer to span close.",
      "chat span closed without usage bound at receipt; re-derived counters are not admissible for cost governance."
    ],
    "stdlib_dependencies": [],
    "observation_mode": "EMPIRICAL_BOUNDARY_CAPTURE",
    "zero_external_deps": true
  }
}
