Skip to content

fix(ai): record the gateway cost of each gateway call #4734

Closed as not planned

Description

@dan-petty

Problem or Motivation

devops_cli_ai_spend_usd_total is 41 % of the gateway spend for the same caller: 24 h, job roadmap-service $1.48 against litellm_spend_metric_total{end_user='roadmap-service'} $3.59; 7 d, $77.33 against $118.57. The requests (4,198 against 4,187) and the tokens (258.2 M against 252.0 M) agree, so devops-cli prices the same tokens lower. src/devops_cli/ai/spend/ledger.py:1066 emits the cost that devops-cli computes; the gateway prices from k8s/llm/gateway/config.yaml:195-197. The spend report and the Service spend panels are low by the same share.

Proposed Solution & Architectural Design

Check the premise first: confirm that the LiteLLM proxy of the pinned gateway version returns the cost of each call in the x-litellm-response-cost response header (LiteLLM proxy documentation), for chat and embeddings, with and without streaming. If it does, the ledger records that cost for each call through the gateway, and computes a price only for a direct provider call. If the header is absent in a case, take the gateway model price from one shared source (the gateway config) for that case. The price that OpenRouter bills is a separate person check (#4422).

Target Subsystem

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

Existing Tool Considered

The LiteLLM proxy response header x-litellm-response-cost (an open tool's standard output). The ledger reads it; no new price table.

Acceptance Criteria

  • A test with the HTTP fake: a gateway answer with x-litellm-response-cost 0.0123 adds 0.0123 USD to the ledger and to devops_cli_ai_spend_usd_total.
  • A test: a direct provider call still uses the price table.
  • Live, 24 h after the rollout: sum(increase(devops_cli_ai_spend_usd_total{provider='gateway'}[24h])) divided by sum(increase(litellm_spend_metric_total{end_user=~'devops-cli|devops-cli-job|roadmap-service'}[24h])) is between 0.95 and 1.05 (today 0.41).

Files

  • src/devops_cli/ai/spend/ledger.py
  • src/devops_cli/ai/client/network.py
  • tests/ai/spend/test_ai_spend_ledger.py
  • tests/ai/spend/test_ai_spend_prometheus.py

Dependencies

None.

Related

#4296 (cost of the Jev pass-through calls, the same header), #4422 (a person confirms the OpenRouter price).

Source: #4675

Activity

  1. added
    source/agent
    on Nov 11, 2026
  2. added
    type/bug
    scope/ai
    status/new
    value/high
    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/ai. The (ai) scope of the title gives the scope.
    • Priority: P2-Medium. The spend report is wrong by a factor of two, which misleads budgeting, but the system still functions.
    • Value: High. AI spend tracking is the primary cost-visibility tool for the project and its users.
    • Effort: Medium. The change touches two source files and two test files with a clear header-reading approach and a fallback path.
    • Related: feat(ai): record the cost of Jev calls that pass through the gateway #4296, which asks for a different change. Both record the cost of gateway calls, but #4296 targets Jev pass-through calls via usage.cost in the body while the candidate targets all gateway chat and embedding calls via the x-litellm-response-cost header.
  4. added
    priority/p2-medium
    owner/agent
    on Nov 11, 2026
  5. dan-petty commented on Nov 12, 2026

    @dan-petty
    OwnerAuthor

    Closed as not planned. Newer code fixed the gap that this item reports, and the header design does not cover a streamed call. No pull request.

    Premise check (2026-11-12)

    1. The gap is gone. fix(ai): price the DeepSeek group and its cached input #4454 (merged 2026-11-11, first released in v0.111.0 at 05:38Z) gives the group deepseek-v4.1-flash-1017 the price of the gateway config. Before fix(ai): price the DeepSeek group and its cached input #4454, each call of that group recorded 0 USD with the source unknown. The ratio of acceptance criterion 3, sum(increase(devops_cli_ai_spend_usd_total{provider="gateway"}[1h])) / sum(increase(litellm_spend_metric_total{end_user=~"devops-cli|devops-cli-job|roadmap-service"}[1h])):

      • 05:00 to 06:00Z on 2026-11-11: 0.006
      • each hour from 06:00 to 10:00Z: 1.11, 0.99, 0.96, 0.98
      • each 6 h window since 06:50Z: 0.98 to 1.04
      • 24 h to 2026-11-12 12:50Z: 1.005 (3.235 USD against 3.217 USD for roadmap-service)

      The 24 h and 7 d values in this item (0.41) include the hours before the rollout. Criterion 3 (0.95 to 1.05) is true now.

    2. One price source is in place. The gateway config sets the price of each hosted group in model_info, and _HOSTED_GROUP_PRICES in src/devops_cli/ai/spend/pricing.py holds the same rates. test_the_client_price_of_a_hosted_group_is_the_price_in_the_gateway_config in tests/ai/spend/test_hosted_group_prices.py fails when the two differ. This is the fallback that the item asks for.

    3. The header is absent on a streamed reply. In LiteLLM 1.103.10 (the pinned image), litellm/proxy/common_request_processing.py sends the headers of a stream with its first chunk (_stream_response_headers, line 2312: response_cost=hidden_params.get("response_cost") or ""). The cost is not known at that time, and get_custom_headers drops an empty value (exclude_values, lines 1676 and 1781). Only a reply that does not stream gets x-litellm-response-cost. The CLI streams each chat call (comment of sse_keepalive_ping_interval_seconds in k8s/llm/gateway/config.yaml). Thus the header cannot give the cost of each gateway call, as the item asks.

    The header also gives no new fact: the gateway calculates it from the same config price. The price that OpenRouter bills stays the person check of #4422. The cost of the Jev pass-through calls stays #4296. The spend coverage stat that #4724 adds shows about 1.0 now, not 0.41.

  6. removed
    owner/agent
    on Nov 12, 2026
  7. removed
    status/new
    on Nov 12, 2026
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/aisource/agenttype/bugvalue/high

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions