fix: generate final LLM response when max_tool_steps is reached #4747
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.
Summary
When
max_tool_stepsis reached during a tool-calling loop, the agent currently silently stops — the_pipeline_reply_taskreturns without generating a final LLM response. This leaves users (both voice and text) with no feedback after potentially multiple tool executions.This PR changes the behavior so that when the limit is hit, the last recursive call to
_pipeline_reply_taskusestool_choice="none", forcing the LLM to produce a text response using whatever tool results have been gathered so far, instead of silently returning.Problem
In
agent_activity.py, the tool execution loop checks:This means:
Solution
Instead of a hard
return, the fix:max_steps_reachedas a boolean flagmax_steps_reachedto the existingtool_choiceconditiontool_choice="none"forces the LLM to respond with text only (no more tool calls)The tool results from the final step are still added to the chat context, so the LLM can reference them in its response.
Changes
File:
livekit-agents/livekit/agents/voice/agent_activity.pyreturnwith amax_steps_reachedflagmax_steps_reachedto thetool_choicecondition alongside the existingdrainingandmodel_settings.tool_choice == "none"checksBefore (current behavior)
After (with this fix)
Testing
tool_choice="none"parameter is already used in the same code path fordrainingscenarios_pipeline_reply_taskrecursive call is reusedmax_tool_stepsset high enough to never hit the limit see no change