Skip to content

feat(ai): record the cost of Jev calls that pass through the gateway #4296

Closed

Description

@dan-petty

Problem or Motivation

The Jev calls (#4289) pass through the gateway as a LiteLLM pass-through (POST /v1/systemone). LiteLLM records no spend for a pass-through that has no cost_per_request, so the spend ledger and the gateway metrics do not count these calls. OpenRouter returns the cost of each call in usage.cost of the answer body. The calls cost about 0.00002 USD each, but the volume can grow when #4282 and #4284 call Jev at each stage run.

Expected

The cost of a Jev call appears in the spend report. Either the client records usage.cost of the answer in the spend ledger, or the pass-through sets a cost for each request. A person or an agent first checks which of the two is the smaller change.

Acceptance criteria

  • A test with the HTTP fake shows that one Jev call adds its cost to the spend ledger.
  • devops ai cost lists the Jev model with a non-zero token count after a call.

Existing tool considered

LiteLLM cost_per_request on pass_through_endpoints (docs: https://docs.litellm.ai/docs/proxy/pass_through) covers a fixed cost; the answer header x-litellm-response-cost covers a variable cost. Not yet checked against the pinned image v1.103.0.

Related

#4289

Proposed design

Problem statement

Jev calls reach OpenRouter through the gateway pass-through POST /v1/systemone. LiteLLM records no spend for a pass-through that has no cost_per_request. The code of this repository records none either: ask_jev, route_with_jev, the intake and the completeness check call DecisionModel.decide directly, which is not an Agent run, so the build_agent spend hook never sees these calls. So the spend ledger (spend.db), devops ai cost and the metrics do not count Jev calls. The volume can grow when #4282 and #4284 call Jev at each stage run.

Acceptance criteria

Requirement: Jev call recorded in the spend ledger

The system SHALL record each Jev decision call in the spend ledger with the model typesafe/jev-1.14, the input and output tokens of the answer, and a cost that is above 0 and priced from the Jev price.

Scenario: One Jev call through the JevFake

  • WHEN a test runs one Jev decision call (for example ask_jev) against the JevFake transport, which answers with 64 input tokens, with a temporary spend ledger and the Jev price set
  • THEN the ledger holds one row for typesafe/jev-1.14 with 64 prompt tokens and a cost equal to 64 input tokens at the Jev price

Requirement: Jev model in the cost report

The command devops ai cost report MUST list the Jev model with a non-zero token count after a Jev call.

Scenario: Cost report after one Jev call

  • WHEN one Jev call has been recorded in a temporary ledger and devops ai cost report --by model runs
  • THEN the report lists typesafe/jev-1.14 with a non-zero token count

Requirement: Failed or unpriced Jev call does not break the call

A Jev call MUST NOT fail or lose its answer when the spend record cannot be written or the answer has no usage.

Scenario: Ledger write fails

  • WHEN the ledger raises while the Jev call is recorded
  • THEN the call returns its typed answer and no exception reaches the caller

Existing Tool Considered

LiteLLM cost_per_request on pass_through_endpoints (https://docs.litellm.ai/docs/proxy/pass_through) covers a fixed cost for each request only. It cannot follow the input-token billing of Jev and it gives no token count, so it cannot meet the acceptance criterion on a token count. The header x-litellm-response-cost was not checked against the pinned image v1.103.0. The repository already has the tools for the client side: track_request_spend (src/devops_cli/ai/spend/ledger.py:926), the PricingRegistry with ai.price_overrides (src/devops_cli/ai/spend/pricing.py:599) and the library pydantic_ai SystemOneModel, which returns the token usage in DecisionResponse.usage. The change builds a thin wrapper around model.decide that calls track_request_spend, plus one price entry (0.042 USD per million input tokens, 0 for output, from k8s/README.md:130). Cost: a small number of tokens to write and test it, and less than one day of elapsed time. No new dependency.

Key questions

  • Which option is the smaller change: the client records the cost of the answer, or the pass-through sets a cost for each request?: The client records the call. Its track_request_spend already writes the ledger and the metrics and prices the call from the tokens. A fixed cost_per_request on the gateway is a config change, but it gives a wrong cost, since Jev bills by input tokens, and no token count. The gateway ledger is also not the ledger that devops ai cost reads. Reading usage.cost needs a custom parse of the HTTP body, because SystemOneModel keeps only the token usage and ignores the extra OpenRouter fields (archived proposal of fix(ai): the gateway has no route for the Jev Decisions API #4289). Pricing the recorded tokens at the known Jev price (0.042 USD per million input tokens, 0 for output) gives the same cost with the existing pricing registry. (best practice) [sources: src/devops_cli/ai/spend/ledger.py:926, src/devops_cli/ai/spend/pricing.py:599, src/devops_cli/ai/jev_ask.py:253, k8s/README.md:130, openspec/changes/archive/2026-11-10-4289-the-gateway-has-no-route-for/proposal.md:30]
    • Chosen: Client records tokens through track_request_spend, priced by the Jev price. It reuses the spend writer, the pricing registry and the report. It gives the token count that the acceptance criteria need. The change is small.
    • Rejected: cost_per_request on the pass-through. It gives a fixed cost that does not follow the input tokens, no token count, and the gateway spend is not the local ledger of devops ai cost.
    • Rejected: Read usage.cost from the answer body. pydantic-ai drops the field, so a custom HTTP parse would replace the library path. The cost is already known from the tokens and the price.
    • Rejected: Read the header x-litellm-response-cost. It is not confirmed for pass-through routes in v1.103.0, and the client path through SystemOneModel does not expose headers.
  • Where does a Jev call reach the spend ledger, and which code holds the Jev calls?: The ledger is written by track_request_spend, which calls SpendLedger.record_request and prices the call with PricingRegistry.calculate_request_cost. The Agent hook in build_agent calls it, but the Jev calls do not run through an Agent: DecisionModel.decide is called directly in src/devops_cli/ai/jev.py (_run_decide), src/devops_cli/ai/jev_ask.py (ask_jev), src/devops_cli/roadmap/intake/intake_jev.py and src/devops_cli/roadmap/jev_completeness.py. So the wrapper goes at these four calls, as one shared function. The model is built by _jev_decision_model in src/devops_cli/ai/pydantic_ai_bridge.py, which sets served_by for a gateway. [sources: src/devops_cli/ai/spend/ledger.py:926, src/devops_cli/ai/spend/ledger.py:468, src/devops_cli/ai/client/factory.py:213, src/devops_cli/ai/jev.py:172, src/devops_cli/ai/jev_ask.py:240, src/devops_cli/roadmap/jev_completeness.py:148, src/devops_cli/roadmap/intake/intake_jev.py:236, src/devops_cli/ai/pydantic_ai_bridge.py:135]
  • How does the Jev call get a non-zero cost, when typesafe/jev-1.14 is no gateway group?: The wrapper passes served_model="typesafe/jev-1.14" with provider gateway, so the ledger treats the call as hosted. Today the rule that does this needs the model to be in CONST_AI_GATEWAY_HOSTED_MODELS, which no longer lists Jev after fix(ai): the gateway has no route for the Jev Decisions API #4289. The price comes from an entry for typesafe/jev-1.14 in ai.price_overrides (0.042 prompt, 0 completion USD per million), which calculate_request_cost reads for a served model. The change sets the entry in k8s/devops/configmap.example.yaml and gives the registry a built-in default, so a workstation also prices it. Whether genai-prices knows this model was not checked; the override makes the price independent of it. (best practice) [sources: src/devops_cli/ai/spend/ledger.py:974, src/devops_cli/ai/spend/pricing.py:621, k8s/devops/configmap.example.yaml:39, k8s/README.md:130, docs/CONFIGURATION.md:206]

Fit

Fits in one pull request.

Suspected block

None.

Dependencies

None.

Alternatives check of 2026-11-11: partly.
Tool: BerriAI/litellm (https://github.com/BerriAI/litellm), NOASSERTION, last activity 2026-11-11, maintained.

Activity

  1. added
    source/agent
    type/feature
    scope/ai
    status/new
    value/medium
    effort/low
    on Nov 9, 2026
  2. dan-petty commented on Nov 9, 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. Cost tracking is incomplete for a growing call volume, but individual calls are cheap and no user is blocked.
    • Value: Medium. Financial visibility for Jev calls improves, but each call costs about 0.00002 USD.
    • Effort: Low. The fix is either reading usage.cost into the ledger or setting cost_per_request on the pass-through, both small changes.
    • Related: fix(ai): a hosted gateway call is recorded as a local call at $0 #3656, which asks for a different change. Both address the spend ledger not capturing cost for a class of gateway calls, but #3656 fixes misclassification of hosted calls as local, while the candidate adds a missing cost value for pass-through calls.
  3. added
    priority/p2-medium
    on Nov 9, 2026
  4. added
    status/ready
    and removed
    status/new
    on Nov 10, 2026
  5. devops-cli-service commented on Nov 10, 2026

    @devops-cli-service
    Contributor

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

  6. devops-cli-service commented on Nov 11, 2026

    @devops-cli-service
    Contributor

    Alternatives check: partly

    Reason: LiteLLM, the gateway the Jev calls already pass through, can record spend for a pass-through route through a flat cost_per_request, and its current pass-through docs also describe a target reporting its own cost and usage, so it covers the gateway side of the need. It does not cover the rest: a flat cost gives no token count, and LiteLLM writes its own spend store, not the repository's spend.db that devops ai cost report reads, so the Jev model row and the token count still need the client-side record.

    Tool: BerriAI/litellm (https://github.com/BerriAI/litellm)

    • License: NOASSERTION
    • Last activity: 2026-11-11
    • Maintained: yes

    Covers:

    • A pass_through_endpoints route can set a flat cost_per_request, so LiteLLM's gateway spend tracking and budgets count the pass-through call
    • Current LiteLLM pass-through docs describe a target that reports its own cost and usage for routes that fan out to several models
    • The spend is recorded in LiteLLM's own gateway ledger, with per-key budgets and spend tracking

    Lacks:

    • A fixed cost_per_request gives no token count, so the acceptance criterion of a non-zero Jev token count is not met
    • The gateway spend store is not the repository's spend.db, so devops ai cost report would not list typesafe/jev-1.14
    • Target-reported cost and usage is not confirmed for the pinned image v1.103.0
    • The client path through SystemOneModel exposes no response headers, so x-litellm-response-cost is not readable there

    Tools considered:

    Searches run:

    • LiteLLM pass_through_endpoints cost_per_request spend tracking, results: 5
    • OpenRouter usage.cost track spend proxy gateway pass-through, results: 5
    • "litellm" "x-litellm-response-cost" header, results: 5
    • litellm docs pass_through cost_per_request, results: 5
    • litellm pass-through cost and usage tracking docs, results: 5
    • BerriAI litellm github pass through endpoint spend, results: 5
    • LiteLLM proxy spend tracking, results: 5
    • OpenRouter API usage cost field, results: 5

    Sources:

    Checked on 2026-11-11. A person decides what this means for the item. The check changes no label and closes no issue.

  7. dan-petty commented on Nov 12, 2026

    @dan-petty
    OwnerAuthor

    A blocker for any net gain of Jev (2026-11-12)

    The decision-model study found that no decision point can show a net gain until this item is built. ADR 0020 section 8 measures the benefit of each point, and the cost is one half of that measure.

    • litellm_spend_metric_total{model="typesafe/jev-1.14"} is 0 on every series, and the token counters of the model are 0 (21 days).
    • The gateway counted about 3,373 Jev requests in 7 days on /v1/systemone, with 0 failures and p95 0.40 s. So the calls occur, but they have no cost record.
    • devops ai ask-jev and the MCP tool ai_ask_jev write no metric, no log line and no cost record.
    • feat(roadmap): report the live shadow bar of the command gate from Loki #5208 (the live shadow report of the command gate) depends on this item.

    The study ranks this item as step 5 of 9: medium gain. Jev calls are fast and cheap, and the value is in the work that they let a job skip. Without the cost, that value cannot be shown.

  8. dan-petty commented on Nov 12, 2026

    @dan-petty
    OwnerAuthor

    Scope note from the decision-model docs review of 2026-11-12. The owner approved it on 2026-11-12. One cause gives this item and #5209.

    The documents say:

    The shared cause: SystemOneModel reports the provider system-one, which genai-prices 0.2.3 does not know. So the price lookup fails (LookupError), and the window lookup of #5209 gives None. With provider openrouter, genai-prices gives 0.042 USD for 1M input tokens and a window of 32000. SystemOneModel also drops usage.cost and the response id that OpenRouter returns in every response (pydantic_ai/models/system_one.py:242-246 in 2.59.0). It keeps only input_tokens and output_tokens.

    Change to this item:

    1. Record DecisionResponse.usage for each call, and price it with genai_prices.calc_price(provider_id='openrouter') through src/devops_cli/ai/spend/pricing.py. This gives the same 0.042 USD price with no hand-written override.
    2. Delete the two hand-written Jev price constants (src/devops_cli/config/constants.py:2874,3821). Only two eval modules read them.
    3. File an upstream pydantic-ai issue, so that SystemOneModel keeps usage.cost and the response id.
    4. The byte budget of about 32,000 bytes, which keeps requests out of the long-context mode, is in fix(ai): set the Jev context window from the model data #5209. If refactor(ai): ask Jev through Agent output types #5282 moves the calls to the Agent path first, the call gives RunUsage and the spend hook of build_agent sees it.
  9. added
    status/in-progress
    and removed
    status/ready
    on Nov 15, 2026
  10. added
    status/in-review
    and removed
    status/in-progress
    on Nov 15, 2026
  11. removed
    status/in-review
    on Nov 15, 2026
  12. devops-cli-service commented on Nov 15, 2026

    @devops-cli-service
    Contributor

    Live since 2026-11-15 23:51 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/p2-mediumscope/aisource/agenttype/featurevalue/medium

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions