Skip to content

fix(ai): an agent run on a hosted group is not local spend #3665

Closed

Description

@dan-petty

Problem Description

The spend record of an agent run on a hosted gateway group is a local call at $0 with the pricing source local. #3656 fixed this for a call that a gateway reply names (x-litellm-model-name). An agent run records no reply header, so that fix does not reach it.

devops_cli.roadmap.agent_run.record_run_spend writes one record for the replies of a run. It passes provider gateway, the gateway host as server, the summed tokens, and neither served_by nor served_model. track_request_spend then calls is_local with a gateway provider, a local gateway host and no reply names, and is_local decides from the host: local. The bridge builds the model (pydantic_ai_bridge._resolve_remote_inferred_model) with a client from create_pydantic_ai_provider, whose only hook names the caller. No hook reads the reply headers.

Local ledger, 2026-11-05, rows of the groups claude-sonnet-5-5, claude-haiku-5-5 and deepseek-v4.1-flash-1017 (and the eval aliases sonnet-native and haiku-native):

request type served model pricing source rows prompt tokens
chat_dispatch set local 354 10.53 M
agent_run none local 301 20.18 M
chat_dispatch none local 17 0.02 M

The first row is the case of #3656. The second row is this issue: two thirds of the hosted tokens.

Reproduction Steps

  1. Run an agent on a hosted group through the gateway, for example devops roadmap eval stage --model claude-sonnet-5-5.
  2. Read the ledger: SELECT request_type, served_model IS NOT NULL, pricing_source, COUNT(*), SUM(prompt_tokens) FROM ai_spend_records WHERE model IN ('claude-sonnet-5-5','claude-haiku-5-5','deepseek-v4.1-flash-1017') GROUP BY 1,2,3.

Expected vs Actual Behavior

Expected: the record of an agent run on a hosted group is hosted, as the record of a chat call is. A Claude run has the source genai_prices:anthropic and a cost above $0. A DeepSeek run has the source unknown and the warning of #3656 until a person sets a price.

Actual: all 301 agent_run rows have the source local and a cost of $0, and their tokens count as local savings.

Environment Diagnostics

  • DevOps CLI Version: 0.62.6 (main)
  • Python Version: 3.14.10
  • OS & Architecture: Linux 6.18 WSL2 x86_64
  • Execution Environment: DevContainer

Logs, Traces, or Terminal Output

None. The evidence is the table above.

Proposed design

Problem statement

Agent runs on hosted gateway groups record local spend at $0 with pricing source local. #3656 fixed chat calls that carry the gateway reply header, but an agent run records no served model. This change captures the gateway reply model and passes it to the spend ledger. Claude runs then get a hosted price, and DeepSeek runs warn as unknown.

Acceptance criteria

Requirement: Capture the gateway reply model

The agent run spend path SHALL read the served model from the gateway reply header x-litellm-model-name.

Scenario: Reply header present

  • WHEN An agent run receives a gateway reply with x-litellm-model-name: claude-sonnet-5-5.
  • THEN The spend record has served_model set to claude-sonnet-5-5.

Requirement: Price a hosted Claude agent run

A spend record for an agent run on a hosted gateway group SHALL use the hosted price and not the local price.

Scenario: Claude group

  • WHEN The run records served_model claude-sonnet-5-5 with provider gateway and a local gateway host.
  • THEN The record has pricing source genai_prices:anthropic and a cost above $0.

Requirement: Warn on an unknown hosted model

A spend record for an agent run whose served model has no price SHALL use pricing source unknown and emit the existing warning.

Scenario: DeepSeek group

  • WHEN The run records served_model deepseek-v4.1-flash-1017 with no price entry.
  • THEN The record has pricing source unknown and the warning names the model.

Requirement: Fall back to the requested model

The agent run spend path SHALL use the requested model as served_model when the gateway reply header is absent.

Scenario: Reply header absent

  • WHEN An agent run on the hosted group claude-sonnet-5-5 receives no x-litellm-model-name header.
  • THEN The spend record has served_model claude-sonnet-5-5 and is not local.

Requirement: Keep the local-host rule

The change SHALL NOT alter the is_local decision for a gateway reply that names no model.

Scenario: No reply name

  • WHEN A gateway call has a local host and no served_model.
  • THEN The is_local call still returns true.

Existing Tool Considered

No alternatives check has run. The open, maintained httpx library covers reading the gateway reply header through its response event hook. It lacks the link from a response to the agent run and the spend pricing. The change adds that link in create_pydantic_ai_provider and agent_run. The added code costs about 8,000 tokens and 4 hours to build, keep and fix.

Key questions

  • Which value identifies the served model of an agent run on the gateway?: Use x-litellm-model-name from the gateway reply as served_model. (best practice) [sources: src/devops_cli/roadmap/agent_run.py, src/devops_cli/ai/spend/pricing.py, tests/ai/client/test_ai_served_by.py]
    • Chosen: Gateway reply header. It names the model that served the request, even when the gateway routes to another model.
    • Rejected: Requested model only. The gateway can route to a different model, so the requested group can hide the served model.
    • Rejected: No served model. The local gateway host then decides local spend, which is the defect.
  • How should the provider expose the gateway reply model to the agent run?: Add an optional response callback to create_pydantic_ai_provider. The callback receives each httpx.Response, and agent_run keeps the first x-litellm-model-name. (best practice) [sources: src/devops_cli/ai/providers/init.py, src/devops_cli/ai/pydantic_ai_bridge.py, src/devops_cli/roadmap/agent_run.py]
    • Chosen: Optional response callback. It scopes the header to one run and keeps roadmap code out of the provider.
    • Rejected: Module global. Concurrent runs would overwrite each other's model.
    • Rejected: Parse pydantic-ai stream events. The events carry messages, not the HTTP reply headers.
  • How should record_run_spend pass the served model to the spend ledger?: Add an optional served_model parameter to record_run_spend and pass it to track_request_spend. (best practice) [sources: src/devops_cli/roadmap/agent_run.py, src/devops_cli/ai/spend/ledger.py, src/devops_cli/ai/spend/models.py]
    • Chosen: Explicit parameter. It keeps the data flow visible and easy to test offline.
    • Rejected: Module global. It mixes concurrent runs and is hard to test.
    • Rejected: Read the header in the ledger. The ledger has no access to the HTTP response.
  • What should happen when the gateway reply names a model with no price?: Keep the existing unknown-price path. Set served_model, use pricing source unknown, and emit the warning of fix(ai): a hosted gateway call is recorded as a local call at $0 #3656. (best practice) [sources: src/devops_cli/ai/spend/pricing.py, src/devops_cli/ai/spend/ledger.py, tests/ai/spend/test_ai_spend_pricing.py]
  • What should the agent run record when the gateway reply has no header?: Use the requested model as served_model so a hosted group is not recorded as local. (best practice) [sources: src/devops_cli/roadmap/agent_run.py, src/devops_cli/ai/spend/pricing.py]
    • Chosen: Requested model fallback. It keeps a hosted group out of local spend when the header is missing.
    • Rejected: Local fallback. It records hosted tokens as local savings, which is the defect.
    • Rejected: Drop the record. It loses the token count and the cost.
  • Should the fix change is_local or pass the served model?: Pass the served model. Do not change is_local. (best practice) [sources: src/devops_cli/ai/spend/pricing.py, tests/ai/spend/test_ai_spend_pricing.py, src/devops_cli/roadmap/agent_run.py]
    • Chosen: Pass the served model. The existing local-host rule still applies when the reply names no model.
    • Rejected: Treat every gateway provider as hosted. A local gateway model would stop counting as local spend.
    • Rejected: Change the gateway host. The host is the real endpoint and other records use it.

Fit

Fits in one pull request.

Suspected block

None.

Dependencies

None.

Alternatives check of 2026-11-07: partly.
Tool: encode/httpx (https://github.com/encode/httpx), BSD-3-Clause, last activity 2026-10-02, maintained.

Activity

  1. added
    source/agent
    type/bug
    scope/ai
    status/new
    value/medium
    effort/medium
    on Nov 5, 2026
  2. dan-petty commented on Nov 5, 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. Two thirds of hosted tokens are misrecorded as $0 local, but no functionality is blocked and no data is lost.
    • Value: Medium. Correct cost attribution for a large share of hosted spend, but the system still functions without it.
    • Effort: Medium. Requires a new identification path for agent runs since reply headers are absent, touching record_run_spend and is_local logic.
    • Related: fix(ai): a hosted gateway call is recorded as a local call at $0 #3656, which asks for a different change. Both fix hosted gateway calls misclassified as local spend, but #3656 covers chat_dispatch via x-litellm-model-name headers while this covers agent_run which records no reply header.
  3. added
    priority/p2-medium
    on Nov 5, 2026
  4. added
    status/ready
    and removed
    status/new
    on Nov 6, 2026
  5. devops-cli-service commented on Nov 6, 2026

    @devops-cli-service
    Contributor

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

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

    @devops-cli-service
    Contributor

    Alternatives check: partly

    Reason: Web searches did not answer (no provider responded), so the check rests on the issue's own alternatives section plus the facts read for httpx: httpx is an open, unarchived library whose response event hooks let a caller read the gateway reply header x-litellm-model-name. That is only one requirement of the issue; httpx does not carry the captured model from a response into record_run_spend/track_request_spend, nor decide the pricing source, so the rest of the fix stays project code.

    Tool: encode/httpx (https://github.com/encode/httpx)

    • License: BSD-3-Clause
    • Last activity: 2026-10-02
    • Maintained: yes

    Covers:

    • Reading HTTP response headers through response event hooks on a client
    • Exposing the gateway reply header x-litellm-model-name to the calling code

    Lacks:

    • A link from one response to the agent run that made the request
    • Passing the served model into record_run_spend / track_request_spend
    • Choosing the pricing source (genai_prices:anthropic / unknown) or emitting the unknown-price warning
    • Keeping the existing is_local local-host rule intact

    Tools considered:

    Searches run:

    • python library capture HTTP response headers per request callback httpx event_hooks, results: 0
    • httpx response event hook read response headers, results: 0
    • httpx event_hooks response headers, results: 0
    • httpx python client, results: 0
    • httpx library response hooks documentation, results: 0
    • litellm x-litellm-model-name header served model, results: 0
    • httpx event hooks, results: 0

    Sources:

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

  7. dan-petty commented on Nov 9, 2026

    @dan-petty
    OwnerAuthor

    New evidence for this item. It comes from the three spend sources for claude-sonnet-5-5 on 2026-11-09. Nothing here is a guess about the Anthropic invoice.

    Check of the premise "the gateway price is wrong". It is not wrong.

    • k8s/llm/gateway/config.yaml prices the group at 2.0 USD input, 10 USD output, 2.5 USD cache write and 0.10 USD cache read for each million tokens. The Anthropic pricing page lists the same four prices for Sonnet 5.5 (cache hits at 0.05 times the input price). genai-prices 0.1.13 gives the same prices.
    • The gateway spend log holds 405 Sonnet requests for the day (2026-11-09, 02:46 to 12:23 UTC), 26.92 M prompt tokens, 0.43 M completion tokens, and 58.13 USD. Every row has cache_read_input_tokens 0 and cache_creation_input_tokens 0. Each row cost equals prompt tokens times 2 USD plus completion tokens times 10 USD (the two exceptions are 400 error rows at 0 USD). So no cache token is priced as a full input token today. feat(ai): cache Claude prompts and price the cache tokens #3658 still holds for the day a cache starts.
    • The Anthropic figure of the owner (5.59 USD) equals the running gateway total at 09:44:48 UTC (5.592 USD, 37 requests). The burst of the stage eval began at 10:00 UTC and added 52.5 USD. So the figure agrees with the gateway for the time it was read. It does not show a price error. I could not read the Anthropic console, so the time of the reading is not verified.
    • One row is a 499 ClientDisconnected with 0 completion tokens, and the gateway still priced 86,965 prompt tokens (0.17 USD). Anthropic may not bill it. The size is small.

    What is wrong, and this item covers it. In the last 24 hours the counter devops_cli_ai_tokens_total{model="claude-sonnet-5-5"} holds:

    • 24.48 M prompt tokens with local="false". They fit the gateway (26.92 M). The matching devops_cli_ai_spend_usd_total is 52.87 USD, against 58.13 USD at the gateway.
    • 68.03 M prompt tokens and 1.47 M completion tokens with local="true", all from job="roadmap-service" on the cluster gateway host. They count as local savings and add 0 USD to spend. The gateway shows only 26.92 M Sonnet prompt tokens, so these tokens are not in its Sonnet log.

    On main today record_run_spend passes served_model only when a reply names a model. When no reply names one, is_local decides from the cluster host and returns local. I did not find which replies carry no model name, and I did not check whether the 68 M tokens are counted twice. Both checks need the Service logs.

    Also, the counter shows 0 USD for deepseek-v4.1-flash-1017 while the gateway shows 14.04 USD for the day. That is the unknown price in this item.

    Window difference. GET /global/spend/models returned the same totals for three different start_date values, so it does not filter by date. Use GET /spend/logs?...&summarize=false for a day. A "last 24 hours" Prometheus value (increase([24h])) is not a calendar day.

    Acceptance criteria to add: a test where a reply with no model name from a hosted group is not local, and a test that compares the sum of the local="false" and local="true" prompt tokens of a run with the usage that the model reported.

  8. added
    owner/agent
    status/in-progress
    status/in-review
    and removed
    status/ready
    status/in-progress
    on Nov 9, 2026
  9. removed
    status/in-review
    owner/agent
    on Nov 9, 2026
  10. devops-cli-service commented on Nov 9, 2026

    @devops-cli-service
    Contributor
  11. devops-cli-service commented on Nov 9, 2026

    @devops-cli-service
    Contributor

    Live since 2026-11-09 15:38 UTC

  12. dan-petty commented on Nov 11, 2026

    @dan-petty
    OwnerAuthor

    Live check on 2026-11-11: the fix is in production.

    This query must stay empty. It finds hosted model tokens that have the label local="true":

    sum by (job, model) (increase(devops_cli_ai_tokens_total{local="true",model=~"deepseek.*|claude.*|gemini.*"}[1d]))
    

    The same check found the reverse defect: an agent run on a local gateway group, such as devops-coder, records local="false", because is_local makes every gateway call with a reply model name hosted. I filed it as #4523.

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/medium

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions