{
  "schema_version": "1.3.0",
  "slug": "which-invariants-prevent-span-attribute-divergence-from-breaking-token-cost-correlation-across-agent-tool-call-traces",
  "title": "Which invariants prevent span-attribute divergence from breaking token/cost correlation across agent tool-call traces",
  "date_of_record": "2026 November 25",
  "scope": "Applies to agents emitting OpenTelemetry GenAI semantic-convention spans (`gen_ai.*`) where a parent agent span fans out to child `execute_tool` spans and LLM invocation spans, and where cost is reconstructed post-hoc by joining `gen_ai.usage.input_tokens`/`output_tokens` to a model-price table keyed on `gen_ai.request.model`. Does not cover single-shot, single-span inference where no fan-out or join occurs.",
  "projections": {
    "unified_markdown_uri": "https://corpusfetch.com/c/which-invariants-prevent-span-attribute-divergence-from-breaking-token-cost-correlation-across-agent-tool-call-traces.md",
    "method_python_slice_uri": "https://corpusfetch.com/c/which-invariants-prevent-span-attribute-divergence-from-breaking-token-cost-correlation-across-agent-tool-call-traces.py",
    "remnant_warrant_slice_uri": "https://corpusfetch.com/c/which-invariants-prevent-span-attribute-divergence-from-breaking-token-cost-correlation-across-agent-tool-call-traces.warrant.json"
  },
  "token_profile": {
    "full_markdown_tokens": 1858,
    "python_slice_tokens": 388,
    "warrant_slice_tokens": 220
  },
  "remnant_summary": {
    "core_distinction": "The naive mental model treats a trace as a faithful ledger: every span carries its own token counts, sum the leaves, multiply by price, done. The real failure mode is **attribute provenance divergence** — the same logical attribute (model identity, token count, tool name) is populated at different points in the agent's execution graph by different code paths...",
    "what_was_observed": "A tool-augmented agent (planner → 3 parallel tool calls → synthesis) under a provider rate-limit produced the following trace fragment: Three distinct divergences surfaced: 1. **Retry re-stamping.** The retry reused the span object and overwrote `gen_ai.request.model` from `gpt-4o` to `gpt-4o-mini`. The exporter saw one span. The cost join priced 1802 input tokens at mini rates even though the first attempt was bille...",
    "condition_of_validity": "The correlation invariant holds only when all three of the following are true: - **I1 — Attribute immutability per span.** Once a span is `Start`ed, `gen_ai.request.model`, `gen_ai.usage.input_tokens`, and `gen_ai.usage.output_tokens` are write-once. A retry MUST create a new span (new `span_id`) with `gen_ai.operation.name` preserved and a link (`SpanLink`)...",
    "decision_boundary": "The boundary between \"correlatable trace\" and \"silently mispriced trace\" is crossed at the moment a retry or fallback mutates an attribute that a downstream join depends on, *without forking the span*. Operationally: - If your exporter is a **batching processor with attribute overwrite semantics** (the default OTel `BatchSpanProcessor` combined with a mutabl...",
    "operational_effect": "Enforcing I1–I3 changes three things: 1. **Cost accuracy becomes bounded.** With forked retry spans, the sum of `gen_ai.usage.input_tokens` across all spans with distinct `gen_ai.response.id` matches the provider invoice to within rounding. The 6–11% mid-band under-report disappears because the 4o attempt is no longer overwritten. 2. **Trace cardinality incr..."
  },
  "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": "6444b62cce7870baff49b82d760e93c2d7e8586ebf5a4f628444795f4c7d7bdf",
    "verified_classes": [
      "SpanProvenanceViolation",
      "GenAISpan",
      "CostLedger"
    ],
    "verified_functions": [
      "ingest_retry_attempt",
      "record",
      "total_tokens"
    ],
    "verified_functions_count": 3,
    "defensive_guards_count": 4,
    "raised_exceptions": [
      "SpanProvenanceViolation"
    ],
    "sample_fail_closed_invariants": [
      "fallback_model equals original; retry would be indistinguishable from a duplicate and cannot be deduped by response_id."
    ],
    "stdlib_dependencies": [
      "dataclasses",
      "typing"
    ],
    "observation_mode": "EMPIRICAL_BOUNDARY_CAPTURE",
    "zero_external_deps": true
  }
}
