Skip to content

feat(agents): Claude Code, Gemini, OpenCode, Letta Code and the proxy give each turn's counts by model - #893

Merged
earakely-scale merged 4 commits into
mainfrom
edgararakelyan/fd6744-agents-model-usage
Nov 23, 2026
Merged

earakely-scale merged 4 commits into
mainfrom
edgararakelyan/fd6744-agents-model-usage

Conversation

@earakely-scale

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

Copy link
Copy Markdown
Collaborator

What changes

The second half of P16 (FD-6744): the agents fill Usage.models (#887), so each model a turn used is priced at its own rate.

  • Claude Code: one row per model in the CLI's modelUsage tally for the turn, with the CLI's own cost for that model. A Haiku subagent under a Sonnet solver is priced at Haiku's rate. The rows come only when the tally is used (it covers every call). On the fallback path, a resumed session this process hasn't run, the turn has no rows and is priced at the role's model, as before.
  • Gemini: one row per model its proxy's calls named: the router, side calls and subagents.
  • OpenCode and Letta Code: the shared model relay now reads each request's model from its JSON body. Calls keeps it beside the call's usage, and Calls.usage(tool_calls, model) gives the rows. Both bridges pass their configured model.
  • ModelUsage.of(model, usages) (protocol): one model's row from the usages of its calls. Each count is summed over the calls that gave it. The cost is set only when every call reported one, as the host requires since feat(protocol): Usage gives a turn's counts by model, and the host prices each model at its own rate #887. The includes-cache flag is the one the calls with input counts agree on.
  • proxy: each recorded call keeps the model it named beside its usage, and the turn's usage gives a row per model through ModelUsage.of. The proxy has no configured model, so a turn with a call that named none gives no rows and is priced at the role's model. The in-process proxy, which an episode uses when the task runs no agent sandbox, has it now. The sandboxed proxy installs the protocol from PyPI, so it takes this from the next release, once its floor is raised.
  • In every agent, a call that named no model counts as the turn's configured model (decision 2 on feat(protocol): Usage gives a turn's counts by model, and the host prices each model at its own rate #887). Without one, the turn gives no rows and is priced at the role's model.
  • provider_details.models, which Claude Code and Gemini kept before, is gone; the typed rows replace it.
  • Protocol floors: Claude Code, Gemini, OpenCode and Letta Code now require 0.1.640, the first release with Usage.models.

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

  • An agent fills models whenever it knows each call's model, on every turn, with no condition. That also prices a whole turn a CLI ran on another model at that model's rate. The cost: these agents' entries always carry per-model rows, so they have no entry-level unit_prices. The protocol README, Usage's docstring and agents/AGENTS.md now say so.
  • The split: Hermes and Codex come in the next PR. The relay and Gemini switch to ModelUsage.of there too, once their protocol floor is the release that has it. Hermes' main and aux calls come from a usage file that names no model, and Codex's subagents' models need each rollout's turn_context.

Tests

  • tst/unit/utils/model_relay_models_test.py: the relay's rows by the model each request named, an unnamed call counted as the configured model's, no rows when neither is known, and the request body's model read.
  • tst/unit/agents/claude_code/test_usage.py: a Sonnet turn with a Haiku subagent gives each model's counts and the CLI's cost per model.
  • tst/unit/agents/gemini/test_usage.py: rows by model, and a call that named no model counted as the configured model's.
  • packages/agentenv-protocol/tests/test_usage_models.py: ModelUsage.of sums what the calls gave, takes their cost only when every call reported one, and takes the flag the counted calls agree on.
  • packages/agentenv-protocol/tests/test_a2a_proxy.py: a turn's recorded calls given by the model each named, and a model's cost only when every call reported one.
  • tst/integration/agent/proxy_usage_local_test.py: an episode's recorded calls reach the host with their row.
  • OpenCode's and Letta Code's relay tests now include their rows.

E2E against the real proxy

On an admin host, through a logging pass-through in front of the real proxy, each run's solver entry was compared row by row with the proxy's own count for each model its calls named: the row's counts, its cost against the map's price of the proxy's counts, and, for Claude Code, against the CLI's own cost for that model.

Run Rows (calls) Counts vs proxy Cost vs map Cost vs Claude Code's own
Claude Code, Sonnet solver, subagent on Haiku claude-sonnet-4-6 (2), claude-haiku-4-5 (2) exact equal equal (ratio 1.0)
Claude Code, Sonnet solver, WebFetch's call on Haiku claude-sonnet-4-6 (2), claude-haiku-4-5 (1) exact equal equal
Gemini, web search gemini-2.5-flash (2), gemini-3-flash-preview (1) exact equal —
Gemini, baseline gemini-2.5-flash (2) exact equal —
OpenCode, with and without a subagent gpt-4.1-mini (5; 3) exact equal —
Letta Code, with and without a subagent gpt-4.1-mini (4; 2) exact equal —
  • This issue's done-when holds: a Claude Code turn with a Haiku subagent under a Sonnet solver is priced at each model's rate, equal to the CLI's own cost. Before this PR the Haiku share was priced at Sonnet's rate, about three times its cost.
  • Finding: Claude Code calls one model by default. The bridge pins every model alias the CLI resolves (ANTHROPIC_DEFAULT_{OPUS,SONNET,HAIKU,FABLE}_MODEL, CLAUDE_CODE_SUBAGENT_MODEL) to the run's model, unless the agent's environment sets one. A gateway may serve no other model, so by default a Claude Code turn runs every call, subagents and WebFetch included, on the run's model. Its rows then have one model, which is right. The two-model runs above set CLAUDE_CODE_SUBAGENT_MODEL and ANTHROPIC_DEFAULT_HAIKU_MODEL in the deploy step's env_vars.
  • Chaos, 15 minutes on two workers: 80 runs of those scenarios with faults at 0%, 20% or 35% on every call. All 80 completed, and all 110 rows were exact against the proxy and equal to the map's price, and to the CLI's own cost for Claude Code. No model the proxy saw went without a row, and every entry is priced.

Model spend: about $2.74 at list prices.

Docs

For FD-6263 and FD-6442: which agents give their counts by model (Claude Code, Gemini, OpenCode, Letta Code, the in-process proxy; the sandboxed proxy from the next release; Hermes and Codex next), that their entries carry a row per model, and that provider_details.models moved to Usage.models.

🤖 Generated with Claude Code

RetriggerView in GreptileConfidence Score: 5/5

The PR appears safe to merge.

Summary

Adds per-model counts to Usage.models for Claude Code, Gemini, OpenCode, Letta Code, and the in-process proxy.

  • Protocol callers can build a model's usage row from its calls.
  • Claude Code reports each model's counts and cost for a turn.
  • Gemini reports each answered call's counts under its model.
  • OpenCode and Letta Code report turn usage by each call's model.
  • The in-process proxy reports recorded call usage by model.

Diagram

%%{init: {'theme': 'neutral'}}%%
flowchart LR
  A[Agent calls or CLI tally] --> B[Group counts by model]
  B --> C[Usage.models]
  C --> D[Host prices each model]
  A --> E[Model cannot be determined]
  E --> F[No model rows]
  F --> G[Host uses role model]
Loading

Reviews (7) · Last reviewed commit: "refactor(agents): one model's row is Mod..." · Reviewed by Greptile

earakely-scale and others added 2 commits November 23, 2026 10:17
… give each turn's counts by model

Usage.models from what each already counts: Claude Code from the CLI's modelUsage tally (with the CLI's own cost per
model), Gemini from its proxy's per-call records, OpenCode and Letta Code from the shared model relay, which now reads
each request's model, and the proxy from the model each recorded call names. A call that named no model counts as the
turn's configured one. Claude Code's and Gemini's per-model counts leave provider_details. The four agents' protocol
floor is 0.1.640, the first with Usage.models; the sandboxed proxy takes it from the next release.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
… 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>
@earakely-scale
earakely-scale marked this pull request as ready for review November 23, 2026 18:44
@earakely-scale
earakely-scale requested a review from a team as a code owner November 23, 2026 18:44
earakely-scale and others added 2 commits November 23, 2026 10:50
…cache flag is its counted calls'

The proxy has no configured model to count an unnamed call under, and the last model the turn named isn't it. A call
without usage no longer leaves its model's includes-cache flag unknown.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…h call keeps its model beside its usage

The rule for a model's row (counts summed over the calls that gave them, the cost only when every call reported one,
the flag the counted calls agree on) is the protocol's ModelUsage.of, which the proxy uses; the relay and Gemini take
it once their floor is the release that has it. The relay's Calls and the proxy's turn keep (model, usage) pairs rather
than two lists in step, and Gemini stops at the first unnamed call when no model is configured.

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