Skip to content

feat(protocol): Usage gives a turn's counts by model, and the host prices each model at its own rate - #887

Merged
earakely-scale merged 5 commits into
mainfrom
edgararakelyan/fd6744-per-model-pricing
Nov 23, 2026
Merged

earakely-scale merged 5 commits into
mainfrom
edgararakelyan/fd6744-per-model-pricing

Conversation

@earakely-scale

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

Copy link
Copy Markdown
Collaborator

What changes

The first half of P16 (FD-6744): the protocol gains a turn's counts by model, and the host prices each model at its own rate. Today the host prices a role's whole turn at the role's one model. A Claude Code turn with a Haiku subagent under a Sonnet solver, or a Gemini turn with side calls on flash-lite, is therefore priced at the solver's rate, and the cost is off with nothing marking it. The agents that fill the breakdown come in the second PR, after the release that publishes this protocol, since agent images install the protocol from PyPI.

  • Protocol.
    • Usage.models is a list of ModelUsage, one per model the turn's calls used. Each row has the model's name as called, its calls, its token and cache counts (read as Usage's are, with its own input_includes_cache), and the agent's own cost_usd for them when it has one.
    • The rows cover every call the turn counted. A call the agent can't name goes under the model it was configured with.
    • Usage validates the rows like its own counts, to_dict writes them only when there are some, and from_dict reads them from mappings. An older reader ignores the key.
    • A count the turn leaves out that every row gives is the rows' sum, and so is the includes-cache flag the rows agree on. A turn can give only its rows. The host counts any turn with rows. A row it can't price, such as one missing its output count, leaves the entry unpriced rather than dropping the turn.
  • Host.
    • RoleUsage.entry prices each model's counts at that model's map rate. A turn with no breakdown counts toward the role's model.
    • The step's entry keeps a row per model (models), with the row's counts and its cost fields (cost_usd, cost_status, cost_source, unit_prices, uncached_input_tokens).
    • The entry's cost_usd is the rows' sum. Since no single rate applies, the entry has no unit_prices or uncached_input_tokens of its own.
    • An entry whose turns give no breakdown keeps today's shape exactly.
  • The echo test agent reports a breakdown when a prompt line starts with usage-models, so the integration test can send one over A2A.

Decisions (made with Edgar, 2026-11-23)

  1. A typed field on Usage, not a provider_details convention. That's why this ships as two PRs with a release between them.
  2. The breakdown covers the whole turn. The host prices from the rows alone and never subtracts.
  3. The entry gets a row per model, and its total is their sum.
  4. The second PR fills the breakdown for Claude Code, Gemini, the proxy and Hermes, plus OpenCode (through the shared relay) and Codex if it can tell its subagents' models.

Also in this PR, how the entry is costed:

  • A model the map lacks leaves the entry unpriced (no_price), as today. Each row keeps its own cost, so the other models' costs stay visible.
  • A row priced from the agent's own figure (the map lacks the model but the agent reported a cost for it) makes the entry reported from harness, since the entry is only as sure as its least sure row. The total is still the sum.
  • An agent's figure stands in for the map only when it's complete: every turn, or every share of a model, must have carried one. A sum of some turns' figures isn't the cost, so a model the map lacks is then unpriced. That also changes the single-model path, where until now only endpoint bills had to be complete: a role on a model the map lacks, with some turns missing the agent's cost, is now unpriced instead of carrying a partial sum. reported_cost_usd still shows the figures that came, summed, as before, so an entry with a cut-off turn keeps the agent's figure for its other turns. For a judge whose endpoint billed only some calls, it now shows the bills that came instead of null.

Tests

  • packages/agentenv-protocol/tests/test_usage_models.py: rows round-trip, are written only when present, are read from mappings with unknown keys ignored, have their counts, cost and flag validated, and must name their model. models must be a list, and from_dict refuses a falsy non-list. A turn that gives only its rows takes their sums as its counts.
  • tst/unit/task_step/usage_models_test.py:
    • each model is priced at its own rate;
    • a turn without rows beside one with them counts as the role's model;
    • a model the map lacks leaves the entry unpriced and keeps each row's cost;
    • an agent's per-model cost stands in where the map lacks the model;
    • an entry with no rows keeps its shape;
    • the host reader parses rows and sets aside a bad one;
    • a turn that gives only its rows is priced from them;
    • a turn whose rows can't all be priced leaves the entry unpriced rather than dropping;
    • an agent's figure stands in for a model the map lacks only when every turn gave one, for rows and for a single-model role;
    • a cut-off turn beside a reported one keeps the agent's figure beside the map's price.
  • tst/integration/cli/run_usage_by_model_test.py: the echo agent sends a breakdown over A2A. The step's entry has a row per model at each model's real map rate, and the task instance's ledger total equals their sum.

E2E

  • Through A2A, on an admin host (tst/integration/cli/run_usage_by_model_test.py, Docker): the echo agent sent a two-model breakdown (gpt-4.1-mini and gpt-4o-mini) over A2A. The step's entry had a row per model at each model's rate from LiteLLM's installed map, with no entry-level unit_prices. The task instance's usage_total.cost_usd equalled the rows' sum.
  • Real agents, regression: 6 runs through a logging pass-through in front of the real proxy, two of them with 35% faults on the judge's model. The agents were the Pydantic AI solver, the gpt-4.1-nano user sim, and direct, batched, Claude Code and user-sim judges. Every role of every run is exact against the proxy's counts, and every entry is priced with today's shape (no models, entry-level unit_prices). Today's agents send no breakdown, so their costs don't change.
  • Chaos, 15 minutes on two workers: 51 runs with faults at 20% or 35% on each model and interrupts mid-turn. The ledger's own checks hold on all 51. Every judge entry is exact (43) or marked as cut off (5), and every solver and user simulator entry is exact but one. Three rows read short by one Claude Code call that arrived before the run's own calls: the previous run's cut-off judge, its call still in flight, landing in the next run's slice of the log. The same three rows read short on main in feat(verifiers): a judge call cut off by a time limit or a cancel marks its step's usage a lower bound #880's chaos run, which used the same seeded order.

Model spend: about $0.82 at list prices. The agent side, including a Claude Code turn with a Haiku subagent checked against the CLI's own cost, is the second PR's e2e.

Docs

For FD-6263 and FD-6442:

  • the entry's models rows, and that its cost_usd is their sum;
  • that an entry priced per model has no entry-level unit_prices or uncached_input_tokens;
  • for agent authors, Usage.models and ModelUsage.

The protocol README and AGENTS.md say it already.

🤖 Generated with Claude Code

RetriggerView in GreptileConfidence Score: 5/5

The PR appears safe to merge; no new actionable issues were found.

Summary

Adds Usage.models and prices each model at its own rate.

  • Agents can report a turn’s counts separately for each model.
  • The host prices each model in a turn at its own rate.

Diagram

%%{init: {'theme': 'neutral'}}%%
flowchart LR
  A[Turn usage] --> B{Model rows supplied?}
  B -->|Yes| C[Group shares by model]
  B -->|No| D[Use the role's model]
  C --> E[Price each model]
  E --> F[Sum model costs]
  D --> G[Price role usage]
  F --> H[Step usage entry]
  G --> H
Loading

Reviews (8) · Last reviewed commit: "refactor(usage): one set of summing help..." · Reviewed by Greptile

…ices each model at its own rate

Usage.models holds a ModelUsage per model a turn's calls used (its calls, token and cache counts, and the agent's
own cost for them), covering every call the turn counted. The host prices each model's counts at that model's map
rate and the step's entry keeps a row per model, its cost_usd their sum; a turn without them is priced at the role's
model, as before. The echo test agent reports a breakdown on a usage-models line, for the integration test.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@earakely-scale
earakely-scale marked this pull request as ready for review November 23, 2026 17:09
@earakely-scale
earakely-scale requested a review from a team as a code owner November 23, 2026 17:09
Comment thread src/agent_env/task_step/usage.py Resolved
Comment thread src/agent_env/task_step/usage.py Resolved
earakely-scale and others added 4 commits November 23, 2026 09:17
…gent's figure stands in only when complete, and from_dict refuses a falsy models value

- Usage: a count a turn with models leaves out, that every row gives, is the rows' sum; so is the includes-cache flag
  the rows agree on. A model-only turn is priced from its rows.
- RoleUsage: the agent's or endpoint's figure stands in for the map only when every turn, or every share of a model,
  carried one; a sum of some isn't the cost, so a model the map lacks is unpriced instead.
- Usage.from_dict passes any non-null models value to the constructor, which refuses one that isn't a list.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…t share a count

A row that can't be priced, one missing its output say, leaves the entry unpriced instead of the turn being left out.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…y complete ones stand in for the map

The agent's or endpoint's figures stand in for a price only when every turn, or every share of a model, gave one;
their sum is kept beside the cost either way, as before, so an entry with a cut-off turn keeps the agent's figure.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…ls' rows, and a shared cost-fields type

- _total, _tokens and _agreed sum a role's turns and a model's shares alike; _priced is _cost_of, so it isn't
  mistaken for model_pricing's own _priced.
- Cost.fields writes the uncached input and unit prices only when the cost has them, so an entry priced per model
  needs no keys popped.
- CostFields holds the cost keys ModelEntry and UsageEntry share.
- _by_model files a turn's rows under their models, and a turn without them under the role's, in plain if/else.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@earakely-scale
earakely-scale merged commit bacdbe1 into main Nov 23, 2026
36 checks passed
@earakely-scale
earakely-scale deleted the edgararakelyan/fd6744-per-model-pricing branch November 23, 2026 17:59
earakely-scale added a commit that referenced this pull request Nov 23, 2026
… reported one; tests take the new floor

As the host's rule since #887: a sum of some calls' figures isn't the model's cost, so the host prices it from the map.
The agents' image-contract tests and the proxy usage integration test take the 0.1.640 floor and the turn's rows.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant