Latest / AI Fire Daily / #317 Neil: Build Error-Free RAG LLM Agents In n8n With This Expert Framework
Transcript
- 0:00Have you ever spent weeks engineering an AI agent
- 0:04only to have it confidently deliver an answer
- 0:07that was just demonstrably catastrophically wrong?
- 0:10It's the paradox of this current AI generation,
- 0:12isn't it? The delivery is perfect, but the information
- 0:15is just terrible. You ask for Q4 revenue, you
- 0:18get a number off by a factor of 10. Or you ask
- 0:21for a contract summary, and it misses the one
- 0:23single liability clause that matters. Exactly.
- 0:26It's a hallucination, but it's delivered with
- 0:28this conviction of a seasoned expert. Welcome
- 0:31back to the Deep Dive. Today, we are tackling
- 0:33that exact engineering frustration. For anyone
- 0:36out there building agents, the secret we found
- 0:38is that the flaw, well, it's rarely the language
- 0:41model itself. No. The problem is almost always
- 0:43rooted in how we feed the data to our systems.
- 0:46Which is what we're calling context engineering.
- 0:48Precisely. It's the whole process of ensuring
- 0:51your retrieval augmented generation, your RAG
- 0:54system, has the exact information in the right
- 0:57format at the precise time it needs it. So RAG
- 1:00is basically giving your LLM some external knowledge
- 1:03to keep it grounded. Yes, and our mission today
- 1:06is to help you transform your agents from these
- 1:09confident guessers into truly verifiable analysts.
- 1:15OK, let's unpack this. We need to move beyond
- 1:17those intro tutorials that always seem to lead
- 1:20to these frustrating failures. For sure. We're
- 1:22going to cover four powerful, specific solutions.
- 1:27We'll start with a deep dive into why the basic
- 1:29default methods fail so badly. Then we'll get
- 1:33into the reliable methods, starting with structured
- 1:35data simple filters and SQL queries. And finally,
- 1:38we'll define when you should actually use full
- 1:40context retrieval or when that traditional vector
- 1:43search is still the right tool for the job. Let's
- 1:46do it. All right. So when you first start building
- 1:47these RIG systems, the default method everyone
- 1:50learns is chunk -based retrieval. It's usually
- 1:52powered by vector search. Great. You take a massive
- 1:54document, you cut it up into these little pieces,
- 1:57these chunks. And then the AI only gets to see
- 1:59a handful of them. maybe the three or four things
- 2:02are most relevant. It's like stacking scattered
- 2:04Lego blocks of data and just hoping they form
- 2:06a perfect blueprint. That standard, that fragmented
- 2:10approach, it basically guarantees three critical
- 2:14issues. The first one is the most common, missing
- 2:16context. The little fragments, they might contain
- 2:19keywords, sure, but they lack all the surrounding
- 2:22narrative. The setup, it's like, if you were
- 2:25summarizing a 100 -page book and I only gave
- 2:27you pages 12, 45, and 88. You'd have no idea
- 2:31what's going on. You couldn't possibly grasp
- 2:33the main character's motivation. You'd miss the
- 2:36crucial setup from chapter one. The LLM, when
- 2:39it's faced with these disconnected pieces, it's
- 2:42just forced to guess. Yeah, and I can offer a
- 2:45vulnerable admission here just based on my own
- 2:47early failures. The second issue is what I call
- 2:49the math wall. Oh, yeah. Language models are,
- 2:52I mean, they're incredible text processors, but
- 2:54they are fundamentally terrible at complex, verifiable
- 2:58math. They're calculators that are bad at math.
- 3:00Exactly. I once asked my internal agent for Q3
- 3:03sales figures from this sprawling 2000 line report.
- 3:06It pulled four records that happened to contain
- 3:09the word total. And it just added those four
- 3:11up. It summed those four lines and presented
- 3:14that tiny result. as our company's entire quarterly
- 3:18revenue. And that is the core absurdity, right?
- 3:20The agent calculated just a tiny fraction of
- 3:23the necessary data, presented the result with
- 3:2699 % confidence. And you have to go use external
- 3:29tools just to realize it's completely broken.
- 3:31It happens because that chunking process, it
- 3:35prioritizes what sounds similar over actual data
- 3:39integrity. Right. And that leads to the third
- 3:41flaw, which is the problem of bad summaries.
- 3:44Yeah. So you need a comprehensive chronological
- 3:46summary of a 50 page technical manual. If your
- 3:49system just randomly hands the AI four paragraphs
- 3:52from all over the document. The summary is going
- 3:54to be useless. It's like trying to write a detailed
- 3:55review of a three hour documentary after only
- 3:58watching like 30 seconds of the trailer. You
- 4:00lose the plot. the sequence, everything. So if
- 4:02we accept that these math errors and logic gaps
- 4:04are so easily created by fragmentation, how should
- 4:07a developer even approach verifiable accuracy
- 4:10at this point? Well, math errors are just inherently
- 4:12masked by the LLM's confidence. You have to force
- 4:16the calculations onto external specialized computation
- 4:19engines. OK. So let's pivot. Let's talk about
- 4:21the first real solution, moving away from all
- 4:23this guesswork. Right. And this one is specifically
- 4:25for structured data. So data that lives in neat
- 4:29rows and columns, think an internal dashboard
- 4:32or even just a Google sheet. Instead of asking
- 4:35the AI to search and guess, we teach it to operate
- 4:38more like a database admin. We teach it to filter.
- 4:41And this approach relies on what's called function
- 4:43calling, or defining these very specific tools
- 4:46the AI can use. We don't ask it to read the data.
- 4:49We ask it to construct a precise query. So for
- 4:52a sales database, for example, we define functions
- 4:55like product name query or date query. And the
- 4:58technical depth here, the really important part,
- 5:00is defining that tool's specification. You have
- 5:02to dictate the exact inputs. So for instance,
- 5:06requiring the date to be strictly in that yyymmdd
- 5:09format. That input validation is so crucial.
- 5:12It is. And we had to give the agent a core system
- 5:15prompt, an instruction that says, always use
- 5:18the most specific tool available for the user's
- 5:20intent. And that instruction, it turns the agent
- 5:23into this highly disciplined engineer, not some
- 5:25creative writer trying to guess what you mean.
- 5:27Exactly. So consider a real -world query. How
- 5:30many wireless headphones were sold in the Northeast
- 5:32region on 2025 -0915? The agent doesn't search
- 5:38a vector database. It sees the query. It identifies
- 5:41the need for filters. And it just chains the
- 5:43tools together. So it uses product name query,
- 5:46then region query, then the date query to narrow
- 5:48everything down. And then finally, it hands that
- 5:51small, perfect data set to a dedicated calculator
- 5:54or computation engine to just sum the results.
- 5:57The outcome is 100 % accurate. Because the heavy
- 6:00lifting was done by precise data filtering, not
- 6:02by linguistic guesswork. Right. But I should
- 6:05say, for listeners who might not have a deep
- 6:06programming background. Yeah. The simplicity
- 6:08of that outcome, it really masks the complexity
- 6:11of the setup. I mean, defining those tools and
- 6:13making sure they don't suffer from prompt drift,
- 6:16where the agent just starts ignoring your instructions
- 6:18over time. I still wrestle with that myself.
- 6:21It's a constant battle. It absolutely is. So
- 6:23what makes implementing these specific filters
- 6:25superior to just using generalized chunking,
- 6:28even for really large structured data sets? Filters
- 6:32provide targeted data access, which eliminates
- 6:35the risk of data fragmentation and guarantees
- 6:37100 % verifiable calculation inputs. OK, so filters
- 6:41are great for selection. But what if you need
- 6:43more complex calculations, things like generating
- 6:46averages or standard deviations or sorting complex
- 6:48reports. Yeah, the AI is going to fail those
- 6:51manual calculations every single time. And this
- 6:54is where we bring in the language of databases.
- 6:57SQL. So think of this as giving your ARAGAY LLM
- 7:00a superpowered calculator that's optimized for
- 7:02massive data operations. Exactly. The process
- 7:05is pretty elegant. The LLM writes the SQL code,
- 7:08maybe a complex group PO by Y function for a
- 7:10Postgres database, sends that query to the actual
- 7:13database engine. And the engine just returns
- 7:15the single precise result. But the critical engineering
- 7:18detail. The part you cannot get wrong is in the
- 7:21system prompt. You have to include the exact
- 7:24table schema. So you have to explicitly tell
- 7:27the AI, hey, there's a table named sales data,
- 7:29and then list every single column. Customered,
- 7:33INT, total price, float, order date, date. Because
- 7:37if you omit the exact column names or the data
- 7:39types, the AI just guesses. And that leads to
- 7:42immediate execution failure. Right. And that's
- 7:45why this is a step up in complexity from simple
- 7:47filtering. You're actually relying on the LLM
- 7:49to write code. And you have to instruct it with
- 7:52absolute clarity. Never attempt math manually.
- 7:55You force it to delegate the calculation using
- 7:57functions like sum, abg, and group pby. You're
- 8:01pushing all that mathematical load onto the database.
- 8:03That raises a critical point, though. If we're
- 8:05having an LLM write code, doesn't that open us
- 8:08up to huge security concerns like SQL injection
- 8:11vulnerabilities? Isn't it much slower than just
- 8:14fetching the data? That is a crucial technical
- 8:16challenge. Yes, the developer has to implement
- 8:18really strict input sanitization. And you typically
- 8:21have to limit the types of queries the LLM can
- 8:24even construct to prevent malicious injection.
- 8:27But you use SQL for speed on complex aggregation,
- 8:30not simple filtering. When a user asks, who are
- 8:33our top three customers by total spending this
- 8:36year? The SQL script processes millions of rows
- 8:39instantly. Ah, OK. So you need that power when
- 8:43a simple filter just won't cut it for the complex
- 8:45sorting and counting. Exactly. We let the database
- 8:48do the heavy lifting for absolute reliability.
- 8:50So why does providing the specific column names
- 8:53and data types prevent the most common SQL failures
- 8:56in these agents? Without the exact schema, the
- 8:59AI guarantees query failure by producing type
- 9:01mismatch errors or ambiguous column references.
- 9:04Makes sense. Now let's move back to unstructured
- 9:07data. We're talking about those long legal contracts,
- 9:09technical manuals, or sprawling video transcripts.
- 9:14Sometimes you really do need the AI to understand
- 9:16the entirety of a source, not just little fragmented
- 9:18parts. modern large context windows, like 128k
- 9:23tokens, this idea of full context retrieval is
- 9:26not only viable, but it's often the most accurate
- 9:29option. Right, you're giving the model a huge
- 9:32memory, the full text. But there are two different
- 9:35ways to do this and understanding the cost differences,
- 9:37well, it's key. Method A is the simple prompt
- 9:40method. You just copy and paste the entire document
- 9:44into the AI agent's static system prompt. But
- 9:47the cost drawback of that simple method is immediate.
- 9:50And it's heavy. Yeah. Since that long text is
- 9:53just hard -coded into the prompt, you pay for
- 9:56every single one of those tokens, the entire
- 9:58document, every single time the agent runs, even
- 10:00if the user just asks, hello, how are you? You're
- 10:03just burning tokens for no reason. Exactly. And
- 10:05that's why we really recommend method B, the
- 10:08tool -based method. We wrap the entire document
- 10:10inside a tool definition. It's the difference
- 10:12between forcing an employee to carry every file
- 10:15in their arms all day. versus just giving them
- 10:17a key to the filing cabinet. Maximum savings,
- 10:20on -demand access. The agent only fetches and
- 10:23reads that full, expensive document if the user's
- 10:26query specifically triggers that tool. So if
- 10:29the user asks about the document, the agent gets
- 10:31the full content instantly. But if they ask about
- 10:34the weather, it saves the cost. And the performance
- 10:37increase is just stunning. We saw a test where
- 10:39standard vector search created this messy chronological
- 10:42nightmare from a video transcript. But full context.
- 10:45Because it had the entire script, it produced
- 10:48a perfect sequential and comprehensive summary.
- 10:51So if accuracy on a single document is paramount
- 10:54and it fits within that context window, this
- 10:56is the choice. Whoa. Imagine scaling this precise
- 11:00chronological retrieval to a billion queries
- 11:02across complex legal documents without losing
- 11:05a single key detail. The analysis potential is
- 11:08just immense. It guarantees context continuity.
- 11:11So given the cost and the speed trade -offs,
- 11:13why should we always avoid that simpler prompt
- 11:15method for long documents? Avoid the prompt method
- 11:18because you will incur unnecessary token cost
- 11:20when the user's query does not require accessing
- 11:22the document. Okay, so... We've been a little
- 11:25tough on chunk -based retrieval, but let's be
- 11:27absolutely clear. Vector search is not a bad
- 11:30technology. Not at all. It's just deployed inappropriately,
- 11:32like 90 % of the time. Vector search or chunk
- 11:36-based retrieval, it remains the undisputed king
- 11:39for one specific thing. Massive scale. It's the
- 11:42library card catalog analogy, right? You use
- 11:44vector search when you have 10 ,000 documents,
- 11:47a massive library of employee handbooks, old
- 11:49support tickets, internal memos. You simply cannot
- 11:52afford to feed the AI all of them using full
- 11:55context, and they're way too unstructured for
- 11:58filters or SQL. And the beauty of it is the indexing.
- 12:01It embeds all that text into these numerical
- 12:03representations, stores them in a vector store.
- 12:06And can rapidly retrieve the pages that are numerically
- 12:08closest to the user's question. It helps you
- 12:10find the right book or the right page within
- 12:12the right book really quickly. So the distinction
- 12:14is crucial. You use vector search when the answer
- 12:17could be anywhere in this sprawling library.
- 12:19It's amazing for the find the needle in the haystack
- 12:21kind of query. But it's fundamentally terrible
- 12:24for analytical tasks. Things like summarize everything
- 12:28or count the total revenue. So of all these powerful
- 12:31context methods, what unique capability does
- 12:34vector search offer that the others just can't
- 12:37replicate? VectorSearch excels at rapidly indexing
- 12:40and retrieving relevant information from massive
- 12:43sprawling libraries of unstructured documents.
- 12:46Okay, so that brings it all together. How do
- 12:48we decide which of these four methods to use
- 12:51in a real -world scenario? Well, the decision
- 12:53framework is actually incredibly simple. We should
- 12:56always start with this question. How would a
- 12:57human knowledge worker answer this exact question?
- 13:00OK. So if a human would naturally use an Excel
- 13:03filter, a query like, show me all orders from
- 13:05yesterday where the product was blue, then you
- 13:07have to use filters. Right. And if a human would
- 13:09need a pivot table or a complex calculator, a
- 13:12question like, what's the average lifetime spending
- 13:14of our top 10 % of customers, then you must use
- 13:18SQL queries. And if a human would need to read
- 13:20the whole file sequentially, like summarize this
- 13:2240 page contract, noting all termination clauses,
- 13:26they use full context retrieval. And finally,
- 13:29if a human would need to search through a massive
- 13:31filing cabinet, if the answer to, how do I reset
- 13:34my password, is buried among thousands of old
- 13:37user guides, then you use vector search. Okay,
- 13:40but what about the layered complexity? What if
- 13:41you get a query like, summarize the Q1 marketing
- 13:45strategy document for new customers in Texas
- 13:47who spent over $500? That is where context engineering
- 13:51really earns its name. the agent has to chain
- 13:54these things together. First, it uses the filter
- 13:57tool to identify the relevant customers. Texas
- 14:00greater than $500. Then it uses the SQL tool
- 14:04to aggregate and sort their spending trends.
- 14:07And then finally. Finally, it uses the full context
- 14:09retrieval tool to fetch and summarize that single
- 14:12marketing strategy document that was mentioned
- 14:14in the query. It's like a chain of custody for
- 14:16information. That layered approach ensures you
- 14:18get maximum accuracy at every single step. So
- 14:21finally, maybe a few essential technical warnings.
- 14:24Yes. Don't be lazy with your system prompts.
- 14:27Give your tools descriptive, specific names that
- 14:29tell the agent exactly what they do and what
- 14:31data format they require. And never ever combine
- 14:35structured data like numbers, dates, or precise
- 14:38inventory counts with unstructured text. blindly
- 14:42in a vector database. Keep that structured data
- 14:44separate. If you mix your math with your text,
- 14:46you are guaranteeing the mathematical analysis
- 14:48will fail. That feels like the core lesson here.
- 14:50I think so. The main takeaway we've distilled
- 14:53today is that building reliable RRAG LLMs, it
- 14:56isn't about finding the perfect model. It's all
- 14:58about context engineering. It's about ensuring
- 15:01the AI has the exact, verifiable information
- 15:04in the right format at the precise time. We move
- 15:07past all the guesswork and into reliable, accountable
- 15:11analysis. And remember, the best agents are polyglots.
- 15:14They use a mix of these methods. You might filter
- 15:16to narrow down a custom race, then use full context
- 15:19to read their contract, then use SQL to report
- 15:22on their spending. That layered accuracy really
- 15:24is the new standard. So here's your final thought
- 15:27for reflection. Look at your current AI workflows.
- 15:30Ask yourself if you're using that generalized
- 15:32vector search where a simple precise filter or
- 15:34a structured SQL query would give you 100 % more
- 15:37accuracy and auditability. Try changing just
- 15:40one tool in your pipeline this week. Go build
- 15:42something amazing with this knowledge and we
- 15:44look forward to the next deep dive with you.