Skip to content

feat(ai): label gateway spend by repository #4578

Open

Description

@dan-petty

Update 2026-11-26 (infra move): the gateway config moved to dan-petty/homelab.

This issue keeps the client part (send repo as request metadata from gateway_caller_headers, with its tests) and the spend-by-repository panel in k8s/devops/dashboards/. The custom_prometheus_metadata_labels setting, the premise check on the live /metrics and the series count are in the homelab follow-up. Do that one first: its premise check decides if this item can proceed.

Follow-up: dan-petty/homelab#271.


Problem or Motivation

The gateway spend series show spend by caller (end_user) and model, but not by repository. A person cannot see what one repository's roadmap work costs. After the move, the row comes from end_user = roadmap-<row> (the service name of each row pod), so only the repository is missing.

Proposed Solution & Architectural Design

  • Premise check first (AGENTS.md rule 2): the LiteLLM page documents custom_prometheus_metadata_labels, but does not say that litellm_spend_metric and litellm_total_tokens_metric carry these labels (https://docs.litellm.ai/docs/proxy/prometheus). Send one request with metadata to the gateway and read /metrics. If the two metrics lack the label, return the item with the evidence.
  • src/devops_cli/ai/client/network.py (gateway_caller_headers, line 64): a context variable that the router, the sweep or a unit pod sets for the repository of the work, sent as request metadata repo = the casefolded key of RepoRef.
  • Gateway config (dan-petty/homelab follow-up): custom_prometheus_metadata_labels: ["metadata.repo"].
  • No row label: the row comes from end_user.
  • Start after the in-flight stage and refine spend telemetry merges, and reuse its context where it fits.

Target Subsystem

AI & Agentic Review Pipeline (gateway client and LiteLLM gateway configuration).

Existing Tool Considered

LiteLLM custom_prometheus_metadata_labels (configuration of an open tool) and a small client change. Cost: about 200,000 tokens and 3 hours, about 5,000 tokens a month to keep.

Acceptance Criteria

  • The two gateway series carry metadata_repo = the casefolded owner/name for each Service call, and no repo for a call outside a repository scope.
  • The issue states a series limit (repository values x caller names x models), and a query shows count(litellm_spend_metric_total) under it after one day.
  • The pipeline dashboard shows USD by repository.

Dependencies

Source: #4517

Alternatives check of 2026-11-22: in-use.
The repository already uses this tool.
Tool: BerriAI/litellm (https://github.com/BerriAI/litellm), NOASSERTION, last activity 2026-11-22, maintained.
Reason: Searches found no better alternative; the gateway the project already runs documents custom_prometheus_metadata_labels, which turns one request metadata value into a Prometheus label, so it covers the need once the metric-label premise check passes.

Covers:

  • Request metadata written as a Prometheus label on gateway series
  • Setting read from the proxy config already in place
  • Existing spend and token counters per caller, model and team

Lacks:

  • Docs do not state spend and token counters carry the custom label
  • No per-repository label without a client change sending metadata
  • No series limit or cardinality guard for the new label

Proposed design

Problem statement

The gateway spend series show spend by caller and by model. They do not show spend by repository, so a person cannot see what one repository's roadmap work costs. This change sends the casefolded RepoRef.key as the repo entry of the x-litellm-spend-logs-metadata header on each gateway call, using the repository scope that #4576 adds. It adds a spend-by-repository panel to the pipeline dashboard. The gateway label setting and its live proof are in dan-petty/homelab#271 and its draft dan-petty/homelab#430.

Acceptance criteria

Requirement: Repository spend-log metadata

Every gateway call inside a repository scope SHALL send the casefolded RepoRef.key as the repo entry of its x-litellm-spend-logs-metadata header.

Scenario: A call inside a repository scope

  • WHEN A test enters repo_scope("Dan-Petty/DevOps-CLI") and calls gateway_caller_headers().
  • THEN The headers hold x-litellm-spend-logs-metadata with the JSON entry repo equal to dan-petty/devops-cli.

Requirement: No repository outside a scope

A gateway call outside a repository scope MUST send no repo entry.

Scenario: A call with no repository scope

  • WHEN A test calls gateway_caller_headers() with no repository scope set.
  • THEN The headers hold no x-litellm-spend-logs-metadata header, or its value holds no repo entry.

Requirement: Scope ends with its block

The repository scope MUST end with its block, so a later call sends no repo entry.

Scenario: A call after the scope ends

  • WHEN A test leaves a repo_scope block and calls gateway_caller_headers().
  • THEN The headers hold no repo entry.

Requirement: Repository scope of the unit of work

The roadmap unit of work SHALL set the repository scope from the RepoRef of its item before its first gateway call.

Scenario: The scope setup of a unit of work

  • WHEN A test calls the repository scope setup of the roadmap unit of work for dan-petty/devops-cli and calls gateway_caller_headers().
  • THEN The headers hold repo equal to dan-petty/devops-cli.

Requirement: Spend by repository panel

The pipeline dashboard SHALL show gateway spend in USD by repository, grouped by the repo label.

Scenario: The dashboard manifest validates

Requirement: Pending a person after the merge

One day after this change and the merge of dan-petty/homelab#430, a person MUST confirm that the panel lists USD by repository and that count(litellm_spend_metric_total) is under 2,000.

Scenario: The panel and the spend count after one day

  • WHEN A person opens the spend-by-repository panel and runs count(litellm_spend_metric_total) on the gateway one day after both merges.
  • THEN The panel lists USD for each repository that made gateway calls, and the count is under 2,000.

Existing Tool Considered

BerriAI/litellm (https://github.com/BerriAI/litellm), the gateway this repository already runs. The issue's alternatives.block records what the tool lacks. The live check in the issue reply confirms that LiteLLM 1.111.0 puts the repo label on the spend and token counters. This change adds the client header and the dashboard panel; the issue's Existing Tool Considered gives about 200,000 tokens and 3 hours to build, and about 5,000 tokens a month to keep. It builds no replacement for the gateway.

Key questions

  • Which request field carries the repository to the gateway?: The x-litellm-spend-logs-metadata header. It holds a JSON object of spend-log metadata. LiteLLM turns the entry that custom_prometheus_metadata_labels names into a Prometheus label. The live check of LiteLLM 1.111.0 shows the header sets the label, also on a streamed request, and wins over the body field metadata.repo. gateway_caller_headers() sets the headers of every gateway call, so one change covers the OpenAI client, the embeddings client and the pydantic-ai requests. (best practice) [sources: src/devops_cli/ai/client/network.py:65]
    • Chosen: The x-litellm-spend-logs-metadata header in gateway_caller_headers(). One hook covers every gateway client, and the live check of LiteLLM 1.111.0 shows the header sets the label, also on a streamed request.
    • Rejected: The metadata field of each request body. Three request builders change, and pydantic-ai builds its own request. The live check shows the header wins when both are set.
    • Rejected: A suffix on x-litellm-end-user-id. It merges the repository into the caller label and splits the caller series of the dashboard.
  • Which label name and value form does the change use?: The label is repo, with no metadata. prefix. The live check of LiteLLM 1.111.0 shows the gateway key is repo, so the gateway series, the devops_cli_* series and the log label use one name. The body names metadata_repo; read it as repo. The value is the casefolded RepoRef.key, which is owner/name in lowercase. The gateway takes any text as a value, so this client keeps the set closed to that form. (best practice) [sources: src/devops_cli/github/repo_ref.py:139, docs/adr/0022-one-repository-reference-names-the-objects-labels-and-keys-of-each-repository.md]
    • Chosen: The label repo with the casefolded RepoRef.key. One type builds the value, a case variant keeps one series, and the project's other series already use the repo name.
    • Rejected: The label metadata_repo. The live check shows the gateway key is repo, so this name would split the gateway series from the project's other series.
    • Rejected: owner/name as written. A case variant splits the spend of one repository into two series.
  • Where does the client keep the repository value of a call?: The client reads current_repo() from src/devops_cli/telemetry/repo_context.py, the one repository scope that feat(telemetry): name the repository the same way in logs and traces #4576 adds. repo_scope() sets it for a block, and gateway_caller_headers() writes its casefolded RepoRef.key when it is set. A second context variable in network.py would give one process two scopes that can disagree. The value travels with the call, like the request priority and the reply cap. (best practice) [sources: openspec/changes/4576-name-the-repository-the-same-way/proposal.md:9, openspec/changes/4576-name-the-repository-the-same-way/tasks.md:10, src/devops_cli/ai/client/network.py:53]
    • Chosen: Reuse repo_scope and current_repo from src/devops_cli/telemetry/repo_context.py. One repository scope serves the logs, the metrics, the traces and the gateway header, and feat(telemetry): name the repository the same way in logs and traces #4576 already builds it.
    • Rejected: A new context variable in src/devops_cli/ai/client/network.py. It duplicates the repository scope, and a call can then read a different repository from the log line of the same work.
    • Rejected: A parameter on each call. Every layer between the call site and the provider takes the parameter.
  • Which code sets the repository scope in the roadmap job?: The unit of work sets repo_scope from its --repo RepoRef for its run. The unit makes every gateway call of its job, and it already names its repository for the Job labels and telemetry. The Service worker sets the same scope for its rounds (feat(telemetry): name the repository the same way in logs and traces #4576); the router and the sweep launch the unit and make no gateway call. (best practice) [sources: openspec/changes/4576-name-the-repository-the-same-way/tasks.md:37, docs/SERVICE_PATTERNS.md:73, src/devops_cli/github/repo_ref.py:139]
    • Chosen: The unit of work sets the scope for its run. The unit makes every gateway call of its job, and it already holds the RepoRef that labels its metrics.
    • Rejected: The router or the sweep sets the scope. They launch the unit and make no gateway call, so the value would not reach the call.
    • Rejected: Each step sets the scope. A step can miss the scope, and a call would then send no repository.
  • Which dashboard shows spend by repository, and does it need feat(roadmap): a dashboard and alerts for the pipeline #3655?: The panel goes on the pipeline dashboard under k8s/devops/dashboards/. feat(roadmap): a dashboard and alerts for the pipeline #3655 delivers that dashboard. The AI spend dashboard already holds the gateway spend queries. A second panel there would split one cost view across two dashboards. The body names the pipeline dashboard, so feat(roadmap): a dashboard and alerts for the pipeline #3655 stays a dependency. (best practice) [sources: k8s/devops/dashboards/kustomization.yaml:27, tests/fixtures/dashboard_queries/ai-spend.yaml, openspec/specs/ai-spend-pricing/spec.md]
  • Does the gateway config change belong to this repository?: No. This repository holds no environment tree. The environment of the homelab cluster lives only in dan-petty/homelab (ADR 0032). The setting custom_prometheus_metadata_labels and its live proof are the work of dan-petty/homelab#271. Its draft pull request is dan-petty/homelab#430. (best practice) [sources: docs/adr/0032-infrastructure-templates-live-in-four-homes.md:41, CONTEXT.md]
    • Chosen: Keep the gateway config in dan-petty/homelab. This repository ships no environment tree, so no manifest here sets the gateway.
    • Rejected: Change the gateway config here. The gateway config moved to dan-petty/homelab, and a copy here would set nothing.
  • Does the change wait for the in-flight stage and refine spend telemetry item?: It reuses the shared repository scope that feat(telemetry): name the repository the same way in logs and traces #4576 adds, so feat(telemetry): name the repository the same way in logs and traces #4576 is a dependency. The body names no issue number for the stage and refine spend telemetry item; if that item merges first, it uses the same repo_context module and the client still reads current_repo(). The change adds no second context. (best practice) [sources: openspec/changes/4576-name-the-repository-the-same-way/proposal.md:9, openspec/changes/4576-name-the-repository-the-same-way/tasks.md:10]
  • How does this item treat the premise check of dan-petty/homelab#271?: The check is done. The spend and token counters of LiteLLM 1.111.0 carry the label, so the item can proceed. The gateway config is the draft dan-petty/homelab#430. The item starts after that merge. AGENTS.md rule 2 asks for the premise check before the code. (best practice) [sources: AGENTS.md:8]
    • Chosen: Proceed after the merge of dan-petty/homelab#430. The person's check found the label on the counters, so the premise holds.
    • Rejected: Return the item with the evidence. The check found the label, so there is no failed premise to return.
  • What series limit does this change state?: The issue states the limit of count(litellm_spend_metric_total) as 2,000. The live check in the issue reply has 16 spend series now, 6 of roadmap callers, and 64 at most with 9 repositories for the present callers and models. A person runs the query one day after both merges. The limit counts only the spend counter; the count of all gateway series is not this limit, because the label is on 16 metric families and the all-series count is 1,906 now. (best practice) [sources: k8s/devops/dashboards/ai-spend.json:83]
    • Chosen: Limit count(litellm_spend_metric_total) under 2,000 and check it after one day. The live check gives 16 spend series now and 64 at most for the present callers and models, so 2,000 leaves room for new models and callers while keeping the check simple.
    • Rejected: Limit all gateway series under 2,000. The live check shows 1,906 gateway series now and up to 14,600 with the label, so this limit would fail.
    • Rejected: Limit the spend count at 64. 64 covers only the present 9 repositories and 6 roadmap callers; a new model or caller would cross it with no cardinality problem.
  • Which tests prove the header without a gateway?: The tests call gateway_caller_headers() and assert the returned headers. They send no request to a gateway. The LiteLLM 1.111.0 rule on mock_response and allow_client_mock_response does not apply to them. A test that needs a gateway call uses a model with mock_response in the gateway config, or a mock provider. [sources: tests/ai/client/test_gateway_caller_headers.py:54, src/devops_cli/ai/client/network.py:65, docs/TESTING.md]

Fit

Fits in one pull request.

Suspected block

The merge of dan-petty/homelab#430 sets the repo label on the gateway series that the panel and the series check read.

Dependencies

#3655, #4576, #4545, #4533

Activity

  1. added
    source/agent
    on Nov 11, 2026
  2. added
    type/feature
    scope/ai
    status/new
    value/medium
    effort/low
    on Nov 11, 2026
  3. dan-petty commented on Nov 11, 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/feature. The feat prefix of the title gives the type.
    • Scope: scope/ai. The (ai) scope of the title gives the scope.
    • Priority: P2-Medium. Improves cost attribution but does not fix a broken or unsafe behaviour.
    • Value: Medium. Lets a person see per-repository spend, which aids budget decisions.
    • Effort: Low. One context variable, one config line, and a premise check; the body estimates about 3 hours.
    • Related: feat(monitoring): the ai-spend dashboard counts gateway traffic from the gateway's own series, shows failed attempts and names each caller #1668, which asks for a different change. Both improve gateway spend visibility, but #1668 adds dashboard panels for callers and failures while the candidate adds a repo label to the metrics themselves.
  4. added
    priority/p2-medium
    on Nov 11, 2026
  5. devops-cli-service commented on Nov 11, 2026

    @devops-cli-service
    Contributor

    Refine cannot set this item to Ready at 818ac6f while the items below stay open. The next refine round looks at the item again when they close, or when a person replies in a comment or edits the body.

  6. devops-cli-service commented on Nov 22, 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 (8)

    Tools considered:

    Searches run:

    • LiteLLM custom_prometheus_metadata_labels spend metric label, results: 5
    • LLM gateway spend metrics by repository label open source, results: 5
    • BerriAI litellm github prometheus spend metric custom labels, results: 5
    • litellm prometheus metadata labels spend logs metadata, results: 5
    • litellm github repository, results: 5
    • litellm prometheus metrics, results: 5
    • "custom_prometheus_metadata_labels", results: 5
    • LiteLLM spend logs metadata header x-litellm-spend-logs-metadata, results: 5

    Sources:

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

    @devops-cli-service
    Contributor

    Next step: the open items close, or a person replies in a comment or edits the body. See Dependencies in the section Proposed design of the body.

  8. dan-petty commented on Nov 27, 2026

    @dan-petty
    OwnerAuthor

    The premise check of dan-petty/homelab#271 is done, and the gateway part is the draft dan-petty/homelab#430. This item can proceed after that merge. Results for LiteLLM 1.111.0, from its source at the tag v1.111.0 and from a test gateway pod with the config of that pull request:

    • The counters carry the label. litellm_spend_metric_total and litellm_total_tokens_metric_total had the repository of each request. So had 14 more metric families: the request counters, five latency histograms, the deployment counters and the two gauges of the key limits.
    • The label name is repo, not metadata_repo. The gateway key is repo, with no prefix metadata., so the gateway series, the devops_cli_* series and the log label of dan-petty/homelab#270 use one name. The acceptance criterion of this issue names metadata_repo: read it as repo.
    • The header works. A request with x-litellm-spend-logs-metadata: {"repo": "<owner>/<name>"} got the label, also a streamed one. The body field metadata.repo works too, and the header wins when both are set. A value with /, - and . kept its text.
    • A request with no repo gets repo="None". A call outside a repository scope sends nothing.
    • The gateway takes any text as a value. It has no list of allowed values, so this client keeps the set closed: only the casefolded RepoRef.key, which is the value form of the repo label of the devops_cli_* series (9 repository values now).
    • Series. count(litellm_spend_metric_total) is 16 now, 6 of roadmap callers, so 64 at most with 9 repositories for the present callers and models. The limit of under 2,000 is the limit of this count. All gateway series are 1,906 now and pass 2,000 with the label (14,600 at most), because the label is on 16 families.
    • A failed call. litellm_llm_api_failed_requests_metric_total had the repository. litellm_proxy_failed_requests_metric_total and the failed rows of litellm_proxy_total_requests_metric_total had None.
    • Tests against a gateway. LiteLLM 1.111.0 drops a mock_response of a client unless the key or the team allows it (allow_client_mock_response). A test needs a model with mock_response in the gateway config, or a mock provider.

    The check of the series count one day after the client sends the repository stays in this item, with the panel.

  9. devops-cli-service commented on Nov 27, 2026

    @devops-cli-service
    Contributor

    Next step: the open items close, or a person replies in a comment or edits the body. See Dependencies in the section Proposed design of the body.

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/p2-mediumscope/aisource/agentstatus/newtype/featurevalue/medium

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions