{
  "schema_version": "1.3.0",
  "slug": "how-does-an-instance-detect-token-accounting-drift-when-an-opentelemetry-genai-agent-span-is-closed-by-a-different-process-than-the-one-that-opened-it",
  "title": "How does an instance detect token-accounting drift when an OpenTelemetry GenAI agent span is closed by a different process than the one that opened it",
  "date_of_record": "2026 November 22",
  "scope": "Applies to any agent runtime that emits OpenTelemetry GenAI semantic-convention spans (`gen_ai.operation.name`, `gen_ai.usage.input_tokens`, `gen_ai.usage.output_tokens`, `gen_ai.tool.name`, `gen_ai.tool.call.id`) across a process boundary — e.g., a tool-call executed in a sandboxed worker, a sub-agent dispatched to a sidecar, or a resume-after-checkpoint flow where the span is finalized by the resuming process. Does not apply to single-process, single-threaded instrumentation where the span opener and closer share the same in-memory `Span` handle.",
  "projections": {
    "unified_markdown_uri": "https://corpusfetch.com/c/how-does-an-instance-detect-token-accounting-drift-when-an-opentelemetry-genai-agent-span-is-closed-by-a-different-process-than-the-one-that-opened-it.md",
    "method_python_slice_uri": "https://corpusfetch.com/c/how-does-an-instance-detect-token-accounting-drift-when-an-opentelemetry-genai-agent-span-is-closed-by-a-different-process-than-the-one-that-opened-it.py",
    "remnant_warrant_slice_uri": "https://corpusfetch.com/c/how-does-an-instance-detect-token-accounting-drift-when-an-opentelemetry-genai-agent-span-is-closed-by-a-different-process-than-the-one-that-opened-it.warrant.json"
  },
  "token_profile": {
    "full_markdown_tokens": 2025,
    "python_slice_tokens": 337,
    "warrant_slice_tokens": 220
  },
  "remnant_summary": {
    "core_distinction": "The OpenTelemetry GenAI conventions treat token usage as a **span-terminal attribute set**: `gen_ai.usage.input_tokens` and `gen_ai.usage.output_tokens` are expected to be attached before `span.end()`, and the convention implicitly assumes the process that opened the span also observed the token counts (because it made the model call). This is an *observabil...",
    "what_was_observed": "A three-process agent pipeline: orchestrator (A) opens `invoke_agent` span, dispatches to a tool-executor sidecar (B) over MCP, which calls the model provider and receives a usage block, then returns the tool result to A. A closes the span. Trace exported to the collector showed, for the `invoke_agent` span: The provider's actual usage block, logged by B, was: The span was structurally valid, passed every OTel schema...",
    "condition_of_validity": "The invariant \"trace token sums reflect billed tokens\" holds **only if** one of the following is true for every span in the trace: - **Same-process ownership:** the process that calls the model also holds the `Span` handle and sets usage attributes from the provider's response before `span.end()`. - **Out-of-band usage return:** the executing process (B) ret...",
    "decision_boundary": "The decision an instance must make at span-close time is: **do I have authoritative usage for this span, or do I have a guess?** The boundary is binary and must be resolved *before* `span.end()`: - If the closer directly observed the provider response → set `gen_ai.usage.*` from it, mark span authoritative. - If the closer received a structured usage block a...",
    "operational_effect": "Downstream, the drift compounds in three ways: 1. **Cost attribution silently misallocates.** Per-agent and per-tool cost dashboards built on `invoke_agent` sums under-report the boundary-crossing agents and over-report same-process agents, producing a ranking that looks stable and is systematically wrong. The error is not noise; it is biased toward whicheve..."
  },
  "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": "fd8d1a1969b12775e908adec191ec05ec84faabbb14c9aa9081df9b5e32994c0",
    "verified_classes": [
      "NonAuthoritativeUsageError"
    ],
    "verified_functions": [
      "close_agent_span",
      "reconcile_trace_usage"
    ],
    "verified_functions_count": 2,
    "defensive_guards_count": 2,
    "raised_exceptions": [
      "NonAuthoritativeUsageError"
    ],
    "sample_fail_closed_invariants": [
      "structured handoff usage arrived without gen_ai.tool.call.id; cannot attribute tokens to a specific agent span"
    ],
    "stdlib_dependencies": [],
    "observation_mode": "EMPIRICAL_BOUNDARY_CAPTURE",
    "zero_external_deps": true
  }
}
