{
  "schema_version": "1.3.0",
  "slug": "which-invariants-prevent-token-accounting-drift-across-parent-child-agent-spans-when-tool-call-attributes-are-emitted-after-the-model-invocation-event",
  "title": "Which invariants prevent token-accounting drift across parent/child agent spans when tool-call attributes are emitted after the model-invocation event closes",
  "date_of_record": "2026 November 30",
  "scope": "Applies to agent runtimes that emit OpenTelemetry GenAI semantic-convention spans (`gen_ai.*`) where a single agent step (one parent `invoke_agent` span) contains one or more `chat`/model-invocation child spans plus `execute_tool` spans. Does not apply to single-shot completion logging where no parent span aggregates child token usage.",
  "projections": {
    "unified_markdown_uri": "https://corpusfetch.com/c/which-invariants-prevent-token-accounting-drift-across-parent-child-agent-spans-when-tool-call-attributes-are-emitted-after-the-model-invocation-event.md",
    "method_python_slice_uri": "https://corpusfetch.com/c/which-invariants-prevent-token-accounting-drift-across-parent-child-agent-spans-when-tool-call-attributes-are-emitted-after-the-model-invocation-event.py",
    "remnant_warrant_slice_uri": "https://corpusfetch.com/c/which-invariants-prevent-token-accounting-drift-across-parent-child-agent-spans-when-tool-call-attributes-are-emitted-after-the-model-invocation-event.warrant.json"
  },
  "token_profile": {
    "full_markdown_tokens": 1603,
    "python_slice_tokens": 368,
    "warrant_slice_tokens": 220
  },
  "remnant_summary": {
    "core_distinction": "The failure is not \"wrong token counts on a span.\" It is a **temporal ordering violation**: the GenAI conventions treat `gen_ai.usage.input_tokens` / `output_tokens` as terminal attributes of a model-invocation span — they are knowable only when the response finishes. But an agent step's *total* cost is only knowable after every tool call in the step complet...",
    "what_was_observed": "A ReAct-style agent step executed: 1. `chat` span A opens → model returns a tool call → span A closes with `input_tokens=1180, output_tokens=42`. 2. `execute_tool` span B opens → tool runs 340ms → returns a 9KB result → span B closes. 3. The runtime re-invokes the model with the tool result appended. `chat` span C opens → returns final answer → closes with `input_tokens=2410, output_tokens=88`. 4. The runtime *then* ...",
    "condition_of_validity": "The aggregation invariant `parent.gen_ai.usage.input_tokens == Σ child.gen_ai.usage.input_tokens` holds **only if** all of the following are true: - Every model invocation that contributes to the agent step is emitted as a child span of the parent *before* the parent closes, and - No child span is exported to a backend that performs rollup *before* its final...",
    "decision_boundary": "The guardrail is a **two-phase emission rule**, not a retry or a reconciliation job: - Model-invocation spans MUST set `gen_ai.usage.*` as their *last* operation before `end()`, and MUST NOT be exported until `end()` — i.e., usage is a terminal, not incremental, attribute. - The parent `invoke_agent` span MUST NOT carry aggregated `gen_ai.usage.*` at all if ...",
    "operational_effect": "Without the invariant, token dashboards drift by exactly the number of tool-call round-trips per step — typically 10–40% undercount on multi-tool agents, and *overcount* on retry-heavy agents because stable `tool.call.id` collapses retries. Cost attribution, rate-limit budgeting, and context-window-pressure alerts all inherit the error. The fix is cheap (ter..."
  },
  "matched_failure_symptoms": [
    "SUBPROCESS_PIPE_HANG_OR_FD_LEAK"
  ],
  "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": "cd9d29fda93f15f989d441d3465c8512ac4f9fb8d00b70f45446757f9f49c75d",
    "verified_classes": [
      "UsageInvariantError",
      "GenAISpan"
    ],
    "verified_functions": [
      "finalize_and_export",
      "aggregate_parent_usage",
      "dedup_retries_by_tool_call"
    ],
    "verified_functions_count": 3,
    "defensive_guards_count": 8,
    "raised_exceptions": [
      "UsageInvariantError"
    ],
    "sample_fail_closed_invariants": [],
    "stdlib_dependencies": [
      "dataclasses",
      "typing"
    ],
    "observation_mode": "EMPIRICAL_BOUNDARY_CAPTURE",
    "zero_external_deps": true
  }
}
