Build a production-grade RAG Agent: Part 2 A developer built a production-grade retrieval-augmented generation (RAG) agent in TypeScript using the Vercel AI SDK, combining a single model with three tools over a library of tax Acts and an agentic search-and-read loop. In evaluation, the agent scored 0.729 versus 0.582 for the first iteration, cutting confidently wrong answers from 5 to 0, while a version given perfect gold passages only tied the no-tools baseline (+0.006) and a single fixed search underperformed it (−0.080). Let's build the agent and its tools, in TypeScript with the Vercel AI SDK. The ideas carry over to any language or framework, such as Python with Pydantic AI. The repo https://github.com/yuvraj2112/rag-agent has two folders that run on their own and meet only over HTTP: harness/ , the measure from Part 1, and agent/ , which you build here. Its README covers the rest. This is Part 2 of a two-part series: TL;DR In Part 2, we build the agent: one model, three tools over a library of tax Acts, and a loop. By the end, we have an agent that beats the first iteration 0.582 → 0.729, with confidently wrong answers down from 5 to 0 , and a way to spot a real improvement. In my main build, the bigger project this tutorial comes from, I first tested which part of the job matters more: finding the law, or reasoning over it. Each version is compared with the floor, one model call with no tools. The repo doesn't include the first two. | Version | Against the floor | Questions won / lost / tied | |---|---|---| | Perfect passages: the gold's own sources handed to the model | +0.006: a tie | 27 / 28 / 3 | | One fixed search: the question searched once, the top passages handed to the model | −0.080 | 17 / 34 / 7 | | The agent, searching and reading for itself | +0.082 | 34 / 16 / 8 | Handing over the right sources didn't beat the model's own memory, and one search made it worse. Only the agent, searching and reading for itself, beat the floor. cd rag-agent/agent the repo you cloned in Part 1 npm i cp .env.example .env add your OPENAI API KEY Every setting lives in agent/.env , and .env.example lists each one with its default. The ones you'll touch: OPENAI API KEY= the only one you must fill in MODEL=gpt-6.1-sol one model for everything MODEL EFFORT=medium TOOLS=library none = the floor; library,web adds the web tools RUN ROUNDS=40 when a run must stop: rounds of tool calls, RUN SECONDS=480 seconds, RUN DOLLARS=3 or dollars EMBEDDINGS=off auto: search by meaning with the library's vectors The model's name is a setting. To change provider, install its package npm i @ai-sdk/anthropic , change chatModel in src/model.ts , and set the new model's prices in .env : js import { anthropic } from "@ai-sdk/anthropic"; export const chatModel = = anthropic "claude-sonnet-5-5" ; The repo ships the three Acts as files in agent/data/law/ , and a ready-made SQLite library built from them. Copy it in before you start the agent: npm run seed copies the ready-made library: 5,694 passages and their vectors npm run seed -- --fresh downloads the Acts again and embeds every passage: about a minute and $0.20 npm run fetch-law --