Repository navigation
feat(usage): what a run's env trigger executors spend counts in its cost - #976
Merged
Merged
Conversation
…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>
…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>
… 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>
…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>
…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>
…ds its own usage under roles it names `validator` and `playground` were roles for usage only embedders produce. Core keeps `UsageRole.EXECUTOR`, the one it records; `record_usage()` takes any role, and the summary lists a role core doesn't name after the ones it does. `StepUsage` takes `ModelRole` again, since nothing passes it a usage role. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
With `validator` and `playground` gone, `UsageRole` named one string. `EXECUTOR_ROLE` in `task_step/usage.py` is that string; it isn't a `ModelRole`, since no `[model.roles]` key configures an executor's model. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…t registers executors files their usage `task_step/executors.py` mixed the gateway's list with pricing and the run's bookkeeping. Now: - `DeployedGatewayEnv.executor_calls()` reads `GET /triggers/usage`, re-reading while calls are still being read and keeping the last good list, and `lists_executor_calls` says whether the gateway can (`env/env.py`). - `executor_entry` prices a list as one `executor` entry beside `RoleUsage` (`task_step/usage.py`). - `record_executor_usage` files the entry under the `register_env_triggers` step that set the executors up (`task_step/task_steps/register_env_triggers.py`); prompt steps, `always_on` and the teardown call it. - The teardown's wait lives in `teardown.py`. Behaviour is unchanged. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…st, not only gateway-fronted records The previous commit read a gateway's executor calls only from `DeployedGatewayEnv` records, so an env outside our sandboxes, a plain `DeployedEnv` with a gateway's card that `register_env_triggers` supports, lost its executors' usage again. `executor_calls()` and `lists_executor_calls` move to `DeployedEnv`, keyed on the card's triggers `usage` method as before; a registered env whose card doesn't offer it counts as one call of unknown usage. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…er the step that set them up Executor usage was tested only from prompt steps; always_on, which PSI's always-on tasks run, records it through the same capture as it ends. The test fails without that call. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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.
ReadTimeoutfor a send held past its window, or no reply for a task still working at its deadline. Aclearleaves these reads to finish.GET /triggers/usage, the triggers extension's newusagemethod. It comes under the engine's own boot id, with aversionthat every change to the list raises andreading, the number of exchanges still running./triggers/state, because steps pollstatewhile they wait for triggers to settle.clearkeeps the list: those calls were spent.Recording.
prompt_agentandalways_onrecord when they end (the existing trigger-state capture), and so does teardown (tear_down, so theteardown_sandboxesstep too). Each writes the gateway's list as oneexecutorentry per env and gateway boot. The code is split by layer:DeployedGatewayEnv.executor_calls()reads the gateway's list (env/env.py);executor_entryprices it (task_step/usage.py);record_executor_usagefiles it under the step that set the executors up (register_env_triggers.py).register_env_triggersstep that set the executors up, with no attempt.partial), never $0.Each call counts once.
gateway_boottoo, as well as attempt, step and role.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.
record_usage(). It merges finished entries into a run's ledger and nothing else, so an embedder can use it too.Roles.
executor(EXECUTOR_ROLEintask_step/usage.py) is the one new role. It isn't aModelRole: no[model.roles]key configures it, since an executor's model is its agent's own.summarizelists it after the solver, user simulator and judge, and counts only the attempts entries name. An embedder that records usage of its own throughrecord_usage()names its own role, which the summary lists after these. Core doesn't name embedders' 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).Tests
clearkeeps the list and its boot id; the route serves the list; the card offersusage.Task.runon 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).gateway_boot;prompt_agent's andalways_on's captures record the entry;summarizelists an executor after the judge and a role an embedder names after that.computer_exectests, which fail the same way onmainon macOS (BSDbase64).plugin-api: no break.The usage role was first called
colleagueand is nowexecutor, 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.
partial, unpriced (no_tokens); the executor kept working and spent $0.078283, which the entry flags as unknown rather than $0That'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):gateway_version2 (the call started, then ended)gateway_version4gateway_version1: the call is listed with no usage, so the entry ispartialand 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):partialand unpriced, with $0.078 unknown.partial, unpriced: the run's teardown closed the gateway, and the late read with it (fixed next)Rerun on the teardown wait (
e9baa6202):cost: $0.1651 (solver $0.0785; executor $0.0867).Rerun on the review fixes (
1067a58da: one exchange task per call, teardown's wait beside the recordings, an unlisting gateway counted as unknown):partialThe solver's entries equal its own pass-through's in all four.
An always-on run (
a5ae24deb), the path PSI's always-on tasks take: analways_onstep with a user simulator, whose solver emails the executor, which replies once.test_always_on_executor_usage.pycovers the same path in the unit tier; it fails withoutalways_on's recording call.Total spend across all runs: $4.20 at list.
For the docs
GET /triggers/usageand the triggers extension'susagemethod.executor,validatorandplaygroundusage roles, and how a run's cost line shows them.record_usage()for embedders.🤖 Generated with Claude Code
Confidence Score: 5/5

The PR appears safe to merge; no new actionable issues were found.Summary
The PR adds trigger executors’ spend to a run’s usage ledger.
All six earlier findings are addressed in the current code.
Diagram
%%{init: {'theme': 'neutral'}}%% flowchart LR A[Trigger fires] --> B[Gateway lists call] B --> C[Executor replies] C --> D[Gateway saves usage] D --> E[Step completion or teardown reads report] E --> F[Price executor entry] F --> G[Merge ledger by gateway boot and version]Reviews (28) · Last reviewed commit: "test(usage): an always_on step records i..." · Reviewed by Greptile