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-27: in-use.
The repository already uses this tool.
Tool: BerriAI/litellm (https://github.com/BerriAI/litellm), NOASSERTION, last activity 2026-11-28, maintained.
Reason: Searches found LiteLLM, the gateway already in use, documents spend and token counters with caller and model labels and supports custom metadata labels; only the client header and dashboard panel remain.

Covers:

  • Request metadata becomes a Prometheus label on gateway metrics
  • Spend and token counters already carry caller and model dimensions
  • One OpenAI-compatible gateway serves chat, embeddings and agent calls

Lacks:

  • No repository value until the client sends request metadata
  • No built-in spend-by-repository dashboard panel
  • No cardinality limit on custom label values

Proposed design

Problem statement

The gateway spend series show spend by caller and by model. They do not show spend by repository. This change sends the casefolded RepoRef.key as the repo entry of the LiteLLM spend-log metadata header from gateway_caller_headers(). 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: 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.

Scenario: The panel after one day

  • WHEN A person opens the spend-by-repository panel one day after both merges.
  • THEN The panel lists USD for each repository that made gateway calls.

Existing Tool Considered

BerriAI/litellm (https://github.com/BerriAI/litellm), verdict in-use. The issue's alternatives block records what the tool covers and lacks. This change adds the client repo metadata and the dashboard panel. It builds no replacement for the gateway. The series limit stays in dan-petty/homelab#271.

Key questions

  • Which request metadata carries the repository to the gateway?: The LiteLLM spend-log metadata header, x-litellm-spend-logs-metadata, with the JSON entry repo. gateway_caller_headers() adds it, so every gateway client sends it. (best practice) [sources: src/devops_cli/ai/client/network.py:64]
    • Chosen: Use the x-litellm-spend-logs-metadata header in gateway_caller_headers(). One hook covers every gateway client. The issue asks for request metadata from gateway_caller_headers.
    • Rejected: Set the metadata field in each request body. Three request builders change, and pydantic-ai builds its own request.
    • Rejected: Add a suffix to x-litellm-end-user-id. It merges the repository into the caller label and splits the caller series.
  • Which label name and value form does the change use?: The label is repo. The value is the casefolded RepoRef.key, which is owner/name in lowercase. (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: Use repo with the casefolded RepoRef.key. One type builds the value, and the casefolded form keeps one series for one repository.
    • Rejected: Use metadata_repo. The gateway key is repo, so this name would split the gateway series from the project's other series.
    • Rejected: Use the owner/name spelling as written. A case variant splits the spend of one repository into two series.
  • Where does the client read the repository of a call?: The client reads current_repo() from src/devops_cli/telemetry/repo_context.py. The shared repo_scope sets it. The client writes no second context variable. (best practice) [sources: src/devops_cli/telemetry/repo_context.py:21]
    • 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. feat(telemetry): name the repository the same way in logs and traces #4576 builds it.
    • Rejected: Add a new context variable in src/devops_cli/ai/client/network.py. It duplicates the repository scope. A call can then read a different repository from the log line of the same work.
    • Rejected: Add a parameter on each call. Every layer between the call site and the provider takes the parameter.
  • Which code sets the repository scope for a roadmap job?: The roadmap unit of work sets repo_scope from its --repo RepoRef for its run. The router and the sweep make no gateway call. [sources: docs/SERVICE_PATTERNS.md:73]
  • Which dashboard holds the spend-by-repository panel?: The pipeline dashboard under k8s/devops/dashboards/, after feat(roadmap): a dashboard and alerts for the pipeline #3655 merges. The AI spend dashboard already holds the gateway spend queries, so a second panel there would split one cost view. (best practice) [sources: k8s/devops/dashboards/kustomization.yaml:27, tests/fixtures/dashboard_queries/ai-spend.yaml]
  • Does the gateway label setting belong to this repository?: No. The environment tree lives in dan-petty/homelab (ADR 0032). The setting and its live proof are in dan-petty/homelab#271 and its draft 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 in this repository. The gateway config moved to dan-petty/homelab. A copy here would set nothing.

Fit

Fits in one pull request.

Suspected block

The premise check in dan-petty/homelab#271 and the merge of its draft dan-petty/homelab#430 set the repo label on the gateway series that the panel reads.

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.

  10. devops-cli-service commented on Dec 1, 2026

    @devops-cli-service
    Contributor

    Owner question: See Suspected block in the section Proposed design of the body.

    Options

    1. Decide the question: The job refines the item with your answer.
    2. Leave it to the job: The job records the best-practice answer and goes on.

    Recommendation: Decide the question: only a person can answer it.

    Evidence

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