Agents and Tool Use in AI Systems
A RAG system works in two steps: it first retrieves relevant information from external memory, then uses that information to generate a response.
Why a Model Needs Outside Material
What do you think happens?
A user asks, using the word automobile, about a policy. The only relevant document says car instead of automobile. Will a retriever that works by matching words necessarily find it?
Reveal answer
Not necessarily. A term-based retriever decides relevance by looking at the words themselves, so a document that shares no terms with the query gives it little to match on.
The source states that the term-based approach is only as good as the vocabulary overlap between the question and the documents. An embedding-based retriever has the potential to recognize relevance even when the wording differs, which is one reason it can outperform term matching.
A language model answers questions using two sources: what it absorbed during training, and whatever text appears in its current input. Neither source is ideal. Training knowledge goes stale, and the input has a limited size, so a model cannot simply be handed an entire company's documents and asked a question about them. Retrieval-Augmented Generation, usually shortened to RAG, is a design pattern that sidesteps both limits.
Instead of relying on memory alone, a RAG system first looks up material that might be relevant and then places that material in front of the model. This article focuses on the first half of that arrangement, the retriever, because the quality of a RAG system is tied closely to how well the retriever does its job.
The Two Stages of RAG
A RAG system works in two stages. In the first stage, it retrieves information from an external memory, which is a store of documents, passages, or records that exists outside the model. In the second stage, it passes the retrieved material to the model, which uses it to generate a response. The retrieval stage supplies the facts, and the generation stage turns those facts into an answer.
Follow the arrows in the diagram. The query never reaches the model directly as a request to be answered from memory. Instead, the retriever uses it to search the external memory, and only the material it finds is passed forward. The final response is produced by the model, but its factual basis comes from what the retriever supplied. The result is expected to be more accurate than what the model could produce from its own memory, because the answer is anchored to material the system actually found.
Tracing One Query Through a RAG System
Following a Question Through the Two Stages
A company stores its travel policy, expense rules, and holiday schedule as separate documents in an external memory. An employee asks: How many days of leave can I carry over to next year? Trace what the system does.
- 1Receive the query
The question enters the system. The model itself has not been trained on this company's current policy, so its memory alone is not a reliable source.
- 2Stage one: retrieve
The retriever searches the external memory for material that might be relevant. Documents about leave and carry-over are candidates; the holiday schedule may score lower because it shares fewer relevant terms with the question.
- 3Hand off the material
The retrieved passages are placed into the model's input. The model does not search for them itself at this point; the system has already done that work.
- 4Stage two: generate
The model reads the retrieved passages and writes an answer. Because the answer is built from the leave policy it was given, it is anchored to material the system actually found.
The response is generated by the model, but its facts come from the retrieved leave policy. If the retriever had missed that document, the model would have had no reliable basis for the answer.
How a Term-Based Retriever Decides Relevance
A term-based retriever decides relevance by looking at the words themselves. The process runs in a sequence that you can follow step by step.
- 1Break the query into its terms.
- 2Compare those terms against the words in each stored document.
- 3Favor documents that contain the same terms as the query.
- 4Give more weight to documents where the matches are strong, rather than treating every shared word the same.
- 5Rank the documents so that the strongest term matches appear first and count as the most relevant.
Systems such as Elasticsearch and the BM25 ranking method are examples of this approach. Their main practical advantage is that they are much lighter to set up and run. They also often give a solid starting point against which more advanced retrievers can be measured. The limitation follows directly from the method: the approach is only as good as the vocabulary overlap between the question and the documents. A relevant document written with different words can be missed.
Vector Search with Embedding-Based Retrievers
An embedding-based retriever takes a different route. Rather than comparing words directly, it relies on vector search. Items are represented as vectors, and the system looks for the stored vectors that are closest to the query's vector. Because the comparison happens in this representation space, the approach has the potential to recognize relevance even when the wording differs.
The diagram shows the lookup. The query is converted into a vector, and the system measures how close that vector sits to each stored document vector. The document whose vector lies closest, here Document B, is the strongest candidate. The search does not depend on the documents sharing the same words as the query; it depends on where the vectors sit in the shared space.
The cost is computational. Embedding-based retrieval is more computationally intensive than term matching, so the system does more work for each lookup. Vector search is not a technique invented only for language models. It is also the backbone of many core internet applications, including search and recommender systems, and many vector search algorithms developed for those applications can be reused in RAG.
Comparing the Two Retriever Families
The two families differ along several dimensions at once: what they match on, how much effort they require, and where their advantages appear. Seeing them side by side makes the choice easier to reason about than reading about each separately.
| Dimension | Term-based retriever | Embedding-based retriever |
|---|---|---|
| What it matches on | The words and terms in the query and documents | Vectors representing items, compared in a shared space |
| Typical examples | Elasticsearch and BM25 | Vector search over stored document vectors |
| Cost to run | Much lighter to set up and run | More computationally intensive for each lookup |
| Main strength | A solid baseline against which others can be measured | Potential to recognize relevance when wording differs |
| Main limitation | Only as good as the vocabulary overlap | Requires more work per lookup |
Term-based and embedding-based retrievers compared along the dimensions described in the source.
Because the generator can only work with what the retriever supplies, the retriever largely determines RAG quality. A stronger generator cannot recover a relevant document that the retriever never returned. This is why the choice of retrieval method is a central design decision rather than a minor implementation detail.
RAG as a Special Case of an Agent
RAG can be understood as a special case of an agent in which the retriever is one of the tools the model is allowed to use. Both RAG and agentic designs let a model get past its context limit and stay current, because both bring in outside information. The agentic pattern goes further. An agent is defined by its environment and by the tools it can access. Inside an AI-powered agent, the model acts as the planner: it analyzes the task, considers several possible solutions, and selects the most promising one.
Read the map from the top down. The language model is the planner inside an agent. An agent gains capability through the tools it can access, and RAG is one arrangement of that pattern in which the retriever is the tool. The same map also shows the cost of this flexibility. Tool use exposes the agent to a range of security problems, and because the retriever is itself a tool in the RAG-as-agent view, the same concern applies to the retrieval step.
RAG and agents share one more feature: both are prompt-based methods. They shape the model's output solely through what goes into its input, without changing the model's internal weights. That is what separates them from approaches that modify the underlying model itself.
Why More Automation Adds Risk
Capability and exposure rise together. The more tools a model is given, the more capabilities it has, which lets it take on harder tasks. Planning also matters, because a complex task may need many steps and the model must be powerful enough to plan them. Planning can be strengthened with reflection and with a memory system that keeps track of progress.
As an agent becomes more automated, the consequences of its failures can become more severe. For agents to operate reliably in the real world, rigorous defensive mechanisms must be in place before they are trusted with consequential work. The source does not reduce this to a single rule; it treats automation, tools, and risk as a package that must be managed together.
Mistakes About Retrievers and Automation
Common mistake Assuming a stronger generator will fix weak retrieval
What it looks likeA capable model is given only loosely related passages and produces a confident but poorly anchored answer.
Why it failsThe generator can only work with what the retriever supplies, so it cannot recover a relevant document that was never returned.
Do this insteadTreat the retriever as the main determinant of RAG quality and evaluate it directly.
Common mistake Believing term matching finds documents by meaning
What it looks likeA query uses automobile, and the only matching policy says car, so a term-based retriever ranks it low.
Why it failsA term-based retriever favors documents containing the same terms, so it is only as good as the vocabulary overlap.
Do this insteadRecognize the limitation, and consider an embedding-based approach when wording differences matter.
Common mistake Assuming embedding-based retrieval is always better
What it looks likeA team switches to vector search for every lookup without considering the extra work each lookup requires.
Why it failsEmbedding-based retrieval has the potential to outperform term-based methods but is more computationally intensive, and term-based systems are much lighter to run.
Do this insteadWeigh matching quality against cost, and use term-based methods as a baseline for comparison.
Common mistake Treating more tools as free capability
What it looks likeAn agent is given many tools to handle harder tasks, and no one considers what happens if one tool is misused.
Why it failsTool use exposes an agent to a range of security problems, and more automation can make failures more severe.
Do this insteadPut defensive mechanisms in place before trusting the agent with consequential work.
Trace the Retriever
A RAG system is asked about a refund rule. The retriever returns a document about shipping times, which shares no terms with the query, and the document that actually describes refunds is not returned. Using the two-stage model, explain what the generator will be able to do, and identify which stage failed and why.
Hints
- Ask which stage supplies the facts and which stage turns them into an answer.
- Recall that the generator can only work with what the retriever supplies.
- Consider how a term-based retriever decides relevance and what happens when vocabulary does not overlap.
Key Takeaways in Review
- 1A RAG system works in two stages: it first retrieves relevant information from an external memory, then passes that material to the model to generate a response.
- 2A term-based retriever such as Elasticsearch or BM25 favors documents that share terms with the query. It is lighter to run but depends on vocabulary overlap.
- 3An embedding-based retriever represents items as vectors and finds the closest ones. It can recognize relevance across different wording, but it is more computationally intensive.
- 4The generator can only work with what the retriever supplies, so the retriever largely determines RAG quality.
- 5RAG is a special case of an agent in which the retriever is a tool. More automation and more tools bring greater capability along with more severe failure and security risks.
At a glance
- A RAG system retrieves relevant material from external memory, then the model generates a response from that material.
- Term-based retrievers match query terms against documents and are lighter to run; embedding-based retrievers compare vectors and can recognize relevance across different wording at higher computational cost.
- The generator can only work with what the retriever supplies, so the retriever largely determines RAG quality.
- RAG is a special case of an agent in which the retriever is a tool, and more automation with more tools increases both capability and security risk.