feat(task): expose subagent token consumption to primary agent - #4611
feat(task): expose subagent token consumption to primary agent#4611arsham wants to merge 1 commit into
Conversation
Expose token consumption and context capacity to primary agent to enable informed delegation decisions. Aggregates subagent token consumption and updates parent message with cumulative costs. Includes context_limit and context_percentage in task_metadata so primary agent can decide whether to continue with subagent or spawn fresh session based on remaining context capacity.
|
Side note - noticed something in processor.ts While working on this, I came across something in case "finish-step":
input.assistantMessage.cost += usage.cost // accumulates
input.assistantMessage.tokens = usage.tokens // replacesCost uses Is this intentional? My guess is tokens represents "current context window state" while cost is "total spent" - but wanted to flag it in case this was an oversight. If a single message has multiple finish-step events, only the last step's tokens would be retained. Not blocking this PR on it, just curious if anyone knows the reasoning here. |
f1dc981 to
3e15a39
Compare
df8bdf9 to
0dd5039
Compare
|
yes that’s intentional |
|
for your token consumption stuff, is this a problem you were running into? Also note that child sessions work like any other so it automatically prunes tool calls and automatically compacts if needed so it is rare thatd itd run out of context |
|
Not a problem. This is an improvement over the The primary agent delegates to some agents. My subagents return the confidence level of what they are doing, but the only measurement they don't have is the context consumption. On the next turn, the primary looks at the confidence level and other metrics including the session_id, then makes a decision to reuse the same agent. This helps by reducing duplicate work and also if we get lucky and before cache invalidation, we get to reuse the cached prompt from the API side. |
f8ee907 to
6a9856d
Compare
00637c0 to
71e0ba2
Compare
f1ae801 to
08fa7f7
Compare
|
Closing this pull request because it has had no updates for more than 60 days. If you plan to continue working on it, feel free to reopen or open a new PR. |
Primary agent now receives token consumption data from subagents via
task_metadata, enabling smarter delegation decisions based on remaining context capacity.The
task_metadatanow includes:Example
Why
Previously,
task_metadataonly returnedsession_id. The primary had no visibility into how much context a subagent had consumed, making it impossible to know when to spawn a fresh session vs continue with an existing one.Primary can now check
context_percentageand decide to continue with the same subagent or start fresh when context is running low.