Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/core/src/agents/runtime/agent-core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1207,7 +1207,7 @@ export class AgentCore {

// Update token usage if available
if (lastUsage) {
this.recordTokenUsage(lastUsage, turnCounter, roundStreamStart);
this.recordTokenUsage(lastUsage, cumulativeRounds, roundStreamStart);
}

if (functionCalls.length > 0) {
Expand Down
37 changes: 37 additions & 0 deletions packages/core/src/agents/runtime/agent-headless.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ import {
type AgentStreamTextEvent,
type AgentToolCallEvent,
type AgentToolResultEvent,
type AgentUsageEvent,
} from './agent-events.js';
import type {
ModelConfig,
Expand Down Expand Up @@ -661,6 +662,42 @@ describe('subagent.ts', () => {
expect(scope.getExecutionSummary()).toMatchObject({ rounds: 2 });
});

it('should keep usage rounds unique across finishing input segments', async () => {
const { config } = await createMockConfig();
mockSendMessageStream.mockImplementation(async () =>
(async function* () {
yield {
type: 'chunk',
value: {
candidates: [{ content: { parts: [{ text: 'Done.' }] } }],
usageMetadata: { totalTokenCount: 1 },
},
};
})(),
);

const scope = await AgentHeadless.create(
'test-agent',
config,
{ systemPrompt: 'You are a test agent.' },
defaultModelConfig,
defaultRunConfig,
);
const usageRounds: number[] = [];
scope
.getEventEmitter()
.on(AgentEventType.USAGE_METADATA, (event: AgentUsageEvent) => {
usageRounds.push(event.round);
});

await scope.execute(new ContextState());
await scope.executeExternalInputs(['late correction'], undefined, {
resetStats: false,
});

expect(usageRounds).toEqual([1, 2]);
});

it('should preserve statistics for continuation work in the same logical turn', async () => {
const { config } = await createMockConfig();
mockSendMessageStream.mockImplementation(
Expand Down
Loading