{
  "schema_version": "1.3.0",
  "slug": "how-should-an-instance-reconcile-opentelemetry-genai-token-accounting-when-an-agent-span-is-exported-after-the-model-span-it-summarizes-has-already-be",
  "title": "How should an instance reconcile OpenTelemetry GenAI token accounting when an agent span is exported after the model span it summarizes has already been sampled out",
  "date_of_record": "2026 November 29",
  "scope": "Applies to agent runtimes that emit OpenTelemetry GenAI spans (`gen_ai.operation.name`, `gen_ai.usage.input_tokens`, `gen_ai.usage.output_tokens`) with a parent agent span whose lifecycle outlives the individual model/tool child spans it aggregates. Does not apply to single-shot inference services where the model span is the root span or where token usage is exported as OTel metrics independent of span sampling.",
  "projections": {
    "unified_markdown_uri": "https://corpusfetch.com/c/how-should-an-instance-reconcile-opentelemetry-genai-token-accounting-when-an-agent-span-is-exported-after-the-model-span-it-summarizes-has-already-be.md",
    "method_python_slice_uri": "https://corpusfetch.com/c/how-should-an-instance-reconcile-opentelemetry-genai-token-accounting-when-an-agent-span-is-exported-after-the-model-span-it-summarizes-has-already-be.py",
    "remnant_warrant_slice_uri": "https://corpusfetch.com/c/how-should-an-instance-reconcile-opentelemetry-genai-token-accounting-when-an-agent-span-is-exported-after-the-model-span-it-summarizes-has-already-be.warrant.json"
  },
  "token_profile": {
    "full_markdown_tokens": 1734,
    "python_slice_tokens": 353,
    "warrant_slice_tokens": 220
  },
  "remnant_summary": {
    "core_distinction": "The failure is not \"sampling drops spans.\" It is that **GenAI token accounting is defined as an attribute of the span that observed the tokens, but agent-level accounting is defined as the sum of tokens consumed during the agent's lifetime**—and these two definitions live on different spans with different sampling fates. A head-sampling policy that keeps the...",
    "what_was_observed": "A multi-step agent (planner → 6 tool calls → synthesizer) emitted a root span `agent.run` with `gen_ai.operation.name=invoke_agent` and child spans `chat` (model calls) and `execute_tool` (tool calls). The runtime used an incremental accumulator: each time a model child span ended, it added `response.usage.prompt_tokens` / `completion_tokens` to the parent's `gen_ai.usage.input_tokens` / `output_tokens` attributes vi...",
    "condition_of_validity": "The reconciliation invariant holds only when all of the following are true: 1. The agent span is the single writer of its own `gen_ai.usage.*` attributes, and the write happens **after** all child model spans have ended (i.e., at `agent.run` end, not incrementally). 2. The sampling decision for the agent span is independent of child span export—i.e., the acc...",
    "decision_boundary": "The decision the runtime must make at each model-call completion: - **If the child model span is sampled AND the parent agent span is recording:** accumulate from the provider response's `usage` field into a runtime-owned counter; do not read from the child span's attributes (they may be truncated by attribute limits). - **If the child is not sampled but the...",
    "operational_effect": "The downstream effect of getting this wrong is a *silent* cost and capacity mis-attribution that survives aggregation: because the wrong numbers are structurally valid (`int` attributes on a correctly-named span), no schema validation, no dashboard, and no alert fires. The only detector is a cross-span reconciliation query that compares `invoke_agent` token ..."
  },
  "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": "33ec1bc472e2f64dfbed7b84454b9e491d8fc313f4178fdd93dde320d6849164",
    "verified_classes": [
      "TokenAccountingInvariantError",
      "UsageRecord",
      "AgentSpanState"
    ],
    "verified_functions": [
      "record_model_call",
      "finalize_agent_span"
    ],
    "verified_functions_count": 2,
    "defensive_guards_count": 3,
    "raised_exceptions": [
      "TokenAccountingInvariantError"
    ],
    "sample_fail_closed_invariants": [
      "usage record missing for one or more model calls; refusing to export partial gen_ai.usage.* on agent span"
    ],
    "stdlib_dependencies": [
      "dataclasses"
    ],
    "observation_mode": "EMPIRICAL_BOUNDARY_CAPTURE",
    "zero_external_deps": true
  }
}
