{
  "schema_version": "1.3.0",
  "slug": "which-invariants-prevent-semantic-convention-token-metrics-from-silently-under-counting-agent-cost-across-provider-retries",
  "title": "Which invariants prevent semantic-convention token metrics from silently under-counting agent cost across provider retries",
  "date_of_record": "2026 November 26",
  "scope": "Applies to LLM agent runtimes that emit OpenTelemetry-style GenAI semantic-convention metrics (`gen_ai.usage.input_tokens`, `gen_ai.usage.output_tokens`, `gen_ai.client.operation.duration`) aggregated per logical agent step. Does not apply to single-shot, non-retried inference calls where one physical request maps 1:1 to one logical step.",
  "projections": {
    "unified_markdown_uri": "https://corpusfetch.com/c/which-invariants-prevent-semantic-convention-token-metrics-from-silently-under-counting-agent-cost-across-provider-retries.md",
    "method_python_slice_uri": "https://corpusfetch.com/c/which-invariants-prevent-semantic-convention-token-metrics-from-silently-under-counting-agent-cost-across-provider-retries.py",
    "remnant_warrant_slice_uri": "https://corpusfetch.com/c/which-invariants-prevent-semantic-convention-token-metrics-from-silently-under-counting-agent-cost-across-provider-retries.warrant.json"
  },
  "token_profile": {
    "full_markdown_tokens": 1262,
    "python_slice_tokens": 190,
    "warrant_slice_tokens": 220
  },
  "remnant_summary": {
    "core_distinction": "The GenAI semantic conventions define *usage* attributes as properties of a *physical* model invocation: `gen_ai.usage.input_tokens` is the token count the provider *billed for on that wire call*. The agent runtime, however, reasons in *logical* units: one \"step\" is one intention to call a tool or produce a decision, and it may fan out into N physical calls ...",
    "what_was_observed": "A tool-calling agent issued a `search_documents` tool call. The provider returned HTTP 429 on the first attempt; the SDK's retry layer re-issued the identical request 1.2s later, which succeeded. Both wire calls were instrumented. The instrumentation emitted: - Attempt 1: `gen_ai.usage.input_tokens=4180`, `gen_ai.response.finish_reasons=[\"error\"]`, duration 1180ms. - Attempt 2: `gen_ai.usage.input_tokens=4180`, `gen_...",
    "condition_of_validity": "The naive invariant \"sum `gen_ai.usage.input_tokens` across all spans tagged with a step\" is *only* valid when: 1. Every physical attempt emits a span (retry layers that swallow failed attempts before instrumentation break this), **and** 2. The aggregation is additive (sum/count), not last-value-wins, **and** 3. Failed attempts are not filtered out by a `fin...",
    "decision_boundary": "The boundary is: **does the aggregation key represent a physical call or a logical step?** If physical, additive sums are correct but the metric answers \"what did the provider bill,\" not \"what did this step cost the agent,\" and retries must be counted as separate rows. If logical, the pipeline must *explicitly* sum attempts and must carry a `gen_ai.retry.att...",
    "operational_effect": "Under-counting scales with provider instability: the worse the 429/5xx rate, the larger the silent gap, so the metric degrades *precisely when* the system is under stress and cost decisions matter most. Latency SLOs derived from `gen_ai.client.operation.duration` are structurally optimistic by the backoff sum. The remediation is to treat the semantic convent..."
  },
  "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": "49184ec67d426b0c1a89753edb45ec79cfeedd2ec79de3555826f5d85e20a367",
    "verified_classes": [
      "TokenReconciliationError",
      "PhysicalAttempt",
      "LogicalStep"
    ],
    "verified_functions": [
      "emit_step_metrics",
      "record",
      "billable_input_tokens",
      "logical_duration_ms",
      "reconcile"
    ],
    "verified_functions_count": 5,
    "defensive_guards_count": 1,
    "raised_exceptions": [
      "TokenReconciliationError"
    ],
    "sample_fail_closed_invariants": [],
    "stdlib_dependencies": [
      "dataclasses"
    ],
    "observation_mode": "EMPIRICAL_BOUNDARY_CAPTURE",
    "zero_external_deps": true
  }
}
