I’ve been following the discussions about n8n and I’m really impressed with what can be built using it. I’m currently working on a project for a supermarket, where I created a flow that connects to a PostgreSQL database via SSH and answers questions about revenue and other data. It’s coming along nicely, but sometimes the AI ends up “dumbing down” and not responding as expected.
To keep the results consistent, I added in the prompt the exact same queries used by the POS system, so the outputs match the official POS reports. The strange thing is: sometimes it executes the queries correctly, and sometimes it doesn’t. I’ve also tried different prompt formats (markdown, yaml, json…), and results vary. I feel like it’s probably just a simple adjustment I’m not seeing yet.
Has anyone here faced something similar or have best practices to share on improving this kind of flow in n8n? Any insights would be super appreciated
I’ve had success when giving the agent the schema, column names, types, etc. You can also try using multiple agents instead of one. The first chooses the most relevent column names. The second one writes the query.
Here’s a template that I have that works pretty well.
If this response helped you, please click the heart to show that it is useful If this response solved your issue, mark it as the solution to help the community
No I tried going down that path more or less, but it didn’t work either. So I ended up putting the query directly in the prompt, telling it to always run the query whenever I ask something related to that specific agent (like billing, for example). I pasted the query inside so it executes it. The problem is: sometimes it runs correctly, sometimes it doesn’t run at all, and sometimes it just makes up data.
I have several agents – financial, purchasing, accounts payable, etc. – and each one has its own query. I’m using the ExecuteQuery tool for this.
Here’s an example of my setup: I have an orchestrator that routes requests to the right agent based on keywords. For example: “How much did I sell today?”, “What’s the best-selling product?”, “Which bills do I need to pay?” — and it directs them accordingly.
Inside each agent I have a prompt. Below is an example of my Sales agent prompt, where I include the query it should execute. The issue is: sometimes it runs the query correctly, sometimes it doesn’t run at all, and sometimes it just makes up data. I honestly don’t know what else to do.
I’ve already tested several models… DeepSeek has been working well for me, especially since it’s much cheaper.
The problem is that the AI sometimes gets lost, no matter the model — sometimes it hallucinates data, sometimes it doesn’t trigger the execute query tool, other times it runs the query I hard-coded in the prompt, and sometimes it just ignores it.
Feels like it has a mind of its own, I honestly don’t know what else to do haha.