Skip to content

feat(pricing)!: a configured price source is tried before LiteLLM's map, and a listed service tier is priced at its own rates - #962

Open
earakely-scale wants to merge 9 commits into
mainfrom
edgararakelyan/price-source-seam
Open

earakely-scale wants to merge 9 commits into
mainfrom
edgararakelyan/price-source-seam

Conversation

@earakely-scale

@earakely-scale earakely-scale commented Nov 30, 2026 •

Copy link
Copy Markdown
Collaborator

Every cost is priced from the price map one LiteLLM release bundles. That leaves two gaps:

  • Missing models. A model the map doesn't list stays unpriced, or takes its agent's own figure, until a LiteLLM release, a framework bump and a deployment's pin all catch up.
  • Wrong rates. Some of the map's rates differ from what a provider charges, with nothing to correct them.

This adds a seam for a price source that a deployment configures, tried before the map. The map stays the in-tree default and the fallback, so a bare install prices exactly as before.

Change

  • [model_prices] (named for model usage, so sandbox prices can take a table of their own later) names a PriceSource (impl = "module:Class", [model_prices.config]), built with from_config(**config) as [runner] is. It's a plain config table with no environment variable. config show reports it, owned by config.runtime.
  • PriceSource.listed(model), the one method a source implements, gives a model's ModelPrice and the source's own name for cost_source (Listed), or None for a model it doesn't list. It gets the name as called, routing prefix and all, and matches it to its own ids. The name carries a version that changes whenever its prices do.
    • PriceTable, LiteLLM's map, is a PriceSource, so every caller that passes a table works unchanged.
    • PriceChain tries sources in order.
  • configured_prices() gives the configured source chained before litellm_prices(), or the map alone without the table. The config builds and caches it once (Config.get_model_prices()).
    • StepUsage, which also prices judges' calls, and model_call.complete both read it. An embedder that prices usage should too.
  • Order in price: an endpoint's bill, then the configured source, then the map, then an agent's own figure, then unpriced. The agent's figure stays in reported_cost_usd.
  • Failures never fail a step.
    • A source that can't be built (an import error, or a config it can't take) leaves the map pricing, with one warning.
    • A source whose lookup raises is skipped for the next, with one warning. The config caches the chain, so the warning comes once however often usage is priced.
    • Fetching and refreshing are the source's own, off the pricing path.
  • Refreshing. A step is priced at the rates its source lists when the step's last turn is recorded, so a refresh during a step moves that step's earlier turns, never a finished step's.
  • Finding a model in the map. PriceTable.lookup tries the name as called, then without its first segment, then each of its segments with its last, in order, then its last alone. So vertex_ai/global/gemini-3.1-pro-preview, and the same route behind a proxy's openai/ prefix, find vertex_ai/gemini-3.1-pro-preview. Before, they found nothing, and every such call went unpriced.
  • Entries priced from several sources. A turn whose models were priced from different sources (one from the catalog, one from the map) names no cost_source on its entry. Each model row names its own.
  • Service tier rates. ModelPrice.service_tiers holds a source's rates by tier, keyed by the names providers give it in any case (one tier may be listed under several, as a tier name and a speed). Each tier's rates are complete in themselves; nothing is taken from the standard ones.
    • Tokens whose every non-standard tier and speed names one listed tier are charged its rates, with no mark.
    • Otherwise they're charged the standard rates, marked standard_service_tier, as before.
    • An entry is charged at the tier it's labelled with. Tokens keeps the agreed service_tier and speed, combined by agreed_service_level as the entry's label is: a standard turn plus a priority turn is mixed (standard rates, marked), and a turn that reported no tier doesn't disagree.
    • LiteLLM's map lists them too, for 396 models (input_cost_per_token_priority, _flex, _balanced, _ultrafast), and PriceTable now reads them as LiteLLM bills them: a rate a tier's keys don't give is the standard one, past a long-context tier too; writes kept a while are charged the standard rates; the fast speed is charged priority's. So OpenAI and Gemini priority and flex calls priced from the map are exact with no source configured.
    • Checked against LiteLLM's own cost_per_token(service_tier=...) for every tiered model of the installed map, under and past the long-context tier: 625 match. The exceptions are LiteLLM's, not the map's: its Fireworks and Bedrock Kimi cost paths ignore the tier (they charge the standard rate while the map lists a priority one, which we charge), and it can't route the gemini-*-latest aliases.
  • Breaking, hence the !: ModelPrice.tiers (long-context rates) is now ModelPrice.context_tiers, so it can't be mistaken for service_tiers. The plugin-API check counts it because ModelPrice joins the plugin surface here. The hub builds ModelPrice positionally and never reads .tiers, so nothing outside agent-env changes.
  • Plugin surface: PriceSource joins BASES. Listed, ModelPrice, ContextTier, PriceTable, configured_prices and litellm_prices join USED.
  • Docs: AGENTS.md's pricing paragraph names the seam, the order and the fallback, and the example config shows a commented [model_prices].

Flaky test fix folded in

test_a_compose_of_chromium_from_a_release_record_pulls_its_image_and_the_browser_boots_from_it failed once on this PR (slow shard 7), with KeyError: 'published'.

  • Cause: earlier in the shard, the GHCR lookup of the browser base's published image timed out (ReadTimeout, 10 s). published_digest gave None, so that compose built the base itself, under another key and other lock inputs, and left the shared chromium env composed that way. The test's release record came from that compose, and its fresh compose, which reached GHCR, didn't match it.
  • Fix: _registry_digest tries a lookup that couldn't reach the registry once more. A missing tag (NoSuchImageError) is still asked once.
  • Diagnosis: the test now names both sides' lock inputs and keys when they differ, instead of a KeyError.
  • Test: tst/unit/env/published_images_test.py::test_a_lookup_that_cant_reach_the_registry_is_tried_once_more, which fails without the retry.

Not in this PR

A concrete source lives with the deployment that configures it.

Testing

  • tst/unit/config/test_price_sources.py, with a stub source in tst/util/price_sources.py:
    • Without the table, the map prices alone.
    • With it, a listed model is priced from the source and its cost_source names it. A model the source lacks falls to the map, then to the agent's figure, then to unpriced.
    • An endpoint's bill still comes first.
    • A raising source leaves the map pricing, with one warning however often usage is priced.
    • A refresh during a step moves that step's earlier turns to the new rates, and a finished step keeps its cost.
    • A source that can't be built leaves the map alone, with one warning.
    • A prompt step's entry and a judge's entry are priced from the source.
  • tst/unit/config/test_model_call.py: complete prices a model the map lacks from the configured source.
  • tst/unit/config/test_model_pricing.py:
    • A listed tier is priced at its rates, whether named by tier, by speed, in any case, or by both naming one tier.
    • An unlisted tier, mixed, or two names for different tiers keep the standard rates and the mark.
    • Standard tokens ignore the tiers.
    • A source may name its tiers in any case.
    • The map's tiers are read as LiteLLM bills them (synthetic map), and one call at each of GPT's priority, flex and fast and Gemini's priority and flex costs what LiteLLM's cost_per_token says, under and past the long-context tier.
    • A standard turn plus a priority turn is mixed, charged standard rates and marked. An unreported turn plus a priority turn, or two priority turns, get priority rates. Two spellings of standard stay standard.
  • tst/unit/task_step/usage_models_test.py: an entry whose models were priced from two sources names neither, and each row names its own.
  • Each review fix's test fails without its fix.
  • tst/unit/config/test_model_pricing.py: a routed name finds its provider's entry before the bare one (region route, behind a proxy route, first prefix, last segment), and the installed map prices vertex_ai/global/gemini-3.1-pro-preview.
  • tst/unit/config/test_describe.py: the full-surface config reports [model_prices].
  • Tests that patched litellm_prices where StepUsage read it now patch configured_prices.
  • 13,660 tests pass in the touched areas. The four computer_exec system-command tests that fail locally fail the same way on main on macOS (BSD base64 flags).
  • ruff is clean, and check_plugin_api.py reports no break.

