tool() definitions, so the only packages you need are ai and zod.
1. Store your key
Create your key on the API key page (it is shown once) and add it to your server environment, next to your model provider’s key:.env.local
2. Define the tools
Each tool starts an Importly job and polls its status until it finishes. A transcript of a video with captions usually comes back within the first request, becausewait holds it open for up to 30 seconds.
lib/importly-tools.ts
maxCostDollars caps what one call can spend. If a job would cost more, it fails with MAX_COST_EXCEEDED and the price instead of running. Raise the caps to suit your app. mode: 'auto' uses the video’s captions when it has them and speech-to-text when it doesn’t.
3. Call the model with the tools
A Next.js route handler that summarizes the video behind a link:app/api/summarize/route.ts
transcribeMedia, reads result.text, and answers in the next step. stopWhen lets it take more than one step, which it needs to use a tool result. Swap in any model your AI SDK setup supports. The same tools object works with streamText for a chat UI. stepCountIs works in AI SDK 5, 6 and 7 (7 also calls it isStepCount).
What each tool returns
The tools hand the job object back to the model:
A job that can’t finish, for example because the video is private, comes back with
status: 'failed' and a code the model can explain to the user (error codes). A long download may still be running when the tool stops polling. For files, a webhook is the better fit in production: start the import from your route, store the jobId, and save mediaUrl when the webhook arrives. The Next.js guide shows that flow.