The applying we construct is a RAG app.
The recipe is straightforward. Chunking, embedding, retrieval, and response.
Appears to be like good on paper. However issues rapidly get complicated when utilized in actual instances. Similarity searches might discover comparable wording, however not essentially in helpful chunks; searched contexts might rank too low to point out related proof, or essential context could also be break up throughout chunk boundaries.
With out ample context, LLMs have little room for restoration.
Let’s do a search repetitiveWhat if the mannequin might search, learn, determine if there’s sufficient proof, and search once more if wanted? Maybe we do not even want vector embeddings within the first place.
That is the premise agent lag.
On this put up, we’ll construct a mini-agent RAG workflow utilizing the OpenAI Brokers SDK. Discover how brokers repeatedly search, learn, and floor their solutions.
Lastly, let’s take a step again and briefly talk about concerns for constructing a sensible agent RAG resolution.
1. Case Research: Utilizing Agentic RAG to Reply Coverage Questions
On this case examine, we’ll construct a coverage RAG agent on high of our firm’s coverage doc assortment.
1.1 Curation of doc collections
We have created six complete company coverage paperwork. These are all markdown information. Every coverage features a title, efficient date, brief abstract, and coverage textual content.
Realistically, these paperwork cowl six frequent company coverage areas.
approval_matrix.mdcontains approval ranges for common enterprise journey choices and is efficient July 1, 2025.conference_guidelines.mdwhich accommodates guidelines for collaborating in exterior occasions and can take impact from Could 15, 2025.faq.mdaccommodates unofficial solutions to frequent journey questions and is efficient September 1, 2025.policy_updates_2026.mdefficient January 1, 2026, contains the newest data on lodging, convention journey, and approval timing for 2026.remote_work_policy.mdwhich incorporates guidelines concerning distant work and can take impact from February 1, 2026.travel_policy.mdwhich incorporates customary journey reserving guidelines for flights, lodging, meals and transportation, efficient March 1, 2025.
We intentionally set out that the solutions to coverage questions might not exist inside a single doc. This lets you confirm desired agent conduct.
You’ll find full synthesis documentation and agent RAG implementation notebooks. here.
1.2 Agent definition
Subsequent, configure the agent. To take action, we’ll use the OpenAI Brokers SDK.
Broadly talking, brokers are:
# pip set up openai-agents
from brokers import Agent
agent = Agent(
identify="Coverage analysis assistant",
directions=INSTRUCTIONS,
mannequin="gpt-5.4",
instruments=[list_docs, search_docs, read_doc],
)
There are two components it’s essential undergo: the agent’s directions and the instruments the agent has entry to.
To start with, there’s steering. Now outline the specified search conduct.
# Be aware: This instruction is iterated with AI
INSTRUCTIONS = """
[Role]
You're a cautious inner coverage analysis assistant.
[Research behavior]
Reply worker coverage questions utilizing the doc instruments.
Discover sufficient related proof to assist the reply.
Hold conclusions grounded within the coverage paperwork.
[Expected output]
Give a direct reply first.
Then briefly clarify the proof.
Cite the doc filenames used for every essential declare.
""".strip()
On this case examine, brokers should be capable to entry paperwork solely by three predefined instruments.
The primary is a software that provides brokers a fast overview of what paperwork exist.
@function_tool
def list_docs() -> checklist[dict]:
"""Listing accessible coverage paperwork with out returning their physique textual content."""
return [
{
"doc_name": doc["doc_name"],
"title": doc["title"],
"efficient": doc["effective"],
"abstract": doc["summary"],
}
for doc in docs.values()
]
The second software is a key phrase search software. I will preserve it easy right here. Every doc is split into chunks of paragraphs, and every question is matched in opposition to these chunks by token overlap.
@function_tool
def search_docs(question: str) -> checklist[dict]:
"""Search coverage paperwork and return the highest three brief snippets."""
query_tokens = tokenize(question)
scored = []
for chunk in chunks:
rating = len(query_tokens & chunk["tokens"])
if rating:
scored.append((rating, chunk))
scored.kind(key=lambda merchandise: merchandise[0], reverse=True)
outcomes = []
for rating, chunk in scored[:3]:
snippet = chunk["text"].change("n", " ")
if len(snippet) > 420:
snippet = snippet[:417].rstrip() + "..."
outcomes.append({
"doc_name": chunk["doc_name"],
"title": chunk["title"],
"part": chunk["section"],
"snippet": snippet,
"rating": spherical(rating, 2),
})
return outcomes
The final software is one that enables the agent to open a single doc by file identify.
@function_tool
def read_doc(doc_name: str) -> str:
"""Learn one coverage doc by filename."""
if doc_name not in docs:
legitimate = ", ".be part of(sorted(docs))
return f"Unknown doc: {doc_name}. Legitimate paperwork: {legitimate}"
return docs[doc_name]["text"]
It is a full RAG agent.
1.3 Operating one coverage query
Subsequent, check your agent with one particular query.
“I am attending a convention in Berlin. The convention organizers have listed an official resort, however the nightly charge exceeds the conventional resort restrict. Can I e book that resort? What approvals do I want earlier than reserving?”“
Run the agent utilizing:
from brokers import Runner
end result = await Runner.run(agent, PROMPT, max_turns=12)
The agent got here up with the right reply. Sure, staff can e book official convention accommodations if there are sensible enterprise causes. The data was obtained by conference_guidelines.md.
For the approval half, the agent first recognized that the resort was above regular limits and wanted approval. Then, the corresponding approval circumstances got. Agent used travel_policy.md, approval_matrix.mdand policy_updates_2026.md That confirms the reply, and it is precisely what we’d count on.
Much more fascinating are the traces, from which we will learn the way the agent is pondering. You’ll be able to view traces within the following methods:
for merchandise in end result.new_items:
print(kind(merchandise).__name__, merchandise)
end result.new_items Comprises intermediate software calls and power outputs generated by the agent. In my run, I can see that the agent makes the primary name. search_docs() Key phrases embrace convention resort, resort cap, approval, and Berlin. Then it known as list_docs() Examine accessible coverage paperwork. I then opened the related file as follows: read_doc(). Solely then did I get a closing reply.
That is precisely the agent loop we wished to see.
3. Issues to determine earlier than constructing an Agentic RAG
The case research we reviewed solely scratch the floor. Primarily based on my expertise, I like to recommend answering the next 5 questions to truly construct a working agent RAG resolution:
Q1: How a lot freedom should brokers have?
One frequent possibility is strictly what we did within the earlier case examine. This implies we publish just a few fastidiously curated instruments that brokers can use solely to conduct their investigations. That is straightforward to manage, check, and audit.
Nonetheless, you too can give the agent broader entry, equivalent to a shell or file system. On this manner, the agent can run scripts immediately to look and examine information, and presumably carry out additional knowledge processing to generate helpful artifacts, all by itself.
This sample could also be extra highly effective, but it surely additionally will increase danger and makes conduct much less predictable.
Due to this fact, most RAG purposes begin with fastidiously chosen instruments and solely add shell/file system entry when the complexity of the duty justifies it.
Q2: Ought to brokers solely search uncooked textual content?
Most RAG initiatives seemingly begin with plain textual content, equivalent to a PDF, Wiki web page, or handbook, and that is okay.
However in actuality, in lots of instances Information layer on high of the uncooked textual content.
These derived information artifacts might be doc metadata, summaries, and hyperlinks between paperwork. Or you may go additional and implement a correct information graph.
These derived information artifacts assist brokers navigate the corpus, whereas the uncooked textual content stays the supply of reality.
Q3: Is embedding nonetheless essential?
Agentic RAG doesn’t essentially imply the tip of embedding.
Vector embeddings are an environment friendly strategy to discover semantically associated textual content and sometimes carry out higher than pure key phrase search methods.
With Agent RAG, what has basically modified is that retrieval is now an “motion” that brokers can carry out. Even below this framework, “actions” might be carried out by embedded-based search performance, keyword-based search performance, and even hybrid search performance.
Due to this fact, embedding remains to be helpful. These are only one attainable strategy to improve your agent search instruments.
This fall: Does one agent have to deal with all the pieces?
The best agent RAG setup is only one agent that searches, reads, and responds.
Nonetheless, as your duties develop into extra complicated, you might need to break up the work amongst a number of brokers. Extra particularly, multi-agent technique.
Work might be divided by function. for instance, planner, retriever, author In a break up, the planner decides what proof is required, the retriever collects it, and the author makes use of the collected proof to reach on the closing reply.
It’s also possible to break up it by supply kindevery agent has personalized instruments and focuses on a selected kind of supply.
Please notice that a number of agent setups are extra complicated to tune and there’s no assure that they’ll carry out higher than a single agent setup. Empirical testing is essential.
Q5: Ought to I all the time use Agent RAG?
Perhaps not all the time.
Simply because Agent RAG has develop into a trending matter does not imply it ought to all the time be utilized by default.
Agentic RAG will increase flexibility, but it surely comes at a value. That value is said not solely to latency and token prices, but in addition to unpredictable agent conduct.
At all times begin easy and add agent loops in case your query truly requires iterative retrieval.

