How does gemini-cli achieve multi-turn conversations? #4106
-
Normally, after a user sends a request, the API model only returns one message. If there is tool calling in this message, it should be executed, and then the execution result and context should be sent to the LLM API for a second interaction. However, gemini-cli clearly handles this logic. The user only needs to input one request, and gemini-cli will think - execute tool - execute tool - output. This should involve multiple API calls. I read the source code, and it seems that the code related to this logic is here: turn.ts. But I still haven't seen where this logic is: "Analyze user input - execute tool - store the tool's results in memory - combine the previous tool results and context and send another request to the LLM," especially where does the re-request logic occur? Thank you very much! |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 1 reply
-
I was going to type up an answer but then I thought - why not let Gemini CLI itself answer this question: You've asked a great question, and your intuition is correct. The logic for handling the multi-step "think -> execute tool -> repeat" flow is indeed a core part of gemini-cli. You were close with Here is a breakdown of how it works:
In summary, the re-request logic happens when the while loop in runNonInteractive completes an iteration. If any tools were called, their outputs are packaged into a new message (currentMessages) which is then sent back to the API in the next iteration of the loop. This continues until the model responds with text instead of tool calls. |
Beta Was this translation helpful? Give feedback.
-
BTW, answers to half ot the questions here and to https://github.com/google-gemini/gemini-cli/issues could be automated thereby: let Gemini Core AI 🤖♊ (or GitHub AI etc) answer ASAP them, simply... |
Beta Was this translation helpful? Give feedback.
-
@jerop for auto-triaging of issues as well |
Beta Was this translation helpful? Give feedback.
I was going to type up an answer but then I thought - why not let Gemini CLI itself answer this question:
You've asked a great question, and your intuition is correct. The logic for handling the multi-step "think -> execute tool -> repeat" flow is indeed a core part of gemini-cli.
You were close with
turn.ts
, but the main re-request loop you're looking for is inpackages/cli/src/nonInteractiveCli.ts
.Here is a breakdown of how it works:
Entry Point (
gemini.tsx
): When you run the CLI with piped input or a command-line question (i.e., non-interactively), the main function in gemini.tsx calls runNonInteractive.The Core Loop (
nonInteractiveCli.ts
): The runNonInteractive function contains…