Skip to content

feat(usage): what a run's env trigger executors spend counts in its cost - #976

Open
earakely-scale wants to merge 11 commits into
mainfrom
edgararakelyan/env-side-agent-usage
Open

earakely-scale wants to merge 11 commits into
mainfrom
edgararakelyan/env-side-agent-usage

Conversation

@earakely-scale

@earakely-scale earakely-scale commented Dec 1, 2026 •

Copy link
Copy Markdown
Collaborator

Why

An env's gateway calls a trigger's executor when the trigger fires; the executor is an AI-played contact, such as a coworker who replies to an email. No prompt step prompts it, so no step recorded its usage, and a run's ledger never held what its executors spent. On runs whose executors reply often, that's a large share of the real cost.

What

Gateway.

  • The trigger engine keeps every call to an executor together with the usage its final A2A message reported, exactly as the executor sent it. The gateway container ships no protocol package, so steps do the parsing.
  • A call is listed as it starts, with no usage until its reply ends. A call that ends without a reply keeps what it reported, or nothing. That covers a failed task and an HTTP error.
  • Each call runs as one exchange task that owns it from the send to its last read; the fire waits for the answer that task gives it. A reply past its time limit is still read for its usage, in the background, for up to 10 minutes more: the send still waiting for its reply, or the task still to poll. The fire sees what it always did: ReadTimeout for a send held past its window, or no reply for a task still working at its deadline. A clear leaves these reads to finish.
  • The list is served at GET /triggers/usage, the triggers extension's new usage method. It comes under the engine's own boot id, with a version that every change to the list raises and reading, the number of exchanges still running.
  • It's a separate method from /triggers/state, because steps poll state while they wait for triggers to settle.
  • clear keeps the list: those calls were spent.

Recording.

  • prompt_agent and always_on record when they end (the existing trigger-state capture), and so does teardown (tear_down, so the teardown_sandboxes step too). Each writes the gateway's list as one executor entry per env and gateway boot (task_step/executors.py).
  • Teardown reads each env's list again every 2 s while calls are still being read, for at most 60 s, while it ends the run's recordings, so the wait overlaps time it spends anyway. A reply still running when the run ends is counted before the env closes.
  • An env whose gateway doesn't list its calls (an older gateway image) counts as one call of unknown usage, logged once per run, so the run's cost reads "at least" rather than exact.
  • The entry goes under the register_env_triggers step that set the executors up, with no attempt.
  • Calls are priced like any role's turns: by model, from the per-model rows the executor reports.
  • A call that reported nothing makes the entry a lower bound (partial), never $0.

Each call counts once.

  • The ledger now keys an entry by gateway_boot too, as well as attempt, step and role.
  • Each write is the gateway's whole list, so it replaces the previous one. A call counts once whichever step, attempt or teardown wrote it.
  • A gateway that a retry redeployed has its own boot id, so its calls are kept beside the first gateway's.
  • A write read from an earlier copy of a gateway's list (a lower gateway_version) never replaces a later one, in the ledger or in a step's context. Parallel steps' completion writes can land out of order.

Ledger writes.

  • A step's completion merges the context's executor entries along with its own.
  • Teardown writes its entries through the new record_usage(). It merges finished entries into a run's ledger and nothing else, so an embedder can use it too.

Roles. UsageRole adds three roles:

  • executor;
  • validator, for a step that checks a run without grading it;
  • playground, for an embedder's chat with a run's agent outside its steps.

None of them is a [model.roles] key. summarize lists them after the solver, user simulator and judge, and counts only the attempts entries name. StepUsage accepts the new roles.

Also: a flaky registry read (found by this PR's CI). A bundle run failed when a single manifest read from GHCR timed out (couldn't read its manifest from ghcr.io: ReadTimeout).

  • The registry client now sends a GET or HEAD that a transport error dropped once more, after a second. A write is never sent twice.
  • The error a failed write raised read "couldn't writs manifeste its manifest"; it now reads "couldn't write its manifest".

Tests

  • Gateway: a reply's usage is kept; a read that fails after the fire has its answer leaves the usage unknown and nothing running; a failed task, an HTTP error and a reply with no usage are each kept as a call; clear keeps the list and its boot id; the route serves the list; the card offers usage.
  • Executor entry: priced model by model; a call with no usage makes the entry partial, never $0; it's written under the registration step and each write replaces the last; a new boot is kept beside the old; an env with no executors is skipped; one whose gateway doesn't list its calls makes the entry partial and is logged once; a failed read is logged.
  • Through Task.run on a real SQLite store: the entry joins the ledger as each step completes; a retried span over the same gateway leaves one entry of 4 calls for 4 calls; teardown writes the calls that came after the last step; teardown ends the recordings while it waits for a reply (the test fails on a sequential teardown).
  • Elsewhere: the ledger key with gateway_boot; prompt_agent's capture records the entry; summarize's role order.
  • Wider runs: the gateway, task, task_step and config tiers pass 7,677; the only failures are 4 computer_exec tests, which fail the same way on main on macOS (BSD base64).
  • plugin-api: no break.

The usage role was first called colleague and is now executor, the framework's own name for these agents (register_env_triggers' executor_agent_name). The end-to-end runs below, but the last table's, predate the rename and show the same numbers.

End to end

Each run: a real local gateway, a Claude Code executor with its own model endpoint, and a Claude Code solver. Each agent's model calls went through its own logging pass-through to the real model gateway, so each role's truth is that gateway's per-call usage.

Run Executor entry vs. its gateway truth Solver
one reply 1 turn; 153 out, 55,256 in, $0.073531: equal equal
two replies, over two prompt steps 1 entry, 2 turns; 313 out, 110,978 in, $0.081274: equal equal
reply after the last prompt, recorded at teardown 1 turn; 161 out, 55,264 in, $0.015765: equal equal
reply cut off past the gateway's read window 1 turn, partial, unpriced (no_tokens); the executor kept working and spent $0.078283, which the entry flags as unknown rather than $0 equal
one reply, 30% of the executor's model calls failed equal after Claude Code's retries (2 calls faulted) equal
two replies, 30% failed equal equal
after the last prompt, 30% failed equal (1 call faulted) equal
one reply, repeated equal equal

That's 8 of 8 matching, plus one earlier cut-off attempt whose reply came back within the read window and matched exactly. Total spend $1.49 at list.

Rerun on the review fixes (ebbabfb12):

Run Executor entry vs. its gateway truth
one reply equal; gateway_version 2 (the call started, then ended)
two replies equal; one entry, gateway_version 4
reply after the last prompt, recorded at teardown equal
reply still running when the run tore down (90 s, past the prompt step's 60 s settle window) gateway_version 1: the call is listed with no usage, so the entry is partial and unpriced, and the run's cost line reads "at least" where it would have read exact before the fix. The executor went on to spend $0.078683.

Rerun on the late-read change (34cbfb95e):

Run Executor entry vs. its gateway truth
reply past the gateway's 60 s read window, with the run still up equal: 426 out, 140,235 in, $0.086742. Before this change it was partial and unpriced, with $0.078 unknown.
reply still running when the run ended partial, unpriced: the run's teardown closed the gateway, and the late read with it (fixed next)
one reply equal

Rerun on the teardown wait (e9baa6202):

Run Executor entry vs. its gateway truth
reply still running when the run ended (90 s) equal: 418 out, 140,209 in, $0.086653. Teardown waited for it, and the run's cost line is exact: cost: $0.1651 (solver $0.0785; executor $0.0867).
reply after the last prompt, recorded at teardown equal

Rerun on the review fixes (1067a58da: one exchange task per call, teardown's wait beside the recordings, an unlisting gateway counted as unknown):

Run Executor entry vs. its gateway truth
reply past the fire's 60 s read window, with the run still up equal: 445 out, 140,322 in, $0.087030
reply still running when the run ended equal: 420 out, 140,192 in, $0.086664; teardown waited while it ended the recordings
one reply, 30% of the executor's model calls failed equal: 161 out, 55,264 in, $0.073631
reply cut off past the fire's window (a 3 s limit, the reply after about 80 s) equal: 491 out, 140,445 in, $0.087598, read late, where this scenario's first run was partial

The solver's entries equal its own pass-through's in all four.

Total spend across all runs: $3.95 at list.

For the docs

  • GET /triggers/usage and the triggers extension's usage method.
  • The executor, validator and playground usage roles, and how a run's cost line shows them.
  • How executor usage is recorded: when, under which step, keyed by gateway boot, and partial when a reply is cut off.
  • record_usage() for embedders.

🤖 Generated with Claude Code

Confidence Score: 5/5RetriggerView in Greptile

The PR appears safe to merge; no new actionable issue was found.

Summary

Adds trigger-executor spend to each run’s usage ledger.

  • Gateways keep trigger executor calls and their reported usage in a readable list.
  • Run costs include trigger executor usage from prompt steps and teardown.
  • Usage summaries show executor, validator, and playground roles.
  • Registry reads get one retry after a dropped connection.

All five supplied earlier findings are addressed in the current code. Their threads have no supplied comment numbers, so previousFindings is empty.

Reviews (16) · Last reviewed commit: "refactor(usage): one task owns each exec..." · Reviewed by Greptile

…n its cost

An env's gateway calls a trigger's executor (an AI-played contact) when the trigger fires. No prompt step
prompts it, so no step recorded its usage and a run's ledger never held what its colleagues spent.

- The gateway keeps every executor call with the usage its final message reported, as sent, and serves the
  list at GET /triggers/usage (the triggers extension's new `usage` method) under the engine's own boot id.
  `clear` keeps it: it was spent.
- `prompt_agent` and `always_on` as they end, and the teardown, write the list as one `colleague` entry per env
  and gateway boot (`task_step/colleagues.py`), with no attempt, under the `register_env_triggers` step that set
  the executors up. A call that reported nothing makes the entry a lower bound, never $0; calls are priced by
  model from their per-model rows.
- The ledger keys an entry by `gateway_boot` too, so each write is the whole list and replaces the last: a call
  counts once whichever step or attempt wrote it, and a gateway a retry redeployed is kept beside the first.
- A step's completion takes the context's colleague entries into the ledger with its own; the teardown writes
  its own through the new `record_usage()`, which merges finished entries into a run's ledger alone, for any
  embedder.
- `UsageRole` adds `colleague`, `validator` (a step that checks a run without grading it) and `playground` (an
  embedder's chat with a run's agent outside its steps). They aren't `[model.roles]` keys; `summarize` lists
  them after the solver, user simulator and judge, and `StepUsage` takes them.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@earakely-scale
earakely-scale requested a review from a team as a code owner December 1, 2026 03:37
Comment thread src/agent_env/env/gateway/triggers.py Resolved
Comment thread src/agent_env/task/store.py Resolved
Comment thread src/agent_env/task_step/colleagues.py Resolved
earakely-scale and others added 4 commits November 30, 2026 19:45
…of a gateway's list, count real attempts

- The gateway lists a call to an executor as it starts, with no usage until its reply ends, so a teardown that
  reads while a colleague is still answering counts that call, as one of unknown usage, and the entry is a lower
  bound rather than exact.
- `GET /triggers/usage` carries a `version` every change to the list raises, kept on the entry
  (`gateway_version`). The ledger, and a step's context, never replace a gateway's colleague entry with one read
  from an earlier copy of its list (`older`), which a parallel step's completion write can land with after a
  newer one.
- `summarize` counts the attempts entries name; an attempt-less colleague entry isn't one of its own.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
An executor that answers past its time limit is still working, and still spending. Its call was recorded with no
usage, so its run's colleague entry stayed a lower bound with those dollars unknown. The gateway now hands that
call's read to the background when it gives up on it: the send still waiting for its reply, or the task still to
poll, up to `_LATE_REPLY_S` (10 minutes) more. When the task ends, the usage its final message reports goes on the
call and raises the list's version, so the next write of the run's colleague entry prices it. The fire sees what
it always did: `ReadTimeout` for a send held past its window, no reply for a task still working at its deadline.
A `clear` leaves the reads to finish.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Comment thread src/agent_env/env/gateway/triggers.py Resolved
earakely-scale and others added 2 commits December 1, 2026 05:43
… for its usage

A fire that `clear` or `remove` cancelled while it polled its executor's task closed the client and left the call
unpriced. The poll now hands the task to the background read (`_read_late`), as a cancelled send already does.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…d before it records them

A reply still running when its run ended stayed unpriced: the teardown read the gateway's list, then closed the
env, and the reply's read went with it. `GET /triggers/usage` now says how many calls the gateway is still
reading (`reading`, a late reply's included), and the teardown reads each env's list again every 2 seconds until
none is, for at most `TEARDOWN_WAIT_SECONDS` (60), before it records; envs are read side by side. Prompt steps
don't wait here: they already wait out replies in flight.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Comment thread src/agent_env/task_step/colleagues.py Resolved
earakely-scale and others added 3 commits December 1, 2026 06:56
…t last read

The teardown's wait for calls still being read dropped the env's whole list when a read after the first failed,
so the calls it had already read went unrecorded. It now logs the failure and records the last list it read.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
A bundle run failed when one manifest read from GHCR timed out (`couldn't read its manifest from ghcr.io:
ReadTimeout`), as integration CI does now and then. The registry client sends a GET or HEAD a transport error
dropped (a timeout, a reset connection) once more after a second; a write is never sent twice. The error a write
raises said "couldn't writs manifeste its manifest"; it now says "couldn't write its manifest".

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…r`, not `colleague`

The calls an env's gateway makes to a trigger's executor are recorded under the role `executor`
(`UsageRole.EXECUTOR`), the framework's own name for that agent (`register_env_triggers`' `executor_agent_name`
and `executors`), rather than `colleague`, one benchmark's word for it. `task_step/colleagues.py` is
`task_step/executors.py`, with `record_executor_usage` and `executor_entry`.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@earakely-scale earakely-scale changed the title feat(usage): a run's colleagues, its env triggers' executors, count in its cost feat(usage): what a run's env trigger executors spend counts in its cost Dec 1, 2026
…rdings while it waits, and an unlisting gateway makes the cost a lower bound

- The gateway runs each executor call as one exchange task that owns its client from the send to the last read; the
  fire awaits a future the exchange settles (the reply, None at the time limit, ReadTimeout past it, or the error), and
  `reading` is the count of exchanges still running, so no exit path has to hand the call on or count it down.
- Teardown reads its executors' usage while it ends the run's recordings, so its wait for a late reply overlaps time
  it spends anyway.
- An env whose gateway doesn't list its executors' calls counts as one call of unknown usage, said once per run, so
  the run's cost reads as a lower bound rather than exact.

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

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant