Motivation
A caller that logs token usage once per run needs a supported way to read the total after
consuming the stream with for await. stream.finalResponse() already returns it — usage
contents are summed into usageDetails as updates are folded, matching Go, which sums usage
across the response's messages — but nothing pins that path at the agent level, and the
early-termination case is undocumented.
Scope
- A test that iterates an agent run to completion and asserts
usageDetails on the response from
finalResponse(), using a mock chat client that emits UsageContent.
- TSDoc noting that a run abandoned partway reports only the usage observed up to that point.
Motivation
A caller that logs token usage once per run needs a supported way to read the total after
consuming the stream with
for await.stream.finalResponse()already returns it —usagecontents are summed into
usageDetailsas updates are folded, matching Go, which sums usageacross the response's messages — but nothing pins that path at the agent level, and the
early-termination case is undocumented.
Scope
usageDetailson the response fromfinalResponse(), using a mock chat client that emitsUsageContent.