Add onFinal lifecycle#16962
Description
The AI SDK currently has fragmented lifecycle callbacks (onEnd, onError, onAbort) that run in different scenarios, making it difficult to implement single-point cost tracking and cleanup logic.
Users who want to persist final cost data must duplicate logic across multiple callbacks and external try/catch blocks because:
onEnd only runs on successful completion onError runs when errors occur, but errors are still thrown afterward onAbort runs separately for aborts Using onStepEnd for cost tracking would insert one database row per step, increasing database load
plz add it for streamText, generateText and loop agent class
Always runs at the end of a request, regardless of success, error, or abort Receives all completed steps, usage, abort/error state, and final metadata Runs before onEnd/onError/onAbort (following the LangChain adapter pattern) When provided, suppresses direct throwing of handled request lifecycle errors (calls onError then onFinal instead)
AI SDK Version
No response
Code of Conduct
- I agree to follow this project's Code of Conduct