PARAS MADAN
All writing

Latest in AI

How to tell why a Vercel AI SDK tool loop stopped

AI SDK 7.0.181 adds stoppedBy to generation and streaming results. Use it to separate a model finish from a step limit or a pending tool.

A tool can return successfully on the last allowed step while the assistant never writes the answer. If your app only records finishReason, that run can look like an ordinary model tool call. In Vercel AI SDK 7.0.181, released November 23, stoppedBy tells you what ended the tool loop as a whole.

Record both fields. One describes the final model step; the other describes why the SDK didn't start another step.

Read the two fields together

Consider a fixture that looks up document titles. The model requests a lookup, the tool returns a title, and your isStepCount(1) limit prevents the model from writing a summary. That last model step can report finishReason: "tool-calls" while the run reports stoppedBy: "stop-condition". A successful lookup alone doesn't make the summary complete.

The tagged loop-control guide documents four values:

  • model: The loop ended at the model step without continuing through a tool. Inspect finishReason for details. An output length limit can also land here, so don't treat this value as proof that the answer was complete.
  • stop-condition: A configured stopWhen condition ended the loop. Check which limits your application configures before assuming it was the step count.
  • tool-without-execute: The SDK needs a result from a tool that has no server-side execute function. If you designed the tool for your client to handle, a pause may be expected. Otherwise, check how you registered it.
  • approval: A tool call is awaiting approval. Show the approval state instead of counting the run as complete or automatically retrying it.

Collect it where the run completes

With generateText, read result.stoppedBy after awaiting generation. With streamText, the stream reference describes three surfaces: onEnd, the final finish part on fullStream, and the promise-like result.stoppedBy.

If you're already iterating the full stream, use the final part for your summary. If you summarize on the server, use onEnd instead. Pick one place to record the run so the finish part and callback don't inflate your metrics. A stream aborted by the caller can reject when you await stoppedBy; keep cancellation separate from a deliberate stop condition.

The field is set by the generation functions and ToolLoopAgent, but the release's loop guide says WorkflowAgent and HarnessAgent don't report it yet. Handle a missing value explicitly rather than mapping it to model.

Test without a live model

Stub the model to return a tool call to your read-only title lookup. With isStepCount(1), assert that stoppedBy is stop-condition and that the app doesn't mark the summary as complete. Run a second case where the model returns text after the lookup and confirm that the app distinguishes it from the capped run.

If you support streaming, verify that the final part and the result carry the same reason. The SDK's tagged tests use a mock model to cover those paths and the pending-tool cases. Record the stop category and step count in your own telemetry; you don't need prompts or tool arguments to diagnose this difference.

Need help documenting a developer release?

I'm Paras Madan. I help AI teams write technical guides and product stories that show developers what changed and how to test it. Book a 30-minute call.