Repository navigation
feat(agent): count tool calls and failures per tool #2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| /** | ||
| * Per-tool call counts for the current process, so a session can report which tools the | ||
| * agent leaned on and how many of their calls failed. | ||
| */ | ||
| export interface ToolCallStat { | ||
| calls: number; | ||
| errors: number; | ||
| } | ||
|
|
||
| const stats = new Map<string, ToolCallStat>(); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
When multiple Useful? React with 👍 / 👎. |
||
|
|
||
| export function recordToolCall(toolName: string, isError: boolean): void { | ||
| const stat = stats.get(toolName) ?? { calls: 0, errors: 0 }; | ||
| stat.calls += 1; | ||
| if (isError) stat.errors += 1; | ||
| stats.set(toolName, stat); | ||
| } | ||
|
|
||
| /** A snapshot, most-called tool first. */ | ||
| export function getToolCallStats(): Array<{ toolName: string } & ToolCallStat> { | ||
| return [...stats.entries()] | ||
| .map(([toolName, stat]) => ({ toolName, ...stat })) | ||
| .sort((a, b) => b.calls - a.calls); | ||
| } | ||
|
|
||
| export function resetToolCallStats(): void { | ||
| stats.clear(); | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When a tool invokes another tool through the exported
runToolCall()pipeline, the nested call never reaches this block, so only the outer tool is counted. This occurs inpackages/coding-agent/src/core/agent-session.ts, where nested tool calls delegate directly torunToolCall(); for example, a codemode call that invokesreadtwice records onecodemodecall and zeroreadcalls or failures. Record outcomes in the commonrunToolCall/finalization path, while avoiding double-counting model-issued calls.Useful? React with 👍 / 👎.