Skip to content

fix(monitoring): read AI spend from the gateway counter #4724

Closed
1 of 1 issue completed
Closed

Description

@dan-petty

Problem or Motivation

ai-spend.json. Panels 2 and 16 to 18 read devops_cli_ai_spend_usd_total, which is 41 % of the gateway spend: 24 h $1.48 against litellm_spend_metric_total $3.59 for the same caller; 7 d $77.33 against $118.57. Panel 7 uses rate[5m] on a sparse OTLP counter, and at 7 d its points add up to $33.30 of $77.33 (43 %); hourly buckets of the gateway counter add up to $118.76 against $118.57. Panels 10, 11 and 14 use rate[5m] and show 37 % of the requests and 42 % of the tokens at 7 d (33 % at 24 h); rate[$__rate_interval] still shows 81 %. Panel 8 draws 6 to 10 lines, most at 0. Panel 20 (Ollama slot wait by priority) has no series in 30 d (src/devops_cli/ai/client/network.py:481 sends only an outcome label). The projections 16 to 18 use rate([$__range]) with a default of 6 h: $104.86 at 6 h, $109.67 at 24 h, $508.20 at 7 d. Panels 15, 19, 22 and 23 draw rolling [1h] sums at steps under 1 h. Panel 24 shows 98 % request coverage and hides the 41 % spend coverage.

Proposed Solution & Architectural Design

Spend panels read the gateway counter for gateway calls plus devops_cli_ai_spend_usd_total{provider!='gateway'} for direct provider calls. Spend, requests, tokens and savings per hour: bars, panel interval 1h, increase([$__interval]), titles 'per hour'. Projections: a fixed 7 d baseline, increase([7d]) / 7 x N days, with the baseline in the title, so they do not change with the range picker. Panel 20: the gateway slot wait by outcome (p95 is about 19 microseconds), or remove it. Panel 24 becomes a spend coverage stat (devops-cli gateway spend divided by gateway spend, percentunit, threshold 0.95); it shows 41 % until #4734 is live. The 8 local-savings queries keep their old-label fallback until #2202 (after 2026-11-23); this item changes only their windows, and the recorded test of 'Local Cloud Savings in Range' must stay green. No query uses client_ip or user_agent, which #4545 drops.

Scope: queries, panel types, units, legends and defaults only. Keep the default Grafana colors. The project design study owns colors and type.

Target Subsystem

AI & Agentic Review Pipeline (AI spend ledger and its telemetry).

Existing Tool Considered

The LiteLLM gateway counter litellm_spend_metric_total and built-in Grafana panel types. No new code.

Acceptance Criteria

  • Harness cases (tests/fixtures/dashboard_queries/ai-spend.yaml): spend in range equals the gateway fixture sum plus the direct fixture sum; the hourly bars add up to the fixture total; the projection gives the same value for $__range 6h and 7d.
  • tests/prometheus/test_k8s_monitoring_dashboards.py passes, the recorded savings test included.
  • Live at 7 d: the spend bars add up to within 1 % of sum(increase(litellm_spend_metric_total[7d])) plus the direct spend; the request bars add up to at least 95 % of sum(increase(devops_cli_ai_requests_total[7d])) (today 37 %).

Files

  • k8s/monitoring/dashboards/ai-spend.json
  • tests/fixtures/dashboard_queries/ai-spend.yaml
  • tests/prometheus/test_k8s_monitoring_dashboards.py

Dependencies

#4680.

Related

#2202 (old label fallbacks), #4545 (drops per-pod labels), #4578 (gateway spend by repository).

Source: #4675

Owner decision 2026-11-13

The approved pipeline split (tracker #5482) uses the spend coverage stat of this item as a Phase 0 exit check: for 7 days, the devops-cli spend counter agrees with the gateway spend within 10 %. #3665 is closed, and the ratio was 1.005 for the 24 h to 2026-11-12.

  • Live proof: after the merge, a comment gives the 7-day value of the coverage stat.

Alternatives check of 2026-11-14: in-use.
The repository already uses this tool.
Tool: BerriAI/litellm (https://github.com/BerriAI/litellm), NOASSERTION, last activity 2026-11-14, maintained.

Proposed design

Problem statement

The AI spend dashboard understates spend and requests. The spend panels read devops_cli_ai_spend_usd_total, which holds 41 % of the gateway spend. At 24 h it shows $1.48 against $3.59; at 7 d, $77.33 against $118.57. Sparse OTLP samples make rate([5m]) show 37 % of the requests at 7 d. The projection panels follow the range picker, panel 20 has no series, and panel 24 hides the spend coverage. The change reads litellm_spend_metric_total plus the direct-provider counter. It draws hourly bars, fixes the 7 d projection baseline, and makes panel 24 the spend coverage stat.

Acceptance criteria

Requirement: Gateway spend source

The spend panels SHALL read litellm_spend_metric_total for gateway calls and devops_cli_ai_spend_usd_total{provider!="gateway"} for direct provider calls.

Scenario: Spend sums both sources

  • WHEN The query harness runs the spend panels over the ai-spend fixture.
  • THEN The spend in range equals the gateway fixture sum plus the direct fixture sum.

Requirement: Hourly bars

The spend, request and token panels SHALL use increase([$__interval]) with a panel interval of 1h and bars.

Scenario: Hourly bars add up

  • WHEN The harness runs the hourly bars over the fixture.
  • THEN The bars add up to the fixture total.

Requirement: Panel titles

Each changed panel SHALL have a title of the form <signal>: per hour and a non-empty description.

Scenario: Titles name the signal and the measure

  • WHEN The ai-spend dashboard JSON is read.
  • THEN Each changed panel title contains ': ' and the panel has a description.

Requirement: Projection baseline

The projection panels SHALL use increase([7d]) / 7 * <N> and SHALL name the 7 d baseline in the title.

Scenario: Projection does not follow the range picker

  • WHEN The harness runs the projection query with a range of 6 h and of 7 d.
  • THEN Both runs give the same value.

Requirement: Slot wait by outcome

Panel 20 SHALL group the gateway slot wait by outcome and SHALL NOT group by priority.

Scenario: Slot wait has one line for each outcome

  • WHEN The harness runs panel 20 over the fixture.
  • THEN The query groups by outcome and returns no priority group.

Requirement: Spend coverage stat

Panel 24 SHALL be a stat that divides devops_cli_ai_spend_usd_total{provider="gateway"} by litellm_spend_metric_total, with unit percentunit and threshold 0.95.

Scenario: Panel 24 reads both spend counters

  • WHEN The ai-spend dashboard JSON is read.
  • THEN Panel 24 has type stat, unit percentunit, both spend counters in its query, and the dashboard test asserts these counters.

Requirement: No dropped labels

No query of the ai-spend dashboard SHALL use the client_ip or user_agent label.

Scenario: Queries use no dropped label

  • WHEN Each query expression of the dashboard is read.
  • THEN No expression contains client_ip or user_agent.

Requirement: No zero fallback in a time series

A changed time-series panel SHALL NOT use or vector(0).

Scenario: Panel 8 draws no empty line

  • WHEN The panel 8 query is read.
  • THEN The query contains no or vector(0).

Requirement: Dashboard tests pass

The dashboard tests SHALL pass, and the recorded savings query SHALL stay unchanged.

Scenario: The dashboard tests run

  • WHEN tests/prometheus/test_k8s_monitoring_dashboards.py runs.
  • THEN Each test passes and the recorded savings query equals the panel query.

Existing Tool Considered

BerriAI/litellm (https://github.com/BerriAI/litellm, NOASSERTION, last activity 2026-11-14, maintained) is in use. The project already scrapes its litellm_spend_metric_total counter, and the item makes it the primary spend source. It lacks the request and token counters that panels 10, 11 and 14 read (devops_cli_ai_requests_total and devops_cli_ai_tokens_total). It has no slot-wait-by-priority series for panel 20. It supplies no projection or coverage logic; those stay in the Grafana queries. It cannot supply the direct-provider spend, which stays in devops_cli_ai_spend_usd_total{provider!="gateway"}. The change builds no new code.

Key questions

  • Which file holds the ai-spend dashboard?: k8s/devops/dashboards/ai-spend.json. The environment folder k8s/monitoring/dashboards/ does not hold it. The test reads the product folder through PRODUCT_DASHBOARDS_DIR (refactor(k8s): split the product dashboards and rules in place #5292). [sources: tests/prometheus/test_k8s_monitoring_dashboards.py]
  • Which series do the spend panels read?: litellm_spend_metric_total for gateway calls, plus devops_cli_ai_spend_usd_total{provider!="gateway"} for direct provider calls. No panel sums both counters for a gateway call. (best practice) [sources: tests/fixtures/metrics/litellm.yaml:104, openspec/changes/archive/2026-11-09-4222-alert-on-the-daily-claude-spend/design.md:14]
    • Rejected: Keep devops_cli_ai_spend_usd_total only. It holds 41 % of the gateway spend: $77.33 of $118.57 at 7 d.
    • Chosen: Gateway counter plus the direct-provider counter. It covers the gateway spend, and the selector keeps the calls that bypass the gateway.
    • Rejected: Sum both counters for every call. It counts a gateway call twice.
  • How do the spend, request and token panels aggregate a sparse OTLP counter?: Draw bars with a panel interval of 1h and increase([$__interval]), so each bar follows the step. (best practice) [sources: tests/prometheus/test_k8s_monitoring_dashboards.py]
    • Rejected: rate([5m]). Sparse samples undercount: 37 % of the requests at 7 d.
    • Rejected: rate([$__rate_interval]). It still shows 81 %.
    • Chosen: increase([$__interval]) with bars. It follows the step, and the project uses it for the call count.
  • How do the projection panels keep one value for each range?: Use a fixed 7 d baseline: increase([7d]) / 7 * <N>, with the baseline in the title. (best practice) [sources: tests/prometheus/test_k8s_monitoring_dashboards.py]
    • Rejected: rate([$__range]). The value follows the range picker: $104.86 at 6 h and $508.20 at 7 d.
    • Chosen: Fixed 7 d baseline. It gives one value for each range and names the baseline.
  • What does panel 20 show?: The gateway slot wait p95 by outcome, the only label the client sends. The panel keeps its p95 and unit s. (best practice) [sources: src/devops_cli/ai/client/network.py]
    • Rejected: Add a priority label in the client. It changes client code, and the scope is queries and panel settings only.
    • Chosen: Group by outcome. It uses the label that the client sends and gives series in the range.
    • Rejected: Remove the panel. It loses the slot-wait signal.
  • What does panel 24 show?: A spend coverage stat: devops_cli_ai_spend_usd_total{provider="gateway"} divided by litellm_spend_metric_total, unit percentunit, threshold 0.95. It shows 41 % until fix(ai): record the gateway cost of each gateway call #4734 is live. (best practice) [sources: tests/prometheus/test_k8s_monitoring_dashboards.py, k8s/devops/dashboards/ai-spend.json]
    • Rejected: Keep the request coverage stat. It hides the 41 % spend coverage.
    • Chosen: Replace it with the spend coverage stat. The owner decision of 2026-11-13 uses this stat as the Phase 0 exit check.
    • Rejected: Show both stats. The body names one stat, and the spend coverage is the one that the exit check reads.
  • Does the coverage stat filter the gateway counter by user_agent?: No. No query uses client_ip or user_agent, which fix(monitoring): drop per-pod labels from gateway spend series #4545 drops. The 7 d measurement shows that other callers spent $0.0012, so the filter changes almost nothing. (best practice) [sources: tests/fixtures/metrics/litellm.yaml:104]
    • Rejected: Filter with user_agent=~"devops-cli/.*". The body forbids the label, and the filter changes the 7 d total by $0.0012.
    • Chosen: Use no filter. The two totals compare the same spend, and no dropped label is used.
  • Do the local-savings query windows change in this item?: No. The 8 savings queries keep their selectors and windows, and the window change goes to a new issue. The recorded test pins the query text, and a person must re-record it against live Prometheus. (best practice) [sources: tests/prometheus/test_k8s_monitoring_dashboards.py, tests/fixtures/recorded_queries/ai_spend_local_savings.yaml]
    • Rejected: Change the windows and re-record the fixture. It needs live Prometheus, which this roadmap cannot read.
    • Chosen: Keep the windows and defer the change. The recorded test stays green, and the deferred issue re-records the query.
  • How does panel 8 lose its zero lines?: Remove the or vector(0) fallback from the time series, so it draws only the series that have samples. (best practice) [sources: tests/prometheus/test_k8s_monitoring_dashboards.py]
    • Rejected: Keep the fallback. It draws 6 to 10 lines, and most are 0.
    • Chosen: Remove the fallback. A time series draws no line for a series with no samples.
  • Does the spend coverage stat serve as the Phase 0 exit check of the pipeline split (feat(roadmap): track the roadmap pipeline split into jobs #5482)?: Yes. The owner decision of 2026-11-13 makes the 7 d agreement of the two spend counters within 10 % the Phase 0 exit check. The 7 d measurement of 2026-11-13 gives $81.76 against $119.35, a 31.5 % gap, so the check fails until fix(ai): record the gateway cost of each gateway call #4734 is live. The 24 h ratio was 1.005 to 2026-11-12. After the merge, a comment gives the 7 d value. (best practice) [sources: k8s/devops/dashboards/ai-spend.json]
    • Chosen: Use the spend coverage stat as the exit check. The owner decision of 2026-11-13 records it.
    • Rejected: Use the request coverage stat. It hides the spend gap.

Fit

Fits in one pull request.

Suspected block

None.

Dependencies

#4680

Sub-issues

Sub-issues

1 of 1 Issues completed
Loading 1 sub-issues

Activity

  1. added
    source/agent
    on Nov 11, 2026
  2. added
    type/bug
    scope/monitoring
    status/new
    value/medium
    effort/medium
    on Nov 11, 2026
  3. dan-petty commented on Nov 11, 2026

    @dan-petty
    OwnerAuthor

    Intake placed this item to the backlog: intake puts every new item in the backlog. A person plans the milestones.

    • Type: type/bug. The fix prefix of the title gives the type.
    • Scope: scope/monitoring. The (monitoring) scope of the title gives the scope.
    • Priority: P2-Medium. Spend panels show 41% of actual cost, which misleads budget decisions but does not break production.
    • Value: Medium. Accurate spend reporting is important for cost tracking, but the dashboard is an internal tool.
    • Effort: Medium. Fifteen or more panels need query, interval, type and unit changes, plus fixture and test updates.
    • Related: feat(monitoring): the ai-spend dashboard counts gateway traffic from the gateway's own series, shows failed attempts and names each caller #1668, which asks for a different change. Both target the ai-spend dashboard and gateway metrics; #1668 added gateway series for traffic and callers, this item fixes the spend panels to read the gateway counter as the primary source.
  4. added
    priority/p2-medium
    on Nov 11, 2026
  5. dan-petty commented on Nov 13, 2026

    @dan-petty
    OwnerAuthor

    Measured on 2026-11-13 for the agentic workflow spec (#5443, gap G5, rule O3). Instant queries at 2026-11-13T16:00Z, 7-day window:

    • sum(increase(devops_cli_ai_spend_usd_total[7d])): 81.76 USD.
    • sum(increase(litellm_spend_metric_total[7d])): 119.35 USD.
    • sum(increase(litellm_spend_metric_total{user_agent=~"devops-cli/.*"}[7d])): 119.35 USD. Other clients spent 0.0012 USD.

    So the gap of 37.59 USD is devops-cli calls whose counter did not reach Prometheus. It is not spend of other callers. The cause is not confirmed: local runs that export no counter and models with no price in the counter are two places to look.

    The spec asks for a check that compares the two series with the user_agent selector above, and treats a difference over 10 % in 7 days as a defect of the measure. This issue covers it, because the spend panels then read the gateway counter.

  6. devops-cli-service commented on Nov 14, 2026

    @devops-cli-service
    Contributor

    Alternatives check: in-use

    Reason: The item is an in-repo rewrite of the ai-spend dashboard queries, and the only tool it relies on is the LiteLLM gateway's Prometheus counter litellm_spend_metric_total, which the project already scrapes and which the item proposes as the primary spend source; that counter covers the spend half of the need, but the request, token, slot-wait and projection panels come from the project's own counters and Grafana query functions, which no external tool supplies.

    The repository already uses this tool.

    Tool: BerriAI/litellm (https://github.com/BerriAI/litellm)

    • License: NOASSERTION
    • Last activity: 2026-11-14
    • Maintained: yes

    Covers:

    • Supplies the gateway spend counter litellm_spend_metric_total that the spend panels (2, 7, 16-18) are to read as their primary source
    • Carries a user_agent label, so the counter can be limited to devops-cli callers when comparing against the local counter
    • Its hourly buckets add up to the gateway total, so increase() over fixed windows gives per-hour spend bars that match the fixture sum

    Lacks:

    • Does not cover the request and token panels (10, 11, 14), which need the project's own devops_cli_ai_requests_total
    • No slot-wait-by-priority series for panel 20, which stays empty because the client sends only an outcome label
    • No projection or coverage-stat logic; the fixed 7 d baseline and the coverage stat stay in the Grafana queries and the project's own counters
    • Direct-provider calls still need devops_cli_ai_spend_usd_total{provider!='gateway'}, which the gateway counter cannot supply

    Tools considered:

    Searches run:

    • litellm prometheus metric litellm_spend_metric_total spend tracking, results: 5
    • Grafana Prometheus increase($__interval) vs rate sparse OTLP counter dashboard best practice, results: 5
    • LiteLLM prometheus metrics spend total docs, results: 5
    • litellm github repository prometheus spend metric, results: 5
    • litellm, results: 5
    • docs.litellm.ai prometheus metrics /metrics endpoint, results: 5

    Sources:

    Checked on 2026-11-14. A person decides what this means for the item. The check changes no label and closes no issue.

  7. added
    status/ready
    and removed
    status/new
    on Nov 15, 2026
  8. devops-cli-service commented on Nov 15, 2026

    @devops-cli-service
    Contributor

    Status set to Ready at a8a99cb (see proposed design in issue body).

    Moved to new items: #5784.

  9. dan-petty commented on Nov 16, 2026

    @dan-petty
    OwnerAuthor

    New evidence, 2026-11-16 01:35Z, range 6 h. The owner reports that AI Spend Rate Rolling Avg (1h) has spikes that the AI Spend Rate ($/hr) line does not show.

    • litellm_spend_metric_total gives $1.26 for DeepSeek. devops_cli_ai_spend_usd_total gives $0.68 (54%): roadmap-implement $0.35, roadmap-service $0.22, roadmap-stage $0.10.
    • The series of the Job pods are sparse: about 6 samples per series in 6 h (roadmap-stage 6.5, roadmap-implement 6), against 73 for roadmap-service. rate(...[5m]) gives no value for them in the whole range, so the $/hr line shows only roadmap-service. rate(...[1h]) sometimes sees two samples and extrapolates: it peaks at $0.15/h for roadmap-stage and $0.12/h for roadmap-implement. Those are the spikes.
    • All Job pods write to one series: instance="roadmap-stage" or instance="devops-cli-job". resets() over 6 h counts 9 resets on roadmap-stage and 4 on roadmap-implement for the token counter.

    Reading the spend from the gateway counter, as this issue proposes, removes all three effects. The savings panels have the same defects and also a wrong price; that is #5914.

  10. added
    owner/agent
    on Nov 16, 2026
  11. added
    status/in-progress
    status/in-review
    and removed
    status/ready
    status/in-progress
    on Nov 16, 2026
  12. removed
    status/in-review
    owner/agent
    on Nov 16, 2026
  13. devops-cli-service commented on Nov 16, 2026

    @devops-cli-service
    Contributor

    Live since 2026-11-16 03:16 UTC

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

    effort/mediumpriority/p2-mediumscope/monitoringsource/agenttype/bugvalue/medium

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions