Repository navigation
[M10b] Add remote sub-agent token usage to the lead's totals - #297
Merged
Merged
Conversation
A lead delegating to a remoteAgent() now adds the remote run's reported run.done usage to result.usage (totals, usage.delegated, and byModel['remote:<name>'] with the remote's own costUsd). A continuation after a remote approval adds only what it spent after the pause; background remote tasks roll up the same way; a remote without usage adds nothing. RemoteRunOptions gains an optional onUsage callback. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This was referenced Oct 2, 2026
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.
Closes #234
What
A lead that delegates to a
remoteAgent()now adds the remote run's token usage to its ownresult.usage, the same way it does for local sub-agents. Before this, the remote run's usage was dropped.RemoteRunOptions.onUsage?: (usage: AgentEventUsage) => void(additive;run()still returnsPromise<string>).remoteAgent().runcalls it once per remote turn with therun.doneusage, beforeoutcome(), so a turn that paused reports its usage too. This holds for fresh turns and for approval continuations.runRemoteTaskconverts the usage with the newfromEventUsage(usage, 'remote:<name>'), then calls the sameonDelegatedUsagehook local children use. Totals,usage.delegatedandbyModel['remote:<name>']are updated.byModel['remote:...']entries keep the cost the remote reported.refreshDeriveddoes not re-price them, because there is no model id to price by. When the remote sends no cost, the lead'scostUsdbecomesundefined.snapshot.usage), and the continuation adds onlyusageSince(now, before). Chained pauses work the same way.cachedInputTokens/reasoningTokensonAgentEventUsage) were left out to keep the event schema unchanged.Tests
src/subagents/remoteAgent.test.ts: 6 new tests:delegatedandbyModel['remote:remote']onUsageis called with the turn's usagesrc/execution/runUsage.test.ts(new):fromEventUsage, cost merging and restore, unknown cost,usageSinceclamping.src/subagents/remoteAgent.test-d.ts(new): a hand-writtenRemoteSubagentstill type-checks.src/subagents/remoteUsage.live.test.ts(new): runs only undernpm run test:live, and skips withoutOPENROUTER_API_KEY.Docs
docs/sub-agents.md:docs/models-and-cost.md: the "Delegation" bullet.llms.txtandllms-full.txtwere regenerated.Verification (after
git merge origin/main, all exit 0)Live test
Skipped. The OpenRouter account is out of credit:
/creditsreturned total_usage 10.20 against total_credits 10 on 2026-10-02. The key counter shows usage 0 and limit_remaining 10, but calls would fail with 402. Live test spend: before 0, after 0 (no calls made). The step is listed on #260.🤖 Generated with Claude Code