@@ -47,6 +47,7 @@ import {
4747import type { CustomAgentDefinition } from '@claude-code-best/builtin-tools/tools/AgentTool/loadAgentsDir.js'
4848import { runAgent } from '@claude-code-best/builtin-tools/tools/AgentTool/runAgent.js'
4949import { awaitClassifierAutoApproval } from '@claude-code-best/builtin-tools/tools/BashTool/bashPermissions.js'
50+ import type { AgentToolResult } from '@claude-code-best/builtin-tools/tools/AgentTool/agentToolUtils.js'
5051import { BASH_TOOL_NAME } from '@claude-code-best/builtin-tools/tools/BashTool/toolName.js'
5152import { SEND_MESSAGE_TOOL_NAME } from '@claude-code-best/builtin-tools/tools/SendMessageTool/constants.js'
5253import { TASK_CREATE_TOOL_NAME } from '@claude-code-best/builtin-tools/tools/TaskCreateTool/constants.js'
@@ -63,7 +64,10 @@ import {
6364} from '../../utils/messages.js'
6465import { evictTaskOutput } from '../../utils/task/diskOutput.js'
6566import { evictTerminalTask } from '../../utils/task/framework.js'
66- import { tokenCountWithEstimation } from '../../utils/tokens.js'
67+ import {
68+ tokenCountWithEstimation ,
69+ getTokenCountFromUsage ,
70+ } from '../../utils/tokens.js'
6771import { createAbortController } from '../abortController.js'
6872import { type AgentContext , runWithAgentContext } from '../agentContext.js'
6973import {
@@ -915,6 +919,7 @@ export async function runInProcessTeammate(
915919 invokingRequestId,
916920 } = config
917921 const { setAppState } = toolUseContext
922+ const startTime = Date . now ( )
918923
919924 logForDebugging (
920925 `[inProcessRunner] Starting agent loop for ${ identity . agentId } ` ,
@@ -1463,6 +1468,48 @@ export async function runInProcessTeammate(
14631468 // Mark as completed when exiting the loop
14641469 let alreadyTerminal = false
14651470 let toolUseId : string | undefined
1471+
1472+ // Compute result so the detail dialog can show token usage.
1473+ // Walk backwards for the last API usage (cumulative input_tokens from the
1474+ // Anthropic API already includes all prior context).
1475+ let completionTokens = 0
1476+ let completionToolUseCount = 0
1477+ let lastAssistantContent : AgentToolResult [ 'content' ] = [ ]
1478+ let lastUsage : AgentToolResult [ 'usage' ] | undefined
1479+ for ( let i = allMessages . length - 1 ; i >= 0 ; i -- ) {
1480+ const m = allMessages [ i ] !
1481+ if ( m . type === 'assistant' ) {
1482+ const blocks = ( m . message ?. content ?? [ ] ) as any [ ]
1483+ for ( const b of blocks ) {
1484+ if ( b ?. type === 'tool_use' ) completionToolUseCount ++
1485+ }
1486+ const textBlocks = blocks . filter ( ( b : any ) => b ?. type === 'text' )
1487+ if ( textBlocks . length > 0 && lastAssistantContent . length === 0 ) {
1488+ lastAssistantContent = textBlocks . map ( ( b : any ) => ( {
1489+ type : 'text' as const ,
1490+ text : b . text ,
1491+ } ) )
1492+ }
1493+ if ( ! lastUsage && m . message ?. usage ) {
1494+ lastUsage = m . message . usage as AgentToolResult [ 'usage' ]
1495+ completionTokens = getTokenCountFromUsage (
1496+ m . message . usage as Parameters < typeof getTokenCountFromUsage > [ 0 ] ,
1497+ )
1498+ }
1499+ if ( completionTokens > 0 && lastAssistantContent . length > 0 ) break
1500+ }
1501+ }
1502+
1503+ const teammateResult : AgentToolResult = {
1504+ agentId : identity . agentId ,
1505+ agentType : 'teammate' ,
1506+ content : lastAssistantContent ,
1507+ totalToolUseCount : completionToolUseCount ,
1508+ totalDurationMs : Date . now ( ) - startTime ,
1509+ totalTokens : completionTokens ,
1510+ usage : lastUsage as AgentToolResult [ 'usage' ] ,
1511+ } as unknown as AgentToolResult
1512+
14661513 updateTaskState (
14671514 taskId ,
14681515 task => {
@@ -1481,6 +1528,7 @@ export async function runInProcessTeammate(
14811528 status : 'completed' as const ,
14821529 notified : true ,
14831530 endTime : Date . now ( ) ,
1531+ result : teammateResult ,
14841532 messages : task . messages ?. length ? [ task . messages . at ( - 1 ) ! ] : undefined ,
14851533 pendingUserMessages : [ ] ,
14861534 inProgressToolUseIDs : undefined ,
0 commit comments