Fix/duplicate llm call#2
Open
Rodrigosilvars83 wants to merge 2 commits into
Open
Conversation
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
Every chat message triggers two LLM API calls and the frontend receives
the response twice — once as a single
tokenevent from the agent loop'snon-streamed call, and again chunk-by-chunk from the trailing stream call.
This causes:
different answer than the first.
Cause
In
chat_completion_stream, when the LLM returns a final answer (notool_calls), the loop yieldsmessage.contentand breaks. But controlthen falls through to an unconditional second call
(
self.client.chat.completions.create(..., stream=True)) that re-asksthe LLM and yields the same response again.
Fix
Track whether the agent loop produced a final answer (
finalized). If itdid, reuse the content that was already yielded and skip the streaming
call. The streaming call now only runs when the loop exhausted
MAX_AGENT_TURNSwithout finalizing — acting as a fallback to force afinal answer.
Notes
ifblocks that insert the systemprompt,
continuefor unknown tool calls, andfor _ in(unused loop var).Testing
Manually traced the control flow for three scenarios: