ai icon indicating copy to clipboard operation
ai copied to clipboard

Handle tool-call batches cut off by the output token limit as a first-class outcome

Open uditkumar01 opened this issue 1 month ago • 4 comments

Description

Feature request. With parallel tool calling, a model can write a large batch of tool calls in one response and run into the output token limit. The step finishes with length, the last tool call is usually cut off mid-JSON, and since #19066 none of the complete calls run. The SDK reports this as a normal finish, so applications have to detect and explain the situation themselves. Four related gaps, all from that one scenario:

1. A truncated turn looks like a clean finish to UI clients. The UI message stream outcome is completed, and no error or warning chunk is sent. The finish chunk carries finishReason: 'length', but nothing prompts a client to act on it. A way to treat length as its own outcome (for example a distinct outcome status, or a callback) would let chat UIs show that the answer was cut off.

2. There is no "not run" state for tool parts. A tool call that never ran can only be represented as output-error, which reads as "the tool failed". The tool did nothing wrong. A skipped or not-run state in UIToolInvocation, carrying the finish reason, would let UIs show "Not run (output limit)" without custom code.

3. Truncated tool input is not distinguishable from malformed input. The cut-off call fails with a generic JSON parse error (Expected ',' or '}' ...) wrapped in InvalidToolInputError. When the step's finish reason is length, the SDK could mark that error as truncated (for example truncated: true), so callers do not have to match message text, and so repairToolCall implementations can decide whether a repair is safe.

4. Nothing prevents or recovers from the truncated batch. There is no way to cap how many tool calls a step may contain, and no warning when the output budget is small relative to the batch. After the cut-off the loop ends and the complete calls are discarded. Options that would help: a per-step tool call limit, or an opt-in continuation that tells the model its calls were not run because of the output limit and lets it retry with a smaller batch. Running the complete calls from a truncated step would conflict with #19066, so the continuation would need to stay within that rule.

These are suggestions, and I am happy to split this into separate issues if that is easier to track. The related bug, skipped tool calls being left pending, is reported in #22912.

AI SDK Version

  • ai: 7.0.137

uditkumar01 avatar Oct 11 '26 10:10 uditkumar01

Identify issue type

Identified as Feature
Confidence: High

Rationale

This requests new outcome modeling, recovery controls, and UI/tool-call states rather than reporting a failure of currently promised behavior. The concrete bug is explicitly tracked separately.

Evidence

  • The issue is explicitly framed as a feature request for first-class handling of tool-call batches truncated by output limits.
  • It proposes new API/state capabilities such as a distinct length outcome, a skipped/not-run tool state, structured truncation metadata, per-step tool-call limits, and opt-in continuation.
  • The reporter separates the related pending-tool-call bug into issue #22912.

ai-sdk-factory[bot] avatar Oct 11 '26 10:10 ai-sdk-factory[bot]

Feature Analysis

Feature request should be split.

Issue #22914 is only partially supported: finishReason: 'length' reaches finish callbacks, unsafe-finish tool execution is suppressed, and OpenAI Responses has provider-specific guardrails, but the request should be split into independently valuable features for a truncation-specific UI outcome so chats can explain cutoffs, a not-run tool state so UIs accurately represent suppressed calls, structured truncation metadata so repair logic can distinguish incomplete JSON, provider-neutral batch limits or budget warnings to prevent truncation, and opt-in safe continuation to recover without executing calls from the truncated step.

ai-sdk-factory[bot] avatar Oct 11 '26 10:10 ai-sdk-factory[bot]

The “complete calls must also stay suppressed when the step ends with length” constraint is worth retaining in any continuation design. A parseable earlier call is not evidence that the whole step completed safely.

I maintain prefix-safe-json, a caller-owned execution guard. I ran an installed-consumer comparison with your exact [email protected], [email protected], and [email protected]: two interleaved complete calls produce two harmless counter effects; a complete first call plus a truncated second call at length produces zero effects. The raw deltas remain associated with their call IDs, SDK callbacks stay disabled, and dispatch only consumes one-shot authority after draining fullStream.

Runnable batch comparison and project trial guide.

Would you be interested in comparing that caller-owned boundary in your own application's development branch with a harmless counter? This is optional: the SDK's existing unsafe-finish suppression already addresses execution inside the SDK. The comparison does not implement the UI outcome, not-run state, batching limit, or continuation features requested here, and the mock run does not establish live-provider compatibility.

canblmz1 avatar Oct 14 '26 16:10 canblmz1