https://discord.gg/botpress logo
Fetch KB results on every turn
# 🤝help
s
Hey there 🙂 Is there a way to fetch kb results after every incoming message from a user, before anything else runs? And if so is it possible to just fetch chunks from the RAG or the Knowledge Agent to run a query? Maybe with a hook and saving the result to a variable? It would greatly improve our architecture bc doing that plus calling other tools depending on context often confuses our LLM, even gpt 5.1 or claude sonnet 4.5 We did a lot of prompt engineering but eventually we anyway need to call the rag everytime and would rather loose a couple cents here before delivering wrong answers. Help is greatly appreciated! 🙂
b
Yes, the way to do this reliably is to move retrieval outside the agent logic entirely. In production we stopped letting the LLM decide when to call RAG. Instead, every incoming user message first goes through a pre-message hook / middleware that runs a deterministic vector search (chunk-level only, token-capped). The retrieved chunks are saved to a variable (e.g. retrieved_context) and injected into the system prompt before the model runs. At that point RAG isn’t a “tool” anymore, it’s just data the model always sees. This removes tool-ordering issues and avoids the model skipping retrieval under cognitive load (which happens even with GPT-5.x / Sonnet). If retrieval returns empty, we explicitly instruct the model to say it lacks sufficient information. Prompt engineering helps, but deterministic retrieval beats prompting every time. Paying a few extra cents to always retrieve is the correct tradeoff if correctness matters.
s
That's exactly what I had in mind too now. If you are dealing with a very fine knowledge that even GPT doesn't have you need to first give the LLM a little bit of context to even understand what the words mean, it is bombarded with.
Can you share how you retrieve chunks in botpress through a hook?
b
@square-restaurant-14664 Exactly, for domain-specific or proprietary knowledge, retrieval isn’t optional. Without upfront context, the model doesn’t even have the semantic grounding to reason correctly. In Botpress specifically, we handle this with a pre-message hook rather than inside the agent flow. The hook intercepts the incoming message, runs a KB query (chunk-level, top-K only), and stores the result on the event/state (e.g. event.state.retrievedContext). That variable is then injected into the system or instructions node before the LLM executes, so retrieval always happens before any reasoning or tool logic. From the model’s perspective, the knowledge is just part of the context not a callable tool. We also cap tokens and avoid document-level fetches to keep attention focused. If nothing relevant is returned, the instructions explicitly allow the model to respond with “insufficient information.” The key is keeping retrieval deterministic and outside the agent loop. Once you do that, confusion between tools largely disappears.
s
ok sorry, but that sounds almost like an AI-generated answer. ^^ Could you share an actual example of how this is done? Did you do it directly through botpress or setup your own RAG on the side and call it via API?
w
yes, use the raw input capture
it runs a kb search for some reason so perfect for your use case
s
Wow, super interesting work-around! Thanks!
Is there a way to manipulate that into running without direct user-interaction? Otherwise we have to always start/redirect to that raw input right? And I guess the knowledge agent configuration then sets the config for that kb-search right?
3 Views