Synchronizing Retool AI Queries with Webhook Responses in a RAG Chatbot

Hello,

I'm trying to build an AI chat with RAG capabilities, using vector search from an external database (Pinecone). The problem is that when I enter my question in the chat, the Retool AI query starts to run at the same time as the webhook query that triggers the vector search. As a result, the LLM doesn't get the context in time, because the chat starts to generate the response too early. Sometimes it works fine, but other times it doesn't, depending on which query finishes first.

I've tried to delay the Retool AI query from running if the webhook query isn't finished by using the "Disable query" option in the "Advanced" settings, but that doesn't help.

I also tried adjusting the chat's settings by removing the Retool AI trigger query from the "Query to trigger" settings and instead using a Run script event handler that first triggers the webhook query. However, after doing that, the workflow runs as it should, but the response does not get written in the chat for the user.

I also tried triggering the Retool AI query from the webhook query's Success Event Handlers, but that didn't work either.

So my question is: Is there a better way to prevent the Retool AI query from triggering when submitting a message in the chat, and instead trigger the query only after another query has finished first?