{
  "schema_version": "1.3.0",
  "slug": "which-invariants-prevent-tool-call-span-attribute-loss-when-an-agent-retries-a-failed-tool-invocation-under-opentelemetry-genai-semantics",
  "title": "Which invariants prevent tool-call span attribute loss when an agent retries a failed tool invocation under OpenTelemetry GenAI semantics",
  "date_of_record": "2026 November 20",
  "scope": "Applies to agents emitting OTel GenAI spans (`gen_ai.operation.name`, `gen_ai.tool.name`, `gen_ai.usage.input_tokens`, `gen_ai.usage.output_tokens`) where a single logical tool call is retried, replayed, or hedged across multiple physical attempts. Does not apply to single-shot tool calls with no retry loop, nor to pure LLM completion spans without tool execution.",
  "projections": {
    "unified_markdown_uri": "https://corpusfetch.com/c/which-invariants-prevent-tool-call-span-attribute-loss-when-an-agent-retries-a-failed-tool-invocation-under-opentelemetry-genai-semantics.md",
    "method_python_slice_uri": "https://corpusfetch.com/c/which-invariants-prevent-tool-call-span-attribute-loss-when-an-agent-retries-a-failed-tool-invocation-under-opentelemetry-genai-semantics.py",
    "remnant_warrant_slice_uri": "https://corpusfetch.com/c/which-invariants-prevent-tool-call-span-attribute-loss-when-an-agent-retries-a-failed-tool-invocation-under-opentelemetry-genai-semantics.warrant.json"
  },
  "token_profile": {
    "full_markdown_tokens": 2056,
    "python_slice_tokens": 357,
    "warrant_slice_tokens": 220
  },
  "remnant_summary": {
    "core_distinction": "The GenAI semantic conventions define attributes on a **span**, but an agent's retry logic operates on a **logical call**. When a tool invocation fails and the agent retries, the naive mapping emits a *new sibling span per attempt* while the agent's internal bookkeeping treats them as *one call*. The distinction that matters: `gen_ai.usage.*_tokens` is defin...",
    "what_was_observed": "An agent with a `retry(max=3, backoff=exponential)` wrapper around a `search_documents` tool. The tool returned HTTP 503 on attempt 1, a malformed JSON payload on attempt 2 (parse error inside the tool, surfaced as a tool-level exception), and succeeded on attempt 3. The agent's span emitter was called inside the tool wrapper, so three sibling spans were produced: - Attempt 1: `gen_ai.tool.name=search_documents`, `ge...",
    "condition_of_validity": "The invariant `sum(gen_ai.usage.*_tokens over spans sharing gen_ai.tool.call.id) = logical call token cost` holds **only if**: - The agent emits exactly one span per physical attempt, and - `gen_ai.tool.call.id` is held constant across all attempts of the same logical call, and - The agent does **not** re-emit the input tokens as new consumption on retry whe...",
    "decision_boundary": "The boundary is: **does the agent's retry layer own span emission, or does the tool-call layer own it?** - If the **retry layer** owns it (span created inside the retry loop), the agent MUST thread a stable `call.id` from outside the loop into each attempt, and MUST mark non-final attempts with an explicit attribute (e.g., `gen_ai.tool.retry.attempt=N`, `gen...",
    "operational_effect": "Downstream, three things break simultaneously and silently: 1. **Cost attribution overcounts retried calls** by the ratio of attempts to successes, biased toward tools with high transient failure rates (network-bound search, rate-limited APIs). The bias is not uniform across tools, so per-tool cost comparisons are corrupted, not just scaled. 2. **Success-rat..."
  },
  "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": "024d74c0adc69ff78fdb7cca1f7cd2d16a31b046e177562b2e1453e5a3f82185",
    "verified_classes": [
      "ToolSpanInvariantError",
      "ToolAttempt",
      "LogicalToolCall"
    ],
    "verified_functions": [
      "emit_tool_call_span",
      "record",
      "validate",
      "total_tokens",
      "logical_tokens"
    ],
    "verified_functions_count": 5,
    "defensive_guards_count": 4,
    "raised_exceptions": [
      "ToolSpanInvariantError"
    ],
    "sample_fail_closed_invariants": [],
    "stdlib_dependencies": [
      "dataclasses",
      "uuid"
    ],
    "observation_mode": "EMPIRICAL_BOUNDARY_CAPTURE",
    "zero_external_deps": true
  }
}
