Skip to content

Commit 70633f5

Browse files
fix(cli,vscode): include subagent costs in total session cost display
The session cost displayed in the header/sidebar only summed costs from the root session's assistant messages, ignoring costs from subagent (child) sessions spawned via the task tool. This made the displayed cost misleading when subagents were used. Update all cost calculation sites to also aggregate costs from child sessions by filtering sessions with parentID matching the current session and summing their assistant message costs. Affected surfaces: - CLI TUI header and sidebar - VS Code extension (adds parentID to SessionInfo, aggregates in totalCost) - Web app (session-context-metrics, session-context-usage, session-context-tab) - ACP agent usage updates Closes #7495
1 parent ed54a65 commit 70633f5

9 files changed

Lines changed: 95 additions & 13 deletions

File tree

‎packages/app/src/components/session-context-usage.tsx‎

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,20 @@ export function SessionContextUsage(props: SessionContextUsageProps) {
4545
}),
4646
)
4747

48-
const metrics = createMemo(() => getSessionContextMetrics(messages(), sync.data.provider.all))
48+
// kilocode_change start - include subagent session costs in total
49+
const childCost = createMemo(() => {
50+
const id = params.id
51+
if (!id) return 0
52+
return sync.data.session
53+
.filter((s) => s.parentID === id)
54+
.reduce(
55+
(acc, child) =>
56+
acc + (sync.data.message[child.id] ?? []).reduce((sum, m) => sum + (m.role === "assistant" ? m.cost : 0), 0),
57+
0,
58+
)
59+
})
60+
// kilocode_change end
61+
const metrics = createMemo(() => getSessionContextMetrics(messages(), sync.data.provider.all, childCost()))
4962
const context = createMemo(() => metrics().context)
5063
const cost = createMemo(() => {
5164
return usd().format(metrics().totalCost)

‎packages/app/src/components/session/session-context-metrics.ts‎

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,10 @@ const lastAssistantWithTokens = (messages: Message[]) => {
4747
}
4848
}
4949

50-
const build = (messages: Message[] = [], providers: Provider[] = []): Metrics => {
51-
const totalCost = messages.reduce((sum, msg) => sum + (msg.role === "assistant" ? msg.cost : 0), 0)
50+
// kilocode_change start - accept optional child cost to include subagent costs
51+
const build = (messages: Message[] = [], providers: Provider[] = [], childCost = 0): Metrics => {
52+
const totalCost = messages.reduce((sum, msg) => sum + (msg.role === "assistant" ? msg.cost : 0), 0) + childCost
53+
// kilocode_change end
5254
const message = lastAssistantWithTokens(messages)
5355
if (!message) return { totalCost, context: undefined }
5456

@@ -77,6 +79,8 @@ const build = (messages: Message[] = [], providers: Provider[] = []): Metrics =>
7779
}
7880
}
7981

80-
export function getSessionContextMetrics(messages: Message[] = [], providers: Provider[] = []) {
81-
return build(messages, providers)
82+
// kilocode_change start - accept optional child cost to include subagent costs
83+
export function getSessionContextMetrics(messages: Message[] = [], providers: Provider[] = [], childCost = 0) {
84+
return build(messages, providers, childCost)
8285
}
86+
// kilocode_change end

‎packages/app/src/components/session/session-context-tab.tsx‎

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,20 @@ export function SessionContextTab() {
134134
}),
135135
)
136136

137-
const metrics = createMemo(() => getSessionContextMetrics(messages(), sync.data.provider.all))
137+
// kilocode_change start - include subagent session costs in total
138+
const childCost = createMemo(() => {
139+
const id = params.id
140+
if (!id) return 0
141+
return sync.data.session
142+
.filter((s) => s.parentID === id)
143+
.reduce(
144+
(acc, child) =>
145+
acc + (sync.data.message[child.id] ?? []).reduce((sum, m) => sum + (m.role === "assistant" ? m.cost : 0), 0),
146+
0,
147+
)
148+
})
149+
// kilocode_change end
150+
const metrics = createMemo(() => getSessionContextMetrics(messages(), sync.data.provider.all, childCost()))
138151
const ctx = createMemo(() => metrics().context)
139152
const formatter = createMemo(() => createSessionContextFormatter(language.intl()))
140153

‎packages/kilo-vscode/src/kilo-provider-utils.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ export function getErrorMessage(error: unknown): string {
6161
export function sessionToWebview(session: Session) {
6262
return {
6363
id: session.id,
64+
parentID: session.parentID ?? null,
6465
title: session.title,
6566
createdAt: new Date(session.time.created).toISOString(),
6667
updatedAt: new Date(session.time.updated).toISOString(),

‎packages/kilo-vscode/webview-ui/src/context/session.tsx‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1532,7 +1532,13 @@ export const SessionProvider: ParentComponent = (props) => {
15321532
.sort((a, b) => new Date(b.updatedAt).getTime() - new Date(a.updatedAt).getTime()),
15331533
)
15341534

1535-
const totalCost = createMemo(() => calcTotalCost(messages()))
1535+
const totalCost = createMemo(() => {
1536+
const id = currentSessionID()
1537+
const cost = calcTotalCost(messages())
1538+
if (!id) return cost
1539+
const children = Object.values(store.sessions).filter((s) => s.parentID === id)
1540+
return children.reduce((acc, child) => acc + calcTotalCost(store.messages[child.id] ?? []), cost)
1541+
})
15361542

15371543
// Status text derived from last assistant message parts
15381544
const statusText = createMemo<string | undefined>(() => {

‎packages/kilo-vscode/webview-ui/src/types/messages.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,7 @@ export interface SessionFileDiff {
128128
// Session info (simplified for webview)
129129
export interface SessionInfo {
130130
id: string
131+
parentID?: string | null
131132
title?: string
132133
createdAt: string
133134
updatedAt: string

‎packages/opencode/src/acp/agent.ts‎

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,25 @@ export namespace ACP {
106106
}
107107

108108
const used = msg.tokens.input + (msg.tokens.cache?.read ?? 0)
109-
const totalCost = assistantMessages.reduce((sum, m) => sum + m.info.cost, 0)
109+
// kilocode_change start - include subagent session costs in total
110+
const children = await sdk.session
111+
.children({ sessionID, directory }, { throwOnError: true })
112+
.then((x) => x.data ?? [])
113+
.catch(() => [] as { id: string }[])
114+
const childCost = await Promise.all(
115+
children.map((child) =>
116+
sdk.session
117+
.messages({ sessionID: child.id, directory }, { throwOnError: true })
118+
.then((x) =>
119+
(x.data ?? [])
120+
.filter((m): m is (typeof assistantMessages)[number] => m.info.role === "assistant")
121+
.reduce((sum, m) => sum + m.info.cost, 0),
122+
)
123+
.catch(() => 0),
124+
),
125+
).then((costs) => costs.reduce((a, b) => a + b, 0))
126+
const totalCost = assistantMessages.reduce((sum, m) => sum + m.info.cost, 0) + childCost
127+
// kilocode_change end
110128

111129
await connection
112130
.sessionUpdate({

‎packages/opencode/src/cli/cmd/tui/routes/session/header.tsx‎

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,10 +48,25 @@ export function Header() {
4848
const messages = createMemo(() => sync.data.message[route.sessionID] ?? [])
4949

5050
const cost = createMemo(() => {
51-
const total = pipe(
52-
messages(),
53-
sumBy((x) => (x.role === "assistant" ? x.cost : 0)),
54-
)
51+
// kilocode_change start - include subagent session costs in total
52+
const sid = route.sessionID
53+
const total =
54+
pipe(
55+
messages(),
56+
sumBy((x) => (x.role === "assistant" ? x.cost : 0)),
57+
) +
58+
sync.data.session
59+
.filter((s) => s.parentID === sid)
60+
.reduce(
61+
(acc, child) =>
62+
acc +
63+
pipe(
64+
sync.data.message[child.id] ?? [],
65+
sumBy((x) => (x.role === "assistant" ? x.cost : 0)),
66+
),
67+
0,
68+
)
69+
// kilocode_change end
5570
return new Intl.NumberFormat("en-US", {
5671
style: "currency",
5772
currency: "USD",

‎packages/opencode/src/cli/cmd/tui/routes/session/sidebar.tsx‎

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,18 @@ export function Sidebar(props: { sessionID: string; overlay?: boolean }) {
4141
)
4242

4343
const cost = createMemo(() => {
44-
const total = messages().reduce((sum, x) => sum + (x.role === "assistant" ? x.cost : 0), 0)
44+
// kilocode_change start - include subagent session costs in total
45+
const total =
46+
messages().reduce((sum, x) => sum + (x.role === "assistant" ? x.cost : 0), 0) +
47+
sync.data.session
48+
.filter((s) => s.parentID === props.sessionID)
49+
.reduce(
50+
(acc, child) =>
51+
acc +
52+
(sync.data.message[child.id] ?? []).reduce((sum, x) => sum + (x.role === "assistant" ? x.cost : 0), 0),
53+
0,
54+
)
55+
// kilocode_change end
4556
return new Intl.NumberFormat("en-US", {
4657
style: "currency",
4758
currency: "USD",

0 commit comments

Comments
 (0)