feat(ai): add onStepFinish to agent.generate and agent.stream#11980
Merged
feat(ai): add onStepFinish to agent.generate and agent.stream#11980
Conversation
nicoalbanese
approved these changes
Jan 23, 2026
Collaborator
Author
B-Step62
pushed a commit
to B-Step62/ai
that referenced
this pull request
Jan 28, 2026
…#11980) ## Background For things such as token tracking, it would be helpful if callbacks can be registered on a per-call basis. See vercel#11468 ## Summary Add `onStepFinish` callback support to `Agent.generate()` and `Agent.stream()` ## Related Issues Resolves vercel#11468
1 task
rovo89
added a commit
to rovo89/ai
that referenced
this pull request
Feb 25, 2026
This changes the type of the `onStepFinish` callback from `ToolLoopAgentOnStepFinishCallback<TOOLS>` to `UIMessageStreamOnStepFinishCallback<UI_MESSAGE>` for methods which create a `UIMessageChunk` stream for an Agent, giving the implementation access to the generated`UIMessage` so far instead of just the raw parts. This partly reverts vercel#11980, which explicitly added the `onStepFinish` callback to the method signatures. Instead, `onStepFinish` is now added to the `UIMessageStreamOptions` type in the same way as `onFinish` already was, and is passed through via `options`. Affected methods are: - `createAgentUIStream()` - `createAgentUIStreamResponse()` - `pipeAgentUIStreamToResponse()` For even more consistency, the `onStepFinish` callback is also added to: - `DefaultStreamTextResult.toUIMessageStream()` (used by the methods above) - `DefaultStreamTextResult.toUIMessageStreamResponse()` - `DefaultStreamTextResult.pipeUIMessageStreamToResponse()` Callers which want to receive the `ToolLoopAgentOnStepFinishCallback` can easily do that with the `onStepFinish` callback to the constructor of the `ToolLoopAgent` that they're passing to the methods above. Fixes vercel#12841.
rovo89
added a commit
to rovo89/ai
that referenced
this pull request
Feb 25, 2026
This changes the type of the `onStepFinish` callback from `ToolLoopAgentOnStepFinishCallback<TOOLS>` to `UIMessageStreamOnStepFinishCallback<UI_MESSAGE>` for methods which create a `UIMessageChunk` stream for an Agent, giving the implementation access to the generated`UIMessage` so far instead of just the raw parts. This partly reverts vercel#11980, which explicitly added the `onStepFinish` callback to the method signatures. Instead, `onStepFinish` is now added to the `UIMessageStreamOptions` type in the same way as `onFinish` already was, and is passed through via `options`. Affected methods are: - `createAgentUIStream()` - `createAgentUIStreamResponse()` - `pipeAgentUIStreamToResponse()` For even more consistency, the `onStepFinish` callback is also added to: - `DefaultStreamTextResult.toUIMessageStream()` (used by the methods above) - `DefaultStreamTextResult.toUIMessageStreamResponse()` - `DefaultStreamTextResult.pipeUIMessageStreamToResponse()` Callers which want to receive the `ToolLoopAgentOnStepFinishCallback` can easily do that with the `onStepFinish` callback to the constructor of the `ToolLoopAgent` that they're passing to the methods above. Fixes vercel#12841.
4 tasks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.

Background
For things such as token tracking, it would be helpful if callbacks can be registered on a per-call basis. See #11468
Summary
Add
onStepFinishcallback support toAgent.generate()andAgent.stream()Related Issues
Resolves #11468