Skip to content

fix(ai): Price each gateway call at the deployment that answered #6002

Done

Description

@dan-petty

Problem

After #5995, the group deepseek-v4.1-flash-1017 has two deployments: the dated model (order 1) and the undated model (order 2, about 4 times the input price). The reply body of an agent run names the group, not the deployment, so the client spend ledger of devops-cli prices a call that order 2 answers at the order-1 price. Chat calls and the gateway's own spend are correct for each deployment.

Expected behavior

  • The client ledger records the deployment model that answered each call and its price, for agent runs too. LiteLLM 1.103.11 has a request flag that makes the reply name the real deployment model; use it, or read the deployment from the response headers.
  • A test with a reply from order 2 records the order-2 price.

Related

Alternatives check of 2026-11-24: in-use.
The repository already uses this tool.
Tool: BerriAI/litellm (https://github.com/BerriAI/litellm), NOASSERTION, last activity 2026-11-24, maintained.
Reason: LiteLLM, the gateway the project already runs, names the deployment that answered in its x-litellm-model-name response header and prices spend per deployment, but the client ledger rows for agent runs are code of this repository, which no external tool supplies.

Covers:

  • Response header names the deployment that answered
  • Per-deployment spend and pricing records
  • Ordered deployments behind one model group

Lacks:

  • No client spend ledger row for agent runs
  • No per-call deployment price applied to agent replies
  • No verified request flag that rewrites the reply body model

Proposed design

Problem statement

The client spend ledger of devops-cli prices an agent-run gateway call at the model group, because the agent spend hooks store the reply body's model field as served_model, and LiteLLM writes the requested group there. After #5995 the group deepseek-v4.1-flash-1017 has two deployments, and a call that the order-2 deployment answers is priced at the order-1 rate, about 4 times too low. Chat calls and the gateway's own spend already price each deployment. The change records the deployment model that answered an agent call from the gateway reply header and prices the call at that deployment's existing price row.

Acceptance criteria

Requirement: Agent gateway client captures the deployment model

The gateway HTTP client of an agent run MUST capture the x-litellm-model-name response header of each call and carry the deployment model that it names to the agent spend hook.

Scenario: An agent call answered by order 2

  • WHEN A test runs an agent on a gateway stub whose reply sets x-litellm-model-name to openrouter/deepseek/deepseek-v4.1-flash.
  • THEN The spend record of the request names openrouter/deepseek/deepseek-v4.1-flash in served_model.

Requirement: Agent run is priced at the deployment that answered

The client spend ledger SHALL price an agent-run gateway call at the deployment model that the gateway reply header names.

Scenario: An agent run answered by the order-2 deployment

  • WHEN A test runs an agent on a gateway stub that answers with the order-2 deployment header and 1,000,000 input tokens.
  • THEN The ledger row records openrouter/deepseek/deepseek-v4.1-flash and 0.3 USD, not the 0.0784 USD input price of the group.

Requirement: Chat call keeps the deployment price

The client spend ledger SHALL keep pricing a chat gateway call at the deployment model that the reply header names.

Scenario: A chat call answered by the order-1 deployment

  • WHEN A test sends a chat call through a gateway stub that answers with x-litellm-model-name set to openrouter/deepseek/deepseek-v4.1-flash-1017.
  • THEN The ledger row records that deployment model and the 0.0784 USD input price of order 1.

Requirement: Answer counter keeps the group

The agent spend hooks MUST keep the reply body's model group in the asked and answered labels of devops_cli_ai_model_answers_total.

Scenario: An agent run answered by order 2

  • WHEN A test runs an agent on a gateway stub whose reply body names deepseek-v4.1-flash-1017 and whose header names the order-2 deployment.
  • THEN The counter records asked and answered as deepseek-v4.1-flash-1017 with fallback false, while the ledger row names the order-2 deployment.

Requirement: A reply with no deployment header falls back to the group

When a gateway reply names no deployment model, the client spend ledger MUST keep the model group and its price for an agent-run call.

Scenario: An agent reply with no deployment header

  • WHEN A test runs an agent on a gateway stub whose reply sets no x-litellm-model-name header.
  • THEN The ledger row records the group deepseek-v4.1-flash-1017 and the order-1 input price.

Existing Tool Considered

LiteLLM 1.103.11 (https://github.com/BerriAI/litellm) is the existing tool; the alternatives.block of the issue body records the verdict in-use and the lacks it names. The part that the block records as lacking is client code of this repository, so the change builds it: about 25,000 tokens to build, about 2,000 tokens a year to keep, about 3,000 tokens to fix, and about 3 hours elapsed. It adds no tool.

Key questions

  • Which LiteLLM 1.103.11 mechanism should the client use to identify the deployment that answered a gateway call?: Use the x-litellm-model-name response header. The project already reads it in extract_served_model, and LiteLLM sets it to the deployment model while the reply body names the requested group. For agent runs, add a response event hook to the gateway HTTP client, which already installs a request event hook, and carry the header value on GatewayCallModel. The two agent spend paths then pass it as served_model. Keep response.model_name for the devops_cli_ai_model_answers_total labels, so a group that answered still reads as no fallback. Do not use the reply body's model field, which names the group; no repository source verifies the 1.103.11 request flag that the issue mentions. (best practice) [sources: src/devops_cli/ai/client/network.py:169, src/devops_cli/ai/providers/init.py:177, src/devops_cli/ai/models/gateway.py:78, src/devops_cli/config/constants.py:3135, openspec/changes/archive/2026-10-24-2031-gateway-deployment-model-pricing/proposal.md:36, k8s/README.md:115, src/devops_cli/ai/client/factory.py:214, src/devops_cli/roadmap/agent_run.py:440]
    • Chosen: Response header captured by a response event hook. The header names the deployment model, the project already reads it for chat calls, and the gateway HTTP client already installs event hooks.
    • Rejected: Reply body model field. LiteLLM writes the requested group alias there, so order 2 still gets the order-1 price.
    • Rejected: LiteLLM 1.103.11 request flag. No repository source verifies such a flag; the response header is the verified mechanism and already in use.
    • Rejected: Gateway spend record only. It leaves the client ledger row at the group price.
  • How should the client ledger price a call from the deployment that answered?: Pass the deployment model from the header as served_model. calculate_request_cost already prices a hosted call at served_model instead of the asked model, and _HOSTED_GROUP_PRICES already holds a row for each deployment, including openrouter/deepseek/deepseek-v4.1-flash at 0.3 USD input and 1.2 USD output per million. No new row and no multiplier is needed; test_every_hosted_gateway_group_is_priced_by_its_route_name_and_its_deployment_model guards the rows. (best practice) [sources: src/devops_cli/ai/spend/pricing.py:71, src/devops_cli/ai/spend/pricing.py:87, src/devops_cli/ai/spend/pricing.py:865, k8s/llm/gateway/config.yaml:239, tests/ai/spend/test_hosted_group_prices.py:85, openspec/changes/archive/2026-11-11-4403-price-the-deepseek-models-in-the/proposal.md:11]
    • Chosen: Deployment-keyed row in the existing price table. The order-2 row already exists, and calculate_request_cost prefers served_model, so the call is priced exactly.
    • Rejected: Add a second order-2 row. _HOSTED_GROUP_PRICES already holds the order-2 deployment model, so a second row would duplicate it.
    • Rejected: Group price with a multiplier. A multiplier is a hard-coded fact that breaks when prices change.
    • Rejected: Gateway spend record only. It does not write the client ledger row that the issue requires.
  • What should the agent spend paths record when the gateway reply carries no deployment header?: Keep the current behavior: record the reply body's model group as served_model and price it at the group row. A streamed reply whose first token comes after the gateway's first SSE ping has no x-litellm-* headers, so a missing header is a real case; the group price is the safe fallback and keeps a ledger row. (best practice) [sources: k8s/README.md:115, src/devops_cli/roadmap/agent_run.py:440, src/devops_cli/ai/client/factory.py:214]
    • Chosen: Fall back to the group model. It is the current behavior and keeps a ledger row for a stream whose headers never arrived.
    • Rejected: Record no model. The row would lose the deployment and the price even though the group is known.
    • Rejected: Fail the call. A missing header is a known gateway stream case, not a client error.

Fit

Fits in one pull request.

Suspected block

None.

Dependencies

None.

Activity

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

    @dan-petty
    OwnerAuthor
  3. added
    priority/p2-medium
    on Nov 16, 2026
  4. devops-cli-service commented on Nov 17, 2026

    @devops-cli-service
    Contributor

    The result in the alternatives block of the issue body changed. Before this check: no check. A person decides what the new result means for the item.

    Searches (10)

    Tools considered:

    Searches run:

    • LiteLLM response header x-litellm-model-id which deployment answered, results: 5
    • LiteLLM 1.103 request flag return deployment model in response body, results: 5
    • litellm return_response_headers _hidden_params response_headers deployment, results: 5
    • docs.litellm.ai response headers x-litellm-model-id model group, results: 5
    • BerriAI/litellm GitHub which deployment served the request, results: 5
    • litellm proxy model_group_info response model name actual deployment, results: 5
    • litellm, results: 5
    • litellm model_group_alias deployment id header response, results: 5
    • litellm proxy response headers documentation, results: 5
    • litellm github pull request flag return deployment model name in response, results: 5

    Sources:

  5. added
    status/ready
    and removed
    status/new
    on Nov 18, 2026
  6. devops-cli-service commented on Nov 18, 2026

    @devops-cli-service
    Contributor

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

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

    @devops-cli-service
    Contributor

    Live since 2026-11-25 17:09 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/aisource/agenttype/bugvalue/medium

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions