Repository navigation
Description
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
- Run an agent on a hosted group through the gateway, for example
devops roadmap eval stage --model claude-sonnet-5-5. - 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_modelset toclaude-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_modelclaude-sonnet-5-5with providergatewayand a local gateway host. - THEN The record has pricing source
genai_prices:anthropicand 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_modeldeepseek-v4.1-flash-1017with no price entry. - THEN The record has pricing source
unknownand 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-5receives nox-litellm-model-nameheader. - THEN The spend record has
served_modelclaude-sonnet-5-5and 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_localcall 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-namefrom the gateway reply asserved_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 eachhttpx.Response, andagent_runkeeps the firstx-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_spendpass the served model to the spend ledger?: Add an optionalserved_modelparameter torecord_run_spendand pass it totrack_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 sourceunknown, 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]- Chosen: Existing unknown path. It matches the behavior that fix(ai): a hosted gateway call is recorded as a local call at $0 #3656 set for chat dispatch.
- Rejected: Local fallback. It hides hosted spend and repeats the defect.
- Rejected: Hard-coded DeepSeek price. The issue says a person sets the price, and the repository holds no such price.
- What should the agent run record when the gateway reply has no header?: Use the requested model as
served_modelso 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_localor pass the served model?: Pass the served model. Do not changeis_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
dan-petty commentedon Nov 5, 2026
Intake placed this item to the backlog: intake puts every new item in the backlog. A person plans the milestones.
fixprefix of the title gives the type.(ai)scope of the title gives the scope.Two thirds of hosted tokens are misrecorded as $0 local, but no functionality is blocked and no data is lost.Correct cost attribution for a large share of hosted spend, but the system still functions without it.Requires a new identification path for agent runs since reply headers are absent, touching record_run_spend and is_local logic.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.devops-cli-service commentedon Nov 6, 2026
Status set to Ready at 662828d (see proposed design in issue body).
devops-cli-service commentedon Nov 7, 2026
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 intorecord_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)
Covers:
x-litellm-model-nameto the calling codeLacks:
record_run_spend/track_request_spendgenai_prices:anthropic/unknown) or emitting the unknown-price warningis_locallocal-host rule intactTools considered:
Searches run:
Sources:
Checked on 2026-11-07. A person decides what this means for the item. The check changes no label and closes no issue.
dan-petty commentedon Nov 9, 2026
New evidence for this item. It comes from the three spend sources for
claude-sonnet-5-5on 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.yamlprices 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.cache_read_input_tokens0 andcache_creation_input_tokens0. 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.ClientDisconnectedwith 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:local="false". They fit the gateway (26.92 M). The matchingdevops_cli_ai_spend_usd_totalis 52.87 USD, against 58.13 USD at the gateway.local="true", all fromjob="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_spendpassesserved_modelonly when a reply names a model. When no reply names one,is_localdecides 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-1017while the gateway shows 14.04 USD for the day. That is theunknownprice in this item.Window difference.
GET /global/spend/modelsreturned the same totals for three differentstart_datevalues, so it does not filter by date. UseGET /spend/logs?...&summarize=falsefor 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"andlocal="true"prompt tokens of a run with the usage that the model reported.fix(ai): price a hosted group call by the asked model (#4231)
devops-cli-service commentedon Nov 9, 2026
Released in v0.101.0: https://github.com/dan-petty/devops-cli/releases/tag/v0.101.0
devops-cli-service commentedon Nov 9, 2026
Live since 2026-11-09 15:38 UTC
feat(k8s): alert on the daily Claude spend (#4246)
dan-petty commentedon Nov 11, 2026
Live check on 2026-11-11: the fix is in production.
local="true"in a 7-day window. All of them are from before the fix.local="true"sample of a hosted model:deepseek-v4.1-flash-1017at 2026-11-09 02:42 UTC,claude-sonnet-5-5at 2026-11-09 13:26 UTC. PR fix(roadmap): send tool choice auto to Claude and count answers #4216 (merged 09:45 UTC) and PR fix(ai): price a hosted group call by the asked model #4231 (merged 14:32 UTC, release v0.101.0) are onmain.local="false": 541Mdeepseek-v4.1-flash-1017and 7Mclaude-sonnet-5-5. Only Ollama backends havelocal="true".This query must stay empty. It finds hosted model tokens that have the label
local="true":The same check found the reverse defect: an agent run on a local gateway group, such as
devops-coder, recordslocal="false", becauseis_localmakes every gateway call with a reply model name hosted. I filed it as #4523.