Skip to content

feat(usage): a run's colleagues, its env triggers' executors, count in its cost - #976

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

earakely-scale wants to merge 6 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 colleague 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 colleagues spent. On runs whose colleagues 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.
  • A reply past its time limit is still read for its usage, in the background, for up to 10 minutes more. That's 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, under the engine's own boot id and a version that every change to the list raises.
  • 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 colleague entry per env and gateway boot (task_step/colleagues.py).
  • 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 colleague 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:

  • colleague;
  • 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.

Tests

  • Gateway: a reply's usage is kept; 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.
  • Colleague 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 or no usage method is skipped; 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.
  • Elsewhere: the ledger key with gateway_boot; prompt_agent's capture records the entry; summarize's role order.
  • Wider runs: all gateway unit tests pass (1,679). The task and task_step tiers pass 5,382; the only failures are 4 computer_exec tests, which fail the same way on main on macOS (BSD base64).
  • plugin-api: no break.

End to end

Each run: a real local gateway, a Claude Code colleague 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 Colleague 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 colleague kept working and spent $0.078283, which the entry flags as unknown rather than $0 equal
one reply, 30% of the colleague'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 Colleague 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 colleague went on to spend $0.078683.

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

Run Colleague 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 closes the gateway, and the late read with it
one reply equal

Total spend across all runs: $2.98 at list.

For the docs

  • GET /triggers/usage and the triggers extension's usage method.
  • The colleague, validator and playground usage roles, and how a run's cost line shows them.
  • How colleague 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/5 · The PR appears safe to merge; no new actionable issue was found in the latest change.RetriggerView in GreptileConfidence Score: 5/5

The PR appears safe to merge; no new actionable issue was found in the latest change.

Summary

This PR adds colleague usage to a run’s costs without counting repeated snapshots twice.

  • The gateway keeps a usage record for every trigger executor call.
  • Run costs now include usage from trigger executors.
  • Cost summaries now show colleague, validator, and playground usage roles.

Diagram

%%{init: {'theme': 'neutral'}}%%
flowchart LR
  A[Executor call starts] --> B[Gateway lists unknown usage]
  B --> C[Reply reports usage]
  B --> D[Cancelled or timed-out call]
  D --> E[Background read]
  E --> C
  C --> F[Step or teardown reads snapshot]
  B --> F
  F --> G[Ledger keeps newest gateway version]
Loading

Reviews (9) · Last reviewed commit: "fix(gateway): a call cancelled while its..." · 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
… 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>

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