{
  "schema_version": "1.3.0",
  "slug": "what-happens-when-an-instance-records-tool-call-spans-before-the-genai-semantic-convention-attributes-resolve-and-the-parent-invocation-span-closes-wi",
  "title": "What happens when an instance records tool-call spans before the GenAI semantic convention attributes resolve, and the parent invocation span closes with a token count that no longer reconciles",
  "date_of_record": "2026 November 30",
  "scope": "Applies to any agent runtime that emits OpenTelemetry spans under the GenAI semantic conventions (`gen_ai.*`) where a model-invocation span is the parent of one or more tool-execution spans, and where token usage attributes are computed after tool calls complete. Does not apply to single-shot completions with no tool calls, nor to runtimes that buffer the entire trace in-process and emit it atomically after the run terminates.",
  "projections": {
    "unified_markdown_uri": "https://corpusfetch.com/c/what-happens-when-an-instance-records-tool-call-spans-before-the-genai-semantic-convention-attributes-resolve-and-the-parent-invocation-span-closes-wi.md",
    "method_python_slice_uri": "https://corpusfetch.com/c/what-happens-when-an-instance-records-tool-call-spans-before-the-genai-semantic-convention-attributes-resolve-and-the-parent-invocation-span-closes-wi.py",
    "remnant_warrant_slice_uri": "https://corpusfetch.com/c/what-happens-when-an-instance-records-tool-call-spans-before-the-genai-semantic-convention-attributes-resolve-and-the-parent-invocation-span-closes-wi.warrant.json"
  },
  "token_profile": {
    "full_markdown_tokens": 2295,
    "python_slice_tokens": 437,
    "warrant_slice_tokens": 220
  },
  "remnant_summary": {
    "core_distinction": "The GenAI semantic conventions model a model invocation as a span carrying `gen_ai.usage.input_tokens`, `gen_ai.usage.output_tokens`, `gen_ai.request.model`, and `gen_ai.response.finish_reasons`. A tool call is modeled as a *child* span (`execute_tool`) whose `gen_ai.tool.name` and `gen_ai.tool.call.id` bind it to the invocation that requested it. The conven...",
    "what_was_observed": "A retrieval-augmented agent executed a four-step loop: plan → `search_docs` → synthesize → `write_file`. The runtime wrapped the whole loop in a single `gen_ai.chat` span (the invocation), with two `execute_tool` children. Token accounting was accumulated in a mutable counter on the invocation context and flushed to span attributes in the span's `on_end` handler. The trace exported cleanly. Then three downstream cons...",
    "condition_of_validity": "The invariant that makes GenAI invocation spans sound is: > **A `gen_ai.chat` span's token attributes and `finish_reasons` describe exactly one model response, and the span must end before any downstream tool execution that the response requested begins its own span.** This holds only when the agent loop emits one invocation span *per model call*, with each ...",
    "decision_boundary": "The runtime must choose, at span-construction time, between two models, and it must choose *before* the first token is emitted: - **Per-call invocation spans** (conformant). One `gen_ai.chat` span per model response. Token attributes set exactly once, at `end()`, from the response object. Tool-call spans are children of the requesting invocation. Aggregation...",
    "operational_effect": "The practical consequence is that **trace-based cost and evaluation are only as trustworthy as the span's causal closure at `end()`.** A trace that looks complete — every span has tokens, every tool call has a parent — can be systematically wrong in a way that no schema validator catches, because the schema validates *presence* of attributes, not their *prov..."
  },
  "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": "f665fe92f9f736b509142897fa41fcf021ab69d4cd8b55d9fff97c12c95b888f",
    "verified_classes": [
      "InvocationSpanClosureError",
      "InvocationSpan"
    ],
    "verified_functions": [
      "record_response",
      "end",
      "attach_tool_child"
    ],
    "verified_functions_count": 3,
    "defensive_guards_count": 6,
    "raised_exceptions": [
      "InvocationSpanClosureError"
    ],
    "sample_fail_closed_invariants": [
      "invocation span ended with no bound response; gen_ai.usage.* attributes have no provenance.",
      "tool-call child attached before invocation span ended; the requesting response has not been recorded, so the call_id can"
    ],
    "stdlib_dependencies": [
      "dataclasses",
      "typing"
    ],
    "observation_mode": "EMPIRICAL_BOUNDARY_CAPTURE",
    "zero_external_deps": true
  }
}
