Skip to content

fix(ai): a hosted gateway call names its provider as backend #5546

Done

Description

@dan-petty

Problem Description

The AI spend dashboard (k8s/devops/dashboards/ai-spend.json, panels "Spend per Hour by Backend Server" and "Local Savings per Hour by Backend Server") names its series with the legend {{backend}} via {{server}} ({{provider}}). The series of hosted models have an empty backend label, so their legend reads " via ai.retric.click (gateway)" or " via llm-gateway.llm.svc.cluster.local:4000 (gateway)". The owner read these names as parse failures. The data is correct, but the series do not say which provider served the call.

Evidence (Prometheus, sum by (server, backend, provider, model) (increase(devops_cli_ai_requests_total{cached="false"}[24h])), 2026-11-13):

  • Each Ollama series has a backend, for example ollama-48gib-fast for qwen3.8:27b.
  • deepseek-v4.1-flash-1017 (gateway deployment openrouter/deepseek/deepseek-v4.1-flash-1017:floor) has no backend label: about 4,400 requests in 24 hours on the two servers.
  • The Anthropic and Gemini groups have the same gap whenever they run.

Cause

  • record_request in ai/spend/ledger.py sets the metric label backend with backend_name(served_by) (telemetry/instruments.py).
  • served_by comes from the gateway header x-litellm-model-api-base (CONST_AI_GATEWAY_SERVED_BY_HEADER, read by extract_served_by in ai/client/network.py).
  • LiteLLM sends an API base only for a deployment that sets api_base, such as an Ollama deployment. A hosted deployment uses the default base of its provider, so the header is missing and backend_name returns an empty string.
  • The same code already reads the deployment model of the reply from x-litellm-model-name (served_model), for example openrouter/deepseek/... or anthropic/claude-sonnet-5-5. Its LiteLLM provider prefix names the provider that served the call.

Proposed Fix

  • When served_by names no backend and served_model has a LiteLLM provider prefix (openrouter/, anthropic/, gemini/ and the other prefixes of the pinned LiteLLM), set backend to that prefix.
  • Use one helper for the metric labels and for the ledger row, so the two stay the same.
  • Do not invent a backend when both headers are missing: keep the empty label.
  • Check the legend of both dashboard panels after the change. A hosted series must read, for example, openrouter via ai.retric.click (gateway).

Acceptance Criteria

  • A test of record_request with served_by=None and served_model="openrouter/deepseek/deepseek-v4.1-flash-1017:floor" gives the metric label backend="openrouter" (devops ci fail-to-pass).
  • A test with served_model="anthropic/claude-sonnet-5-5" gives backend="anthropic". A test with an Ollama served_by keeps the host label, for example ollama-48gib-fast.
  • A test with neither header keeps backend="".
  • After the release, the Prometheus query above shows no hosted series with an empty backend label.

Related

#2040, #2080, #3656 and #4907 set the local and hosted split of a gateway call. This issue only names the backend.

Alternatives check of 2026-11-23: in-use.
The repository already uses this tool.
Tool: litellm (https://pypi.org/project/litellm), MIT, last activity 2026-11-20, maintained.
Reason: Searches found only LiteLLM, which the project already runs as its gateway: it sends the provider-prefixed model name, but it sets no backend label, so the project still needs its own helper for metric and ledger.

Covers:

  • Reply header carries the deployment model name
  • Provider-prefixed model strings such as openrouter/ and anthropic/
  • Model API base header for deployments that set api_base

Lacks:

  • No backend label on the metric or the ledger row
  • No mapping of a provider prefix into the project's label value
  • No shared helper for metric label and ledger row

Proposed design

Problem statement

A gateway call to a hosted model has an empty backend label on devops_cli_ai_requests_total and an empty backend in the spend ledger. LiteLLM sends the x-litellm-model-api-base header only for a deployment that sets api_base, so backend_name(served_by) returns an empty string for the openrouter, anthropic and gemini deployments. The owner reads the empty name as a parse failure. The change names the LiteLLM provider of the x-litellm-model-name header when the host header is absent.

Acceptance criteria

Requirement: A hosted call names its provider on the metric

record_request SHALL set the backend label of devops_cli_ai_requests_total to the LiteLLM provider of served_model when served_by names no backend.

Scenario: OpenRouter deployment with no API base

  • WHEN A test calls record_request with served_by=None and served_model="openrouter/deepseek/deepseek-v4.1-flash-1017:floor".
  • THEN The test reads the metric and asserts the backend label is openrouter.

Requirement: The ledger row and the metric share one backend

record_request MUST take the backend of the ledger row and of the metric label from one helper, so the two values are equal.

Scenario: Ledger row of a hosted call

  • WHEN A test calls record_request with served_by=None and served_model="openrouter/deepseek/deepseek-v4.1-flash-1017:floor".
  • THEN The test asserts the ledger row backend is openrouter and equals the metric backend label.

Requirement: Each hosted provider prefix names that provider

The helper SHALL name the provider of every hosted prefix, such as anthropic and gemini, from the first segment of served_model.

Scenario: Anthropic deployment with no API base

  • WHEN A test calls record_request with served_by=None and served_model="anthropic/claude-sonnet-5-5".
  • THEN The test asserts the backend label is anthropic.

Requirement: An Ollama call keeps its host label

The helper SHALL return the host of served_by before it reads the provider prefix of served_model.

Scenario: Ollama deployment that sets an API base

  • WHEN A test calls record_request with served_by="http://ollama-48gib-fast.llm.svc.cluster.local:11434" and served_model="ollama_chat/qwen3.8:27b".
  • THEN The test asserts the backend label is ollama-48gib-fast, not ollama_chat.

Requirement: No header names no backend

The helper MUST return an empty backend when served_by is empty and served_model has no provider prefix.

Scenario: A reply with no host and no provider prefix

  • WHEN A test calls record_request with served_by=None and served_model="qwen3.8:27b".
  • THEN The test asserts the backend label is the empty string.

Existing Tool Considered

litellm (https://pypi.org/project/litellm), MIT and maintained, has the verdict in-use in the alternatives check of 2026-11-17 in the issue body. The check there records what litellm covers and what it lacks. The proposal adds the part that litellm lacks. One helper in src/devops_cli/telemetry/instruments.py returns the served_by host first, else the provider prefix of served_model. record_request uses that helper for the metric label and for the ledger row. The change imports no new dependency and keeps the host label of an Ollama deployment.

Key questions

  • Where does the backend label of devops_cli_ai_requests_total come from?: record_request in src/devops_cli/ai/spend/ledger.py sets it from backend_name(served_by) in src/devops_cli/telemetry/instruments.py. served_by is the x-litellm-model-api-base header, CONST_AI_GATEWAY_SERVED_BY_HEADER at src/devops_cli/config/constants.py:3160, which extract_served_by in src/devops_cli/ai/client/network.py:103 reads. A hosted deployment sets no api_base, so the header is absent and backend_name returns the empty string. [sources: src/devops_cli/ai/spend/ledger.py, src/devops_cli/telemetry/instruments.py:1049, src/devops_cli/ai/client/network.py:103, src/devops_cli/config/constants.py:3160]
  • Which value names the provider of a hosted gateway call?: The first segment of served_model, the deployment model that x-litellm-model-name carries: openrouter in openrouter/deepseek/deepseek-v4.1-flash-1017:floor. The helper returns that segment when served_by is empty and the model holds a /. It returns the empty string when the model holds no /. The value is the provider name that the gateway's api_provider label holds, which the panel Spend by provider and model: USD per hour reads. (best practice) [sources: src/devops_cli/ai/client/network.py:103, k8s/devops/dashboards/ai-spend.json]
    • Chosen: The first segment of served_model. It matches the <provider>/<model> form of a LiteLLM deployment, covers a new provider with no list to keep, and invents no label when the model has no prefix.
    • Rejected: A fixed list of the openrouter, anthropic and gemini prefixes. A new hosted provider gets the empty label again, which is the defect that this item fixes.
    • Rejected: litellm.utils.get_llm_provider. The alternatives check records the large dependency and no change to the label or the ledger row, and the CLI reads one header, so the mapping is one string split.
  • Where does the one helper for the metric label and the ledger row live?: backend_label(served_by, served_model) lives in src/devops_cli/telemetry/instruments.py, beside backend_name, which src/devops_cli/ai/spend/ledger.py already calls. record_request calls it once and uses the result for the metric label and for the ledger row. The helper takes two strings and imports no module of the AI client. (best practice) [sources: src/devops_cli/telemetry/instruments.py:1049, src/devops_cli/ai/spend/ledger.py]
    • Chosen: src/devops_cli/telemetry/instruments.py, beside backend_name. The module holds the vocabulary of the metric label, and its caller in the ledger keeps one path to the value.
    • Rejected: src/devops_cli/ai/spend/ledger.py. A second caller of the label would copy the rule, and the metric instrument already holds backend_name.
    • Rejected: src/devops_cli/ai/client/network.py, beside extract_served_by. That module reads headers and holds no metric label, so the label rule does not belong there.
  • Do the two dashboard panels still read the backend label of devops_cli_ai_requests_total?: No. The reply records that fix(monitoring): read AI spend from the gateway counter #5924 changed the first panel to Spend by provider and model: USD per hour, which reads api_provider of litellm_spend_metric_total. The reply also records that fix(monitoring): price local savings at the reference model #5931 for fix(monitoring): price local savings at the reference model #5914 changed the local savings panel to read the gateway token counters and to name each bar from the api_base service. k8s/devops/dashboards/ai-spend.json holds both queries. The dashboard legend step is therefore out of this proposal, and it is not deferred, because no work remains there. [sources: k8s/devops/dashboards/ai-spend.json]
  • Does the post-release Prometheus query stay an acceptance criterion?: No. The query needs the release on the cluster and a person to read Prometheus, so it cannot pass before the merge. The tests of record_request hold the label of the metric and of the ledger row. The live query moves to a follow-up issue, and this item reaches Ready on the tests alone. (best practice) [sources: tests/ai/spend/test_ai_spend_ledger.py]
    • Rejected: Keep the Prometheus query as an acceptance criterion. No offline test checks it, and a criterion must pass before the merge.
    • Chosen: Move the live query to a follow-up issue. The query waits on the release and on a person, and the offline tests already hold the label.

Fit

Fits in one pull request.

Suspected block

None.

Dependencies

None.

Moved to new items

#6497

Activity

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

    @dan-petty
    OwnerAuthor
    • Priority: P3-Low. The data is correct and only the dashboard legend is misleading, so this is a low-urgency fix.
    • Related: feat(ai): llm gateway calls record their serving backend in the spend ledger #474, which asks for a different change. Both concern the backend label in the spend ledger, but #474 delivered the initial recording while this candidate fixes a gap where hosted models get an empty backend.
  3. added
    priority/p3-low
    on Nov 13, 2026
  4. dan-petty commented on Nov 16, 2026

    @dan-petty
    OwnerAuthor

    The two dashboard panels of this item no longer read devops-cli's backend label. #5924 changed "Spend per Hour by Backend Server" to "Spend by provider and model: USD per hour", which reads api_provider of the gateway's litellm_spend_metric_total. Pull request #5931 (for #5914) changes "Local Savings per Hour by Backend Server" to read the gateway token counters, and names each bar by the service of its api_base. So the step "check the legend of both dashboard panels" does not apply after the merge. The fix of record_request and its criteria still apply to devops_cli_ai_requests_total and the spend ledger.

  5. devops-cli-service commented on Nov 17, 2026

    @devops-cli-service
    Contributor

    Alternatives check: in-use

    Reason: Searches found only LiteLLM: its provider map turns a served-model prefix into a provider name, covering the mapping but not the metric label, ledger row or dashboard.

    The repository already uses this tool.

    Tool: litellm (https://pypi.org/project/litellm), MIT, last activity 2026-11-12, maintained.

    Covers:

    • Prefix-to-provider mapping for openrouter, anthropic and gemini models
    • Provider name from the same string as x-litellm-model-name
    • Provider list kept current with each pinned release

    Lacks:

    • No change to the metric label or the ledger row
    • No fallback to the served_by host label for Ollama
    • Large dependency for a CLI that only reads gateway headers
    • No dashboard legend change
    Searches (10)

    Tools considered:

    Searches run:

    • litellm get_llm_provider parse provider from model name, results: 5
    • python library map model name to provider prefix openrouter anthropic gemini, results: 5
    • "litellm" "get_llm_provider", results: 5
    • litellm docs model name provider prefix get_llm_provider, results: 5
    • LiteLLM proxy x-litellm-model-name header provider prefix, results: 5
    • docs.litellm.ai litellm.utils get_llm_provider model provider api_base, results: 5
    • litellm github source get_llm_provider openrouter anthropic gemini prefix mapping, results: 5
    • litellm python package provider detection function, results: 5
    • LiteLLM documentation supported providers list, results: 5
    • python package identify LLM provider from model string, results: 5

    Sources:

    A person decides what this check of 2026-11-17 means for the item.

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

    @devops-cli-service
    Contributor

    Next step: the stage job writes the plan of the item. See Moved to new items in the section Proposed design of the body.

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

    @devops-cli-service
    Contributor

    Live since 2026-11-29 16:50 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/lowpriority/p3-lowscope/aisource/agenttype/bugvalue/medium

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions