Becoming an AI Engineer — Part 2: RAG and Agents
AI applications are no longer limited to answering questions. Many applications now need to work with external information, perform multi-step tasks, and interact with other systems. Two important patterns for building these applications are Retrieval-Augmented Generation (RAG) and AI agents.
Why AI Applications Need More Than a Model
A simple model call is useful for answering questions, but it has limitations. A model call by itself does not automatically have access to an application’s private data or external systems, nor can it directly perform actions such as searching a database or updating a record. These capabilities need to be provided by the surrounding application. Real-world applications need more than just a model; they need systems that can retrieve information, determine what actions to take, and execute those actions.
To address some of these limitations, two useful patterns are Retrieval-Augmented Generation (RAG) and AI agents.
Understanding RAG: Retrieval-Augmented Generation
RAG enhances a model’s ability to perform tasks using relevant external information as context. Instead of relying solely on information learned during training, the application retrieves relevant content from one or more knowledge sources and provides that content to the model when generating a response.
How RAG Works
The RAG process involves three main steps:
Retrieval: The user’s query is used to search a knowledge source for relevant content. This may involve vector search, keyword search, hybrid search, metadata filtering, or a combination of these techniques.
Context Integration: The retrieved content is selected and provided to the model as context.
Model Response: The model uses the provided context to generate a response.
Embeddings and Vector Search
One common approach to retrieval uses embeddings. An embedding represents text as a numerical vector that captures patterns and relationships learned by the embedding model, which can be useful for measuring semantic similarity. Documents and queries can be converted into embeddings and compared using a similarity measure to find content that is semantically related.
For example:
Document
↓
Embedding Model
↓
Vector
↓
Vector DatabaseAt query time:
User Query
↓
Embedding Model
↓
Query Vector
↓
Similarity Search
↓
Relevant DocumentsEmbeddings are one retrieval technique, rather than a requirement for every RAG system.
Conceptual RAG Flow:
query = "What is the capital of France?"
query_vector = embed(query)
documents = vector_search(query_vector, top_k=5)
context = combine(documents)
response = model.generate(
prompt=query,
context=context
)The important idea is not the specific framework API. The application first retrieves relevant information and then provides that information to the model as context.
This example demonstrates how RAG integrates external information into a model’s context. Effective retrieval can provide the model with relevant evidence and may improve the quality and factual grounding of its responses. However, retrieval quality directly affects the final result, so RAG does not guarantee correct answers.
Introduction to AI Agents
AI agents are systems that use a model to determine what actions to take toward a goal, often by interacting with tools and using the results of previous actions. Unlike simple model calls, agent systems can dynamically control the flow of an application, using the results of previous actions to determine what should happen next.
Key Components of an Agent
Example Agent Workflow
Consider an agent that writes a blog post:
Goal: Publish technical blog
↓
Read available sources
↓
Model determines next action
↓
┌──────┴───────┐
│ │
Need more info Enough context
│ │
Read more Generate draft
│ │
└──────→ Evaluate
↓
┌─────┴─────┐
│ │
Revise Good
│ │
└──→ Evaluate
↓
UploadUnlike a fixed workflow, the agent can determine whether it needs more information, whether the draft needs revision, or whether it is ready to be uploaded.
Each step may involve calling a tool, such as read_markdown() or upload_blog(). The model can determine which tool should be called next based on the available tools and the current state. The application then validates and executes the requested tool and provides the result back to the model.
Agent vs. Fixed Workflow
Fixed workflows follow a predefined sequence of steps, which can make them easier to test, debug, and control. Agents introduce more dynamic behavior by allowing the model to determine which action to take next based on the current state and available tools. This flexibility can make agents useful for less predictable tasks, but it also introduces additional complexity and failure modes.
Combining RAG and Agents
RAG and agents solve different problems but can work together to build powerful AI applications.
RAG provides relevant information; agents determine and execute actions.
Example: Education Assistant with RAG and Agents
An education assistant might work like this:
Student question
↓
Agent
↓
Retrieve learning material (RAG)
↓
Generate explanation
↓
Create practice questions
↓
Evaluate answers
↓
Determine next activityIn this example, the agent uses RAG to find relevant learning material and then uses that information to generate explanations and practice questions. The agent controls the flow of the interaction and can use information about the student’s current task or previous results to determine the next activity.
Challenges in RAG and Agents
Both RAG and agents can fail in ways that simple model calls cannot.
To reduce these risks, agent systems should set clear boundaries, restrict available tools, validate tool inputs and outputs, and define limits on execution.
Retrieval Quality
A RAG system is only as useful as the information it retrieves.
Two important concepts are recall and precision:
Recall: How much of the relevant information was successfully retrieved.
Precision: How much of the retrieved information is actually relevant.
A system might retrieve many documents and achieve high recall, but if many of them are irrelevant, the model may receive noisy context and have more difficulty identifying the useful information.
A common approach is to retrieve a larger set of candidates and then use a reranker to identify the most relevant documents before sending them to the model.
Query
↓
Retrieve 20 documents
↓
Rerank
↓
Select best 5
↓
ModelThis retrieval-and-reranking process can improve the quality of the context provided to the model.
Conclusion
RAG and agents are two powerful patterns for building advanced AI applications. RAG enhances a model’s ability to perform tasks using relevant external knowledge, while agents enable dynamic decision-making and control flow in applications.
By combining these two patterns, we can build AI systems that have access to relevant external information while also being capable of performing multi-step tasks and interacting with external tools. Understanding RAG and agents is valuable for AI engineers building real-world applications that need external knowledge, multi-step execution, or interaction with external systems.
