Skip to content

fix(ai): the spend series exists at $0, a gateway call counts as local or hosted by the backend that served it, and the ledger exposition stops reusing the OTLP names #1631

Closed

Description

@dan-petty

Friction

  • devops_cli_ai_spend_usd_total has never existed in Prometheus over 60 days. It is added to only when a request's cost is non-zero, and every request was local.
  • Three timeseries targets without a fallback show "No data" instead of $0, which cannot be told apart from a broken pipeline: "Spend per Hour by Backend Server" (k8s/monitoring/dashboards/ai-spend.json:577, :586) and the spend-by-server panel in k8s/monitoring/dashboards/devops-cli.json:865.
  • Whether a call is local (savings) or hosted (spend) is decided by the host it was sent to, never by the backend that served it. A hosted route added to the gateway would show $0 spend and count as local savings.
  • A custom price override for a local model is booked as cloud spend and drops out of savings.
  • AISpendCollector publishes lifetime-ledger values under the OTLP counter names (devops_cli_ai_requests_total, devops_cli_ai_tokens_total with type="total", devops_cli_ai_spend_usd_total). Nothing scrapes it today. If anything did, the dashboard's name-only sums would double requests and spend and triple tokens.
  • The one gateway appears under two server values, its public host and its in-cluster Service, so by-server panels show it twice.
  • Today's values are right: every gateway route is Ollama and no override exists. This item keeps them right.

Evidence

Checked at main 3dd7fa6 (v0.2.36) on 2026-10-17, read-only.

  • src/devops_cli/ai/spend/ledger.py:709-710 emits spend only if cost. The SDK counter is created on its first add (src/devops_cli/telemetry/tracer.py:1003-1018, :1063-1077).
  • Live: no spend series in 60 days; litellm_spend_metric_total also rose 0 in 24 h.
  • is_local (src/devops_cli/ai/spend/pricing.py:237-245, host helpers :152-234) takes server and provider only.
  • served_by reaches the ledger column and the backend label (ledger.py:675, :704) but not calculate_request_cost (pricing.py:509-528) or _emit_local_cost_equivalent (ledger.py:611).
  • In calculate_request_cost the override check (pricing.py:512) runs before the local check (:516-517).
  • tests/test_telemetry_instruments.py:391-420 pins a gateway call with an Ollama served_by booked as spend when the gateway host is not configured.
  • AISpendCollector (src/devops_cli/ai/spend/prometheus.py:76-109) is served by the non-service devops serve /metrics (src/devops_cli/server/routes/telemetry.py:59-84) and printed by devops ai cost prometheus (src/devops_cli/commands/ai_cost.py:558-564).
  • Live 7 d: the server label holds both gateway names. fix(ai): replies carry their server as an httpx2 origin, so a password in an AI URL stays out of output, the spend ledger, metrics, spans and cache #1110 changes how server is derived (an httpx2 origin).

Remedy

Tests first, in tests/test_telemetry_instruments.py, tests/test_ai_spend_pricing.py, tests/test_ai_spend_prometheus.py and tests/test_k8s_monitoring_dashboards.py. No network:

  • A local non-cached call emits one spend point of 0 next to its savings.
  • Every spend timeseries target in ai-spend.json and devops-cli.json ends in or vector(0), checked in one structural assertion over all targets.
  • A gateway call whose served_by is a configured local backend counts as local even when the gateway host is not configured. One whose served_by is a hosted API counts as spend. With no served_by, the host check still decides.
  • An override on a local server or model leaves the call in local savings.
  • No metric name AISpendCollector exports equals any Instrument.series in src/devops_cli/telemetry/instruments.py, and it exports no type="total" sample.

Code:

Key question for the owner: should a local override be recorded as a separate hardware or energy cost (proposed), or not at all.

Proposed: type/bug, P3-Low. Latent apart from three "No data" targets.

Proposed design

Problem statement

The AI spend tracking system has several issues: it only emits spend metrics for non-zero costs, leading to 'No data' displays in dashboards; it misclassifies calls as local or hosted based on the wrong server context; and the ledger exposition reuses OTLP metric names that conflict with actual instruments.

Acceptance criteria

  • Criterion 1: Every request, including cached ones, emits a spend point of 0 when it's a local call
    Verification: Run tests in tests/test_telemetry_instruments.py to verify local non-cached calls emit spend points with value 0
  • Criterion 2: Dashboard targets for AI spend show $0 instead of 'No data' when no spend has occurred
    Verification: Check that all three dashboard targets in ai-spend.json and devops-cli.json include or vector(0) fallbacks
  • Criterion 3: Gateway calls are classified correctly based on the backend they were served by, not the host that received the request
    Verification: Run tests in tests/test_ai_spend_pricing.py to verify gateway calls with served_by correctly determine local vs hosted status
  • Criterion 4: Local overrides are not recorded as cloud spend and remain in savings
    Verification: Run tests in tests/test_ai_spend_pricing.py to verify that custom pricing overrides on local servers keep the call classified as local
  • Criterion 5: AISpendCollector does not export metric names conflicting with OTLP instruments
    Verification: Verify that AISpendCollector exports no metrics under the same names as Instrument.series in src/devops_cli/telemetry/instruments.py

Key questions

  • Should a local override be recorded as a separate hardware or energy cost?: Not at all, according to the proposed remedy. (proposed)
  • Does the AISpendCollector export any metric names that conflict with OTLP instruments?: Yes, it exports metrics under names like devops_cli_ai_requests_total, devops_cli_ai_tokens_total and devops_cli_ai_spend_usd_total which are also used by instruments in src/devops_cli/telemetry/instruments.py. [sources: src/devops_cli/ai/spend/prometheus.py:76-109, src/devops_cli/telemetry/instruments.py]
  • What determines whether a call is local or hosted?: The call's served_by field should be checked first, then the host if served_by is not present. [sources: src/devops_cli/ai/spend/pricing.py:512, src/devops_cli/ai/spend/pricing.py:516-517]

Fit

Needs splitting into 5 items:

  1. Fix spend series emission to include zero-cost requests
  2. Update dashboard targets with fallbacks for no-data cases
  3. Improve local/host classification logic based on served_by field
  4. Ensure local overrides don't count as cloud spend
  5. Rename or remove AISpendCollector to prevent OTLP name conflicts

Suspected block

None.

Dependencies

None.

Activity

  1. added
    source/agent
    type/bug
    on Oct 18, 2026
  2. dan-petty commented on Oct 18, 2026

    @dan-petty
    OwnerAuthor

    Intake placed this item to the backlog: at intake only a critical fix goes into a release; a release start pulls Ready items in, P0 features first.

    • Type: type/bug. The candidate describes multiple bugs in AI spend tracking and metrics exposition.
    • Priority: P1-High. The issues affect core metrics and reporting, with incorrect data presentation and potential loss of telemetry.
    • Value: High. Fixes critical data accuracy in AI spend tracking that impacts dashboards and cost reporting.
    • Effort: Medium. Requires changes across multiple modules including telemetry, pricing, ledger, and dashboard targets.
  3. dan-petty commented on Oct 18, 2026

    @dan-petty
    OwnerAuthor

    Proposal needs splitting into 5 items at 25e2cda (see proposed design in issue body).

  4. added a commit that references this issue on Oct 24, 2026
    02540c0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    source/agenttype/bug

    Type

    No type

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions