Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions docs/CLI_REFERENCE.md
Original file line number Diff line number Diff line change
Expand Up @@ -5231,7 +5231,7 @@ devops ai cost set-price <target> <prompt_rate> <completion_rate>

| Argument | Type | Required | Description |
|---|---|---|---|
| `<target>` | `string` | Yes | Target model identifier or backend server address of hosted calls (e.g. 'gpt-4o', 'api.example.com'). A local call costs nothing, also when it has a custom price. Its tokens count in local savings. |
| `<target>` | `string` | Yes | Target model identifier or backend server address of hosted calls (e.g. 'gpt-4o', 'api.example.com'). When a gateway reply names its deployment model, such as 'openai/gpt-4o', that model is the target, not the route name. A local call costs nothing, also when it has a custom price. Its tokens count in local savings. |
| `<prompt_rate>` | `float` | Yes | Prompt token cost in USD per 1,000,000 tokens. |
| `<completion_rate>` | `float` | Yes | Completion token cost in USD per 1,000,000 tokens. |

Expand Down Expand Up @@ -5361,7 +5361,7 @@ devops ai spend set-price <target> <prompt_rate> <completion_rate>

| Argument | Type | Required | Description |
|---|---|---|---|
| `<target>` | `string` | Yes | Target model identifier or backend server address of hosted calls (e.g. 'gpt-4o', 'api.example.com'). A local call costs nothing, also when it has a custom price. Its tokens count in local savings. |
| `<target>` | `string` | Yes | Target model identifier or backend server address of hosted calls (e.g. 'gpt-4o', 'api.example.com'). When a gateway reply names its deployment model, such as 'openai/gpt-4o', that model is the target, not the route name. A local call costs nothing, also when it has a custom price. Its tokens count in local savings. |
| `<prompt_rate>` | `float` | Yes | Prompt token cost in USD per 1,000,000 tokens. |
| `<completion_rate>` | `float` | Yes | Completion token cost in USD per 1,000,000 tokens. |

Expand Down
2 changes: 1 addition & 1 deletion docs/TELEMETRY.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ native histogram under the instrument's own name, with no `_bucket`, `_sum` or
| `devops_cli_findings_total` | Counter | `1` | Review findings, by producer, severity and status. |
| `devops_cli_ai_requests_total` | Counter | `1` | LLM calls and embedding requests, by operation (chat or embeddings), provider, model, server, serving backend and whether the cache answered. |
| `devops_cli_ai_tokens_total` | Counter | `1` | LLM and embedding tokens, by operation (chat or embeddings), type (prompt or completion), provider, model, server, backend and local: true when a local model ran them. The AI spend dashboard prices local tokens as savings at the reference price that the Prometheus recording rule devops_cli:ai_reference_price_usd_per_token holds for their operation and type. |
| `devops_cli_ai_spend_usd_total` | Counter | `USD` | Approximate LLM and embedding spend, by operation, provider, model, server and backend. Each request that a model answers adds to it, and a local request adds 0. |
| `devops_cli_ai_spend_usd_total` | Counter | `USD` | Approximate LLM and embedding spend, by operation, provider, model, server and backend. Each request that a model answers adds to it, and a local request adds 0. A hosted gateway call is priced at the deployment model that the gateway names, and its model label is the model that the client asked for, such as the route name. |
| `devops_cli_ai_json_repairs_total` | Counter | `1` | LLM JSON replies repaired after strict parsing failed, by entry point source. |
| `devops_cli_ai_gateway_slot_wait_seconds` | Histogram | `s` | Time a chat call to the LLM gateway waited in this process for one of the gateway's backend slots, by outcome: ok once it held one, or timeout. |
| `devops_cli_rag_query_duration_ms` | Histogram | `ms` | RAG retrieval time, by stage: embedding, search, ranking, or total for the whole query. |
Expand Down
4 changes: 2 additions & 2 deletions docs/commands/ai.md
Original file line number Diff line number Diff line change
Expand Up @@ -1998,7 +1998,7 @@ devops ai cost set-price <target> <prompt_rate> <completion_rate>

| Argument | Type | Required | Description |
|---|---|---|---|
| `<target>` | `string` | Yes | Target model identifier or backend server address of hosted calls (e.g. 'gpt-4o', 'api.example.com'). A local call costs nothing, also when it has a custom price. Its tokens count in local savings. |
| `<target>` | `string` | Yes | Target model identifier or backend server address of hosted calls (e.g. 'gpt-4o', 'api.example.com'). When a gateway reply names its deployment model, such as 'openai/gpt-4o', that model is the target, not the route name. A local call costs nothing, also when it has a custom price. Its tokens count in local savings. |
| `<prompt_rate>` | `float` | Yes | Prompt token cost in USD per 1,000,000 tokens. |
| `<completion_rate>` | `float` | Yes | Completion token cost in USD per 1,000,000 tokens. |

Expand Down Expand Up @@ -2130,7 +2130,7 @@ devops ai spend set-price <target> <prompt_rate> <completion_rate>

| Argument | Type | Required | Description |
|---|---|---|---|
| `<target>` | `string` | Yes | Target model identifier or backend server address of hosted calls (e.g. 'gpt-4o', 'api.example.com'). A local call costs nothing, also when it has a custom price. Its tokens count in local savings. |
| `<target>` | `string` | Yes | Target model identifier or backend server address of hosted calls (e.g. 'gpt-4o', 'api.example.com'). When a gateway reply names its deployment model, such as 'openai/gpt-4o', that model is the target, not the route name. A local call costs nothing, also when it has a custom price. Its tokens count in local savings. |
| `<prompt_rate>` | `float` | Yes | Prompt token cost in USD per 1,000,000 tokens. |
| `<completion_rate>` | `float` | Yes | Completion token cost in USD per 1,000,000 tokens. |

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
schema: spec-driven
created: 2026-10-24
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# Design

## Context

`track_request_spend` in `src/devops_cli/ai/spend/ledger.py` prices each call with `PricingRegistry.calculate_request_cost`, at the model that the client asked for. For a gateway call, this model is the route name, such as `devops-chat`. A reply of the LiteLLM gateway names the backend in `x-litellm-model-api-base`, which decides if the call is local or hosted (#1631). It names the deployment model in `x-litellm-model-name`. See proposal.md for the evidence.

## Goals / Non-Goals

**Goals:**

- A hosted gateway call is priced at the deployment model that served it, for a blocking chat call, a streamed chat call and an embedding request.
- The spend ledger, the metrics and `devops ai cost` keep the model that the client asked for.

**Non-Goals:**

- A metric label for the deployment model. The ledger holds it.
- The `gen_ai.response.model` attribute of the inference span. It keeps the model of the reply body.
- A change to the reference prices that price the local tokens on the dashboard (#1672). See the record below.

## Decisions

- **genai-prices reads the deployment model as a LiteLLM model reference.** The registry gives genai-prices the provider ID `litellm` (`CONST_GENAI_PRICES_LITELLM_PROVIDER`), and genai-prices takes the provider from the prefix. Rejected: price the deployment model under the request provider, `gateway`, and then with no provider, as the registry does for other models. genai-prices knows no provider `gateway`. With no provider, it gave `LookupError` for `text-embedding-3-small`, because no provider has a `model_match` for this model. Rejected: a table from LiteLLM prefixes to genai-prices providers. genai-prices already holds this map in `provider_match`.
- **The deployment model is the target of a price override.** When the reply names a deployment model, the override of that model applies, and the override of the route name does not. A route can send calls to a local deployment and to a hosted deployment, and the price belongs to the deployment. The help of `devops ai cost set-price` says this. Rejected: the override of the route name first. It gives one price to each hosted deployment of the route.
- **The local check comes first.** The backend that served the call decides if it is local before the registry reads the deployment model. Thus a local deployment, such as `ollama_chat/qwen3.8:27b`, stays local and costs nothing.
- **The route name stays the model of the row and of the metrics.** The issue asks for this, and the dashboards and `devops ai cost --by model` group by it. Rejected: write the deployment model in `model`. A route then has one series for each deployment, and the request counts do not show what the clients asked for.
- **The ledger stores the deployment model in `served_model`.** A row that `genai_prices:openai` priced then shows the model that set its price. Rejected: no column. The row then shows only `devops-chat` and `genai_prices:openai`, and a person cannot see which model priced it.
- **No metric label for the deployment model.** No dashboard reads it, and each deployment adds series. The ledger holds it.
- **Only `config/constants.py` and `ai/client/network.py` name a gateway reply header.** `extract_served_model` reads the header in `network.py`, next to `extract_served_by`. A streamed reply keeps the deployment model in the context variable `stream_served_model`, as it keeps the backend. `tests/test_url_credential_masking.py` checks the rule for the two headers.

## Record: the reference models of the dashboard

On 2026-10-24 the owner confirmed that the AI spend dashboard prices local tokens at the default reference models: `gpt-4o-mini` for chat and `text-embedding-3-small` for embeddings. This answers the "Pending a person" bullet about the reference models in `openspec/changes/archive/2026-10-24-1672-local-savings-reference-prices/tasks.md`.

## Risks / Trade-offs

- [A gateway does not send `x-litellm-model-name`] → The call is priced at the route name, as before. Its source is `unknown`, and the registry logs one warning that names the route name.
- [genai-prices does not know a deployment model] → The call costs $0 with the source `unknown`, and the registry logs one warning that names the deployment model and the provider `litellm`.
- [Rows from before the change have no `served_model`] → Their recorded cost does not change.
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
# Proposal

## Why

Issue #2031: the spend ledger priced a hosted gateway call at the route name that the client asked for, for example `devops-chat`. genai-prices does not know a route name, so the call cost $0 with the pricing source `unknown`. The LiteLLM gateway names the deployment model that served the call in the `x-litellm-model-name` reply header, for example `openai/gpt-4o`. devops-cli did not read this header.

## What Changes

- devops-cli reads the `x-litellm-model-name` header of a gateway reply for a blocking chat call, a streamed chat call and an embedding request. `CONST_AI_GATEWAY_SERVED_MODEL_HEADER` in `src/devops_cli/config/constants.py` holds the header name, and `extract_served_model` in `src/devops_cli/ai/client/network.py` reads it. Only these two files name a gateway reply header. `tests/test_url_credential_masking.py` now checks this rule for both headers.
- `PricingRegistry.calculate_request_cost` takes `served_model`. When the reply names a deployment model, a hosted call is priced at that model, as a LiteLLM model reference: genai-prices gets the provider ID `litellm` and takes the provider from the prefix. A price override of the deployment model applies, and a price override of the route name does not. When the reply names no deployment model, the call is priced at the model that the client asked for, as before. A local call stays local.
- The model that the client asked for stays the `model` of the spend-ledger row and the `model` label of `devops_cli_ai_requests_total`, `devops_cli_ai_tokens_total` and `devops_cli_ai_spend_usd_total`. Thus the dashboards and `devops ai cost` still group by the route name.
- Each spend-ledger row stores the deployment model in the new column `served_model`. An existing ledger gets the column and keeps its rows.
- The description of `devops_cli_ai_spend_usd_total` in `docs/TELEMETRY.md` and the help of `devops ai cost set-price` say which model prices a gateway call.
- The value of a metric, the spend ledger and the help of a command change, so the change has a spec delta. It adds requirements to `ai-spend-pricing`.
- The change fits one pull request.

## Capabilities

### New Capabilities

None.

### Modified Capabilities

- `ai-spend-pricing`: adds the requirements that a hosted gateway call is priced at the deployment model that the gateway names, that the route name stays the model of the row and of the metrics, and that each row stores the deployment model.

## Impact

- Code: `src/devops_cli/config/constants.py`, `src/devops_cli/ai/client/network.py`, `models.py`, `openai.py` and `unified.py`, `src/devops_cli/ai/rag/embeddings.py`, `src/devops_cli/ai/spend/pricing.py`, `ledger.py` and `models.py`, `src/devops_cli/telemetry/instruments.py` and `src/devops_cli/commands/ai_cost.py`.
- Documents: `docs/TELEMETRY.md`, `docs/CLI_REFERENCE.md` and `docs/commands/ai.md`, generated again.
- Tests: `tests/test_ai_served_by.py`, `tests/test_ai_spend_pricing.py`, `tests/test_ai_spend_ledger.py`, `tests/test_rag_embeddings.py`, `tests/test_telemetry_instruments.py` and `tests/test_url_credential_masking.py`.
- Today each route in `k8s/llm/gateway/config.yaml` sends its calls to an Ollama deployment in the cluster, so each live gateway call is local. The live spend changes only when a hosted deployment serves a call.

## Feasibility

- LiteLLM 1.103.0 source, read in the gateway pod (image `ghcr.io/berriai/litellm:v1.103.0`, read-only `kubectl -n llm exec deploy/llm-gateway -- python -c ...`): `ProxyBaseLLMRequestProcessing.get_custom_headers` sets `x-litellm-model-name` to `_get_deployment_model_name(litellm_logging_obj)`. That function reads `deployment` from the `litellm_metadata` or `metadata` of the `litellm_params` of the call. Its docstring says that the router writes the model-group alias that the client asked for in the `model` field of the reply. Thus the reply body and the other headers do not name the deployment model.
- `Router._update_kwargs_with_deployment` sets `deployment` to `litellm_params.model` of the deployment that it selects, and `api_base` from the same deployment. Thus `x-litellm-model-name` and `x-litellm-model-api-base` name the same deployment.
- Each call of `get_custom_headers` in `litellm/proxy/common_request_processing.py` gives the logging object: the blocking reply, the streamed reply and the failure reply. `/v1/embeddings` goes through `base_process_llm_request`, as `/v1/chat/completions` does.
- Live Prometheus, read-only through Grafana on 2026-10-24: in the last 7 days, `devops_cli_ai_requests_total{provider="gateway"}` had three `backend` values, one for each Ollama tier that served a call. devops-cli takes these values from `x-litellm-model-api-base`, which the same `get_custom_headers` call sets. The `model` values were the models that the clients asked for.
- genai-prices 0.1.9, installed source: `DataSnapshot.find_provider_model` takes the provider ID `litellm` with a model reference that has a `/`. It takes the provider ID from the prefix and the model from the remaining text, when `find_provider_by_id` knows the provider. `find_provider_by_id` also uses the `provider_match` of each provider, so `bedrock` finds `aws`.
- An offline probe of the bundled snapshot with the provider ID `litellm` and 1,000,000 input tokens gave these prices and providers: `openai/gpt-4o` $2.50 (openai), `openai/text-embedding-3-small` $0.02 (openai), `bedrock/anthropic.claude-3-5-sonnet-20240620-v1:0` $3.00 (aws), `openrouter/anthropic/claude-3.5-sonnet` $3.00 (openrouter), `gemini/gemini-2.5-flash` and `vertex_ai/gemini-2.5-flash` $0.30 (google), `azure/gpt-4o` $2.50 (azure), and `anthropic/claude-sonnet-4-5` (anthropic). `ollama_chat/qwen3.8:27b` and `devops-chat` gave `LookupError`. With no provider, `text-embedding-3-small` and `openai/gpt-4o` gave `LookupError`. With the provider `gateway`, each model gave `LookupError`.
- On the base commit, the ten new tests failed: the ledger had no `served_model` column, `track_request_spend`, `SpendLedger.record_request` and `PricingRegistry.calculate_request_cost` took no `served_model`, and no source file named `x-litellm-model-name`.

## Existing Tool Considered

- genai-prices, the price source of the ledger, reads a LiteLLM model reference itself under the provider ID `litellm`. devops-cli gives it the deployment model with this provider ID. devops-cli holds no table of LiteLLM prefixes and does not split the reference.
- LiteLLM sends its own cost of a call in `x-litellm-response-cost`. Rejected: the ledger keeps one price source for each call: genai-prices, with its snapshot update and the price overrides of `devops ai cost set-price`. A call to a provider without the gateway has no such header.
- `litellm.get_llm_provider` also splits a LiteLLM model reference. Rejected: litellm is not a dependency of devops-cli, and genai-prices already does the split.
- The change deletes no code. `_parse_compat_response` takes the reply headers in place of the backend, and reads the backend and the deployment model from them.
Loading