🦌 Generate graceful summary when maxSteps exhausted mid-tool-call#49
🦌 Generate graceful summary when maxSteps exhausted mid-tool-call#49masudahiroto merged 7 commits intomainfrom
Conversation
…ll chain When the for loop exhausts maxSteps while still processing tool calls, make one final streamText call without tools so the model can summarize its progress, instead of silently stopping mid-execution. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
The graceful summary call (triggered when maxSteps is exhausted) was missing the experimental_telemetry option, so Langfuse did not record it. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
|
|
||
| // Graceful finish: if maxSteps was exhausted while still in a tool-call chain, | ||
| // make one final streamText call without tools so the model can summarize progress. | ||
| if (lastStepHadToolCalls) { |
There was a problem hiding this comment.
Same feedback as #50
It would be nice to try and keep this file a bit cleaner and cover edge cases with functions such that the main logic can be simplified into a function call which handles the edge case if it exists, or no-ops if it doesn't.
|
|
||
| // Graceful finish: if maxSteps was exhausted while still in a tool-call chain, | ||
| // make one final streamText call without tools so the model can summarize progress. | ||
| const gracefulSummary = await generateGracefulSummaryIfNeeded({ |
There was a problem hiding this comment.
this function needs so many arguments :(
it implies a really tight coupling
can we simplify somehow? maybe some of the decision making has to move back into AISDKAgent.
There was a problem hiding this comment.
I agree with you. This function has many arguments and its structure is not ideal. I'll consider how we can solve this. Thank you for your review 🙇
There was a problem hiding this comment.
@mm-zacharydavison I refactored the graceful summary step.
It is now integrated into the main agent loop. The loop iterates one extra time beyond maxSteps, and the last iteration (stepIteration === this.maxSteps) is treated as the graceful summary step, where the messages, tools, and metadata are overridden specifically for summarization. Some minor refactoring of AISDKAgent is also included.
|
sorry but i will merge #53 first. please wait a minutes to fix merge conflicts |
Resolve conflicts in AISDKAgent.ts by applying graceful summary feature (lastStepHadToolCalls, applyGracefulSummaryOverrides, stepConfig) onto main's refactored structure (RunContext, StepContext, executeStepLoop, processStreamChunk). Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
|
I fixed the merge conflict caused by refactoring of AISDKAgent. Please re-review it. I apologize for requesting many reviews. |
Summary
When an agent exhausts its
maxStepslimit while still mid-tool-call chain, it previously ended abruptly without producing a final text response. This change adds a graceful summary step: after the last allowed step, if the final step ended with tool calls (meaning the model never got to produce a text response), an extra step is run with tools stripped and a prompt injected asking the model to summarize its progress.Changes
AISDKAgent.ts: AddedlastStepHadToolCallstracking toRunContext; extended the step loop by one iteration to allow a graceful summary step; introducedapplyGracefulSummaryOverrides()to strip tools and inject a summary prompt on the extra step; refactoredstreamTextcall to use a sharedstepConfigobject for cleaner metadata/tools management.AISDKAgent.maxSteps.test.ts: AddedcreateToolThenTextMockModelhelper for testing the graceful summary path; updated existing call-count assertions to account for the extra summary step; added new tests verifying that a text message is emitted after maxSteps when last step had tool calls, and that no extra call is made when the run completes normally without tool calls.