Conversation
…ted response A response the provider stopped at max_output_tokens can still carry a function_call item whose arguments are whatever prefix fit in the budget. The loop parsed that fragment, failed, fed the parse error back to the model, and issued another request against the same exhausted budget, so every truncated turn cost one wasted round trip. extractToolCallsFromResponse and responseHasToolCalls now yield no tool calls when status is incomplete with reason max_output_tokens. The loop finalizes on the truncated turn and the caller sees incompleteDetails.
…ore the cut-off Review feedback: a parallel turn can complete one function_call and be cut off at max_output_tokens during the next. Rejecting the whole response discarded the completed call. Filter per item instead: on a truncated response a function_call is executable iff its status is completed (arguments must parse when the provider omits status). The dropped item is also excluded from state and from the next request's input, so no call is echoed without an output.
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.
Problem
When the provider stops a turn at
max_output_tokens, the response can still carry afunction_callitem whoseargumentsare whatever prefix fit in the budget (production capture: a reasoning model spent 3,000 tokens thinking and was cut off at{"commands":).responseHasToolCallsandextractToolCallsFromResponseonly look atoutput[].type, so the loop parsed the fragment, loggedFailed to parse tool call arguments, fed the error back to the model, and made another request against the same exhausted budget. Each truncated turn cost one wasted round trip, and the run only ended when a stop condition fired.Fix
New
outputItemsWithoutTruncatedCalls(response): on a response withstatus === 'incomplete'andincompleteDetails.reason === 'max_output_tokens', drop everyfunction_callwhose itemstatusis notcompleted(when the provider omits status, the arguments must parse). On any other response it isresponse.outputunchanged.extractToolCallsFromResponseandresponseHasToolCallsread through it, andmodel-result.tsuses it wherever a response's output is written to state or echoed into the next request, so a dropped call is never sent back without an output. Calls that completed before the cut-off execute normally, matching the Vercel AI SDK and OpenAI Agents SDK loops. With no surviving call the loop finalizes on the truncated turn and the caller seesincompleteDetails, the right signal to raise the budget.Tests
tests/unit/max-output-tokens-truncation.test.ts:callModelagainst a mockedbetaResponsesSend: fully truncated turn finalizes after 1 request with nothing executed (without the fix: 5 requests beforestepCountIs(3)); mixed turn executes the completed weather call once, never the shell call, and the follow-up request's input carriesfunction_call/function_call_outputfor the weather call only.Context: OpenRouterTeam/openrouter-web#43149 carries this as a patch on
@openrouter/agent@0.10.0until the next release.