For the docs site

  • Run costs are priced from a configured source first, then LiteLLM's bundled map, then the agent's figure. cost_source names the one that priced each cost.
  • A recorded cost is never re-priced, so history keeps its sources.
  • A non-standard service tier is priced at its own rates where the source lists it, and is otherwise marked standard_service_tier.

🤖 Generated with Claude Code

Confidence Score: 5/5 · The PR appears safe to merge; no blocking issue remains from this review.RetriggerView in GreptileConfidence Score: 5/5

The PR appears safe to merge; no blocking issue remains from this review.

Summary

Adds configurable model prices before LiteLLM’s bundled map, supports listed service-tier rates, and expands lookup for routed model names.

  • A configured price source can price model usage before LiteLLM’s map.
  • Usage on a listed service tier is priced at that tier’s rates.
  • Published-image lookup retries once when the registry cannot be reached.

Diagram

%%{init: {'theme': 'neutral'}}%%
flowchart TD
  A[Usage and model name] --> B{Endpoint reported a bill?}
  B -->|Yes| C[Keep endpoint bill]
  B -->|No| D{Configured source lists model?}
  D -->|Yes| E[Use listed rates]
  D -->|No or lookup fails| F{LiteLLM map lists model?}
  F -->|Yes| E
  F -->|No| G{Agent reported a cost?}
  G -->|Yes| H[Keep agent figure]
  G -->|No| I[Record as unpriced]
Loading

Reviews (14) · Last reviewed commit: "feat(pricing): the map finds a model cal..." · Reviewed by Greptile

…p, and a listed service tier is priced at its own rates

`[prices]` names a `PriceSource` (`impl`, `config`, built with `from_config`), and `configured_prices` tries it before
the price map the installed LiteLLM bundles, which prices alone without the table, as before. A source that can't be
built, or that raises, leaves the map pricing with a warning and never fails a step. Prompt steps, judges and core's
own calls all price through `configured_prices`. `ModelPrice.service_tiers` holds a source's rates by service tier, and
tokens off the standard tier are charged their tier's rates where it's listed, keeping `standard_service_tier` only
where it isn't.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@earakely-scale
earakely-scale requested a review from a team as a code owner November 30, 2026 05:43
Comment thread src/agent_env/config/model_pricing.py Resolved
Comment thread src/agent_env/task_step/usage.py Resolved
Comment thread src/agent_env/config/model_pricing.py Resolved
Comment thread src/agent_env/task_step/usage.py Resolved
@earakely-scale

Copy link
Copy Markdown
Collaborator Author

End to end

Real agent-env runs on this PR's head (339cd42), on a Linux host with local Docker sandboxes and a real LiteLLM gateway. [prices] named a small file-backed source written for the test (FilePrices, rates from a JSON file, cost_source file-prices@<digest>), listing gemini-3.1-pro-preview, claude-haiku-5-5, and gpt-5.4-mini with a priority tier at twice its standard rates.

Run Who prices it Entry
Gemini on vertex_ai/global/gemini-3.1-pro-preview, graded by a rubric judge on the same model the source (LiteLLM's map lacks it) solver $0.039116 and judge $0.00204, both file-prices@278f4ed1cbf3; bound exact
OpenCode on gpt-5.4-mini, every call asking for the priority tier the source, at its priority rates service_tier: priority, no mark, bound exact; $0.0176022 = the counts at the priority rates (standard would be $0.0088011)
Claude Code on claude-haiku-5-5 (both list it) the source, before the map file-prices@…; the file's rates are 1.2× the map's, and so is the cost
OpenCode on gpt-4.1-mini (only the map lists it) the map litellm@1.106.6
Chaos: the source's file unreadable, so every lookup raises the map both runs passed, one "Price source FilePrices failed" warning each; Haiku from litellm@1.106.6, Gemini no_price as without the table
Chaos: [prices] naming a class that doesn't import the map both runs passed, one "[prices] can't be built" warning each; same costs as without the table
  • Judge routing. The judge first ran on vertex_ai/global/..., which LiteLLM sends to Vertex directly rather than through the configured endpoint, and it failed for want of Google credentials. That's the existing rule for a provider-prefixed judge model, not this change. Run again as openai/vertex_ai/global/..., it went through the gateway and was priced from the source.
  • Cleanup. Every sandbox was torn down. Model spend was about $0.15.

…s, warn once per source

- A sum of turns at the standard level and another is charged the standard rates, marked standard_service_tier,
  rather than every turn at the other tier's rates (Tokens.some_standard).
- A step prices all its turns from the prices as they stood when it started (PriceSource.pinned), so a source that
  refreshes never reprices a turn already recorded.
- The config caches the chain of its source and the map, and pinned copies share its warn state, so a source that
  raises warns once.
- An entry whose models were priced from several sources names none in cost_source; each row names its own.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@earakely-scale

Copy link
Copy Markdown
Collaborator Author

End to end, review fixes

Real agent-env runs on 96302033a, on the same Linux host and kit as before: a file-backed source configured through [prices], and a logging pass-through in front of the model gateway that records each call's tokens and service tier.

Run Gateway truth Ledger entry
OpenCode on gpt-5.4-mini, only the first call asks for priority 4 calls: 1 priority, 3 default service_tier: mixed, $0.0079641 at the standard rates, marked standard_service_tier, bound estimated
OpenCode on gpt-5.4-mini, every call asks for priority 4 calls, all priority service_tier: priority, $0.0058248 at the priority rates, no mark, bound exact
Gemini CLI solver plus a direct judge, the source's file unreadable 3 calls Both steps priced, both unpriced / no_price (the map lacks the model), the run completed, and the log has one Price source FilePrices failed warning for the two steps

Both costs match a hand calculation from the gateway's token counts at the file's rates. Before this fix, the first row would have charged all four calls at priority rates, with no mark.

Pinning has no effect on a source that reads its file once, so the unit test covers it (a source that refreshes between two turns of a step).

Every sandbox was removed and the host is stopped. Model spend was about $0.011.

earakely-scale and others added 2 commits November 29, 2026 22:27
… couldn't be reached

A compose that gets no digest for its base's published image builds the base itself, under another key. One registry
timeout during a shard's first chromium compose left the shared env composed that way, so
test_a_compose_of_chromium_from_a_release_record's record named locks a later, normal compose didn't match. A lookup
that couldn't reach the registry is now tried once more (a missing tag still isn't), and the test names both sides'
lock inputs and keys when they differ.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…lace for the map fallback

- Tokens keeps the agreed service_tier and speed, combined by agreed_service_level as an entry's are, in place of
  levels and some_standard: a sum of a standard turn and a priority one is mixed (standard rates, marked), and a turn
  that reported no tier doesn't disagree, so the label and the price can't differ.
- Config.get_prices() gives the chain or the map alone; configured_prices() reads it.
- RoleUsage.entry() takes its prices, with no empty-table default.
- The published-image lookup reads one digest from either try.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@earakely-scale

Copy link
Copy Markdown
Collaborator Author

End to end, after the tier refactor

The same runs on a6a90ee99, where an entry is priced at the tier it's labelled with (agreed_service_level):

Run Gateway truth Ledger entry
OpenCode on gpt-5.4-mini, only the first call asks for priority 4 calls: 1 priority, 3 default service_tier: mixed, $0.0077211 at the standard rates, marked, bound estimated
OpenCode on gpt-5.4-mini, every call asks for priority 3 calls, all priority service_tier: priority, $0.0137856 at the priority rates, no mark, bound exact
Gemini CLI solver plus a direct judge, the source's file unreadable 3 calls Both steps unpriced / no_price, the run completed, one Price source FilePrices failed warning

Both costs match a hand calculation from the gateway's token counts at the file's rates. The unit tests cover a priority turn next to a turn that reported no tier, which no agent here produces.

Every sandbox was removed and the host is stopped. Model spend was about $0.015.

… sit beside it

[prices] becomes [model_prices] and Config.get_prices() becomes get_model_prices(), leaving room for sandbox prices
in a table of their own. No release read [prices], so nothing else changes.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@earakely-scale

Copy link
Copy Markdown
Collaborator Author

End to end, after the table rename

The same runs on 51b92f8ea, the file source configured as [model_prices]:

Run Gateway truth Ledger entry
OpenCode on gpt-5.4-mini, only the first call asks for priority 3 calls: 1 priority, 2 default mixed, $0.00724305 at the standard rates from file-prices@278f4ed1cbf3, bound estimated
OpenCode on gpt-5.4-mini, every call asks for priority 4 calls, all priority priority, $0.0165267 at the priority rates, bound exact
Gemini CLI solver plus a direct judge, the source's file unreadable 3 calls Both steps no_price, the run completed, one Price source FilePrices failed warning

Both costs match a hand calculation from the gateway's counts. config show on a config with the old [prices] table warns that agent-env doesn't read it.

Every sandbox was removed and the host is stopped. Model spend was about $0.016.

…tiers in any case

- PriceSource.pinned() is gone: a step is priced at the rates its source lists when the step's last turn is recorded,
  so a refresh during a step moves that step's earlier turns, never a finished step's.
- Core lower-cases a source's service tier names when it looks one up; each tier's rates are complete in themselves.
- The docs say what a source matches (the name as called, routing prefix and all) and that its cost_source name
  carries a version that changes whenever its prices do.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@earakely-scale

Copy link
Copy Markdown
Collaborator Author

End to end, after the interface changes

The same runs on deb4bcc13. The file source now lists the priority tier as Priority and no longer lower-cases tier names itself, so core's lookup does it.

Run Gateway truth Ledger entry
OpenCode on gpt-5.4-mini, only the first call asks for priority 3 calls: 1 priority, 2 default mixed, $0.0071943 at the standard rates, bound estimated
OpenCode on gpt-5.4-mini, every call asks for priority 3 calls, all priority priority, $0.0146586 at the Priority tier's rates, no mark, bound exact
Gemini CLI solver plus a direct judge, the source's file unreadable 3 calls Both steps no_price, the run completed, one Price source FilePrices failed warning

Both costs match a hand calculation from the gateway's counts. The source named itself file-prices@d85c4686b73d, a new digest once its file changed, as cost_source names should.

Every sandbox was removed and the host is stopped. Model spend was about $0.015.

…ce_tiers

- ModelPrice.tiers becomes context_tiers (and _tier_rates _context_tier_rates), so the long-context steps and the
  service tiers can't be mistaken for each other; a service tier's own service_tiers are never read, as its docstring
  now says.
- Config.get_model_prices() and its cache are typed PriceSource.
- The price source tests build the stub's config with _stub(fail=...) rather than editing its text.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@earakely-scale earakely-scale changed the title feat(pricing): a configured price source is tried before LiteLLM's map, and a listed service tier is priced at its own rates feat(pricing)!: a configured price source is tried before LiteLLM's map, and a listed service tier is priced at its own rates Nov 30, 2026
@earakely-scale

Copy link
Copy Markdown
Collaborator Author

End to end, after the context_tiers rename

The same runs on f25090304, plus one priced from LiteLLM's map, since the rename touches the map's loader:

Run Gateway truth Ledger entry
OpenCode on gpt-5.4-mini, only the first call asks for priority 3 calls: 1 priority, 2 default mixed, $0.0074928 at the standard rates, bound estimated
OpenCode on gpt-5.4-mini, every call asks for priority 3 calls, all priority priority, $0.0146031 at the Priority tier's rates, bound exact
OpenCode on gpt-4.1-mini, which only the map lists 3 calls, default litellm@1.106.6, $0.0041372 at the map's rates, bound exact
Gemini CLI solver plus a direct judge, the source's file unreadable 3 calls Both steps no_price, the run completed, one Price source FilePrices failed warning

All three costs match a hand calculation from the gateway's counts.

Every sandbox was removed and the host is stopped. Model spend was about $0.019.

…ts for them

The map lists priority, flex, balanced and ultrafast rates for hundreds of models (input_cost_per_token_priority and
the like), which the loader skipped, so a priority call priced from the map took the standard rates. PriceTable now
reads them into ModelPrice.service_tiers as LiteLLM bills them: a rate a tier's keys don't give is the standard one,
past a long-context tier too; writes kept a while are charged the standard rates; the fast speed is charged
priority's. One call at each tier is priced as LiteLLM's own cost_per_token prices it, under and past the long-context
tier, for GPT and Gemini models of the installed map.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@earakely-scale

Copy link
Copy Markdown
Collaborator Author

End to end, LiteLLM's map pricing service tiers

Real agent-env runs on a3947d6e2. To have the map price alone, [model_prices] names the file source with its file unreadable, so every lookup falls to LiteLLM's map:

Run Gateway truth Ledger entry
OpenCode on gpt-5.4-mini, every call asks for priority 4 calls, all priority priority, $0.0062478 at the map's priority rates ($1.50 / $9.00 / $0.15 per million), litellm@1.106.6, bound exact
OpenCode on gpt-5.4-mini, only the first call asks for priority 3 calls: 1 priority, 2 default mixed, $0.0026826 at the map's standard rates, marked standard_service_tier, bound estimated

Before this change, the first run would have been charged the standard rates, about half, and marked.

The file-source runs on the same commit came out as before: mixed $0.00718005 estimated, priority $0.0064818 exact, and gpt-4.1-mini from the map $0.0041452 exact. Two gpt-4.1-mini runs asked for priority, but the gateway served default, and the ledger priced them at the standard rates. The broken-source run gave one warning.

All costs match a hand calculation from the gateway's counts. Every sandbox was removed and the host is stopped. Model spend was about $0.025.

…route

PriceTable.lookup tried the name as called and without its first segment, so vertex_ai/global/gemini-3.1-pro-preview
(and the same route behind a proxy's openai/ prefix) found nothing, though the map lists vertex_ai/gemini-3.1-pro-preview
and gemini-3.1-pro-preview, and every such call went unpriced. It now tries each of the name's segments with its last,
in order, and then its last alone, so the route's provider prices it where the map lists one.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@earakely-scale

Copy link
Copy Markdown
Collaborator Author

End to end, a model called by a provider and region route

The Gemini runs that came out no_price before, on 2425d28ee. [model_prices] names the file source with its file unreadable, so LiteLLM's map prices them alone:

Run Model as called Ledger entry
Gemini CLI solver vertex_ai/global/gemini-3.1-pro-preview $0.039214 from litellm@1.106.6 (vertex_ai/gemini-3.1-pro-preview, $2.00 / $12.00 per million), bound exact
its direct judge openai/vertex_ai/global/gemini-3.1-pro-preview $0.00198, same entry, bound exact
Gemini CLI solver alone vertex_ai/global/gemini-3.1-pro-preview $0.038866, bound exact

Before, these three entries were unpriced / no_price. Their token counts match the gateway's (19,829 for the judged run, 19,163 for the other), and the costs match a hand calculation at the map's Vertex rates.

Every sandbox was removed and the host is stopped. Model spend was about $0.08.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant