Session 7 — Homework

Build Your Own Knowledge Agent

From loading documents to handling failures — your complete guide to the RAG homework. Follow each task in order and you’ll have a working knowledge agent by the end.

4 tasks • Estimated time: 3–4 hours • Due before Session 8
1
2
3
4
1
2
3
4

Create a RAG Agent for Your Domain

Choose a domain you care about, gather documents, and wire up a complete knowledge agent that retrieves information before answering questions.

Your build roadmap

01
Choose Your
Domain
02
Gather 5+
Documents
03
Process
& Chunk
04
Build Search
Tool
05
Wire Agent
Loop
06
Test 10
Questions

Pick a domain

Choose something you know well so you can evaluate accuracy. Here are some ideas:

🍽️
Restaurant

Menu items, allergens, wine pairings, reservation policies

📚
Student Handbook

Academic policies, registration deadlines, grading systems

🛍️
Product Catalog

Product specs, pricing, compatibility, warranty info

⚖️
Legal

Contract templates, compliance checklists, regulation summaries

🏥
Medical

Patient guidelines, medication info, procedure protocols

Complete starter code

This is the restaurant worked example. Replace the documents with your domain content.

starter-code.py
1import anthropic
2import json
3
4client = anthropic.Anthropic()
5
6# ====== STEP 1: Your Documents ======
7# Replace these with YOUR domain documents
8documents = [
9 {
10 "title": "Menu - Main Courses",
11 "content": """
12 Grilled Salmon - $24.99
13 Pan-seared with lemon butter sauce. Contains: fish, dairy.
14 Pairs well with: Sauvignon Blanc, Pinot Grigio.
15
16 Ribeye Steak - $34.99
17 12oz USDA Prime, chargrilled. Contains: none (GF option available).
18 Pairs well with: Cabernet Sauvignon, Malbec.
19
20 Mushroom Risotto - $18.99
21 Arborio rice with wild mushrooms and parmesan. Contains: dairy, gluten.
22 Pairs well with: Chardonnay, Pinot Noir.
23 """,
24 "source": "menu_main_courses.md",
25 "date": "2024-11-01"
26 },
27 {
28 "title": "Reservation & Dining Policies",
29 "content": """
30 Reservations: Required for parties of 6+. Walk-ins welcome for smaller groups.
31 Cancellation: 24-hour notice required or $25 per person fee.
32 Dress code: Smart casual. No athletic wear.
33 Private dining: Available for 12-40 guests. $500 minimum spend.
34 Happy hour: Mon-Fri 4-6 PM, 50% off select appetizers and house wine.
35 """,
36 "source": "policies.md",
37 "date": "2024-10-15"
38 }
39 # ADD 3+ MORE DOCUMENTS FOR YOUR DOMAIN
40]
41
42# ====== STEP 2: Search Tool ======
43def search_documents(query):
44 """Search documents for relevant information."""
45 results = []
46 query_words = query.lower().split()
47
48 for doc in documents:
49 content_lower = doc["content"].lower()
50 # Count matching words
51 score = sum(1 for word in query_words if word in content_lower)
52 if score > 0:
53 results.append({
54 "title": doc["title"],
55 "content": doc["content"][:500],
56 "source": doc["source"],
57 "relevance": score
58 })
59
60 results.sort(key=lambda x: x["relevance"], reverse=True)
61 return results[:3] # Top 3 results
62
63# ====== STEP 3: Agent Loop ======
64tools = [{
65 "name": "search_documents",
66 "description": "Search the knowledge base for relevant information. "
67 "Use this for ANY question about our domain.",
68 "input_schema": {
69 "type": "object",
70 "properties": {
71 "query": {
72 "type": "string",
73 "description": "The search query"
74 }
75 },
76 "required": ["query"]
77 }
78}]
79
80def run_agent(question):
81 print(f"\n{'='*50}")
82 print(f"Question: {question}")
83 print(f"{'='*50}")
84
85 messages = [{"role": "user", "content": question}]
86
87 while True:
88 response = client.messages.create(
89 model="claude-sonnet-4-20250514",
90 max_tokens=1024,
91 system="You are a helpful assistant. ALWAYS use the "
92 "search_documents tool before answering questions. "
93 "Base your answers ONLY on the search results. If the "
94 "search results don't contain relevant information, say "
95 "'I don't have information about that in our "
96 "knowledge base.'",
97 tools=tools,
98 messages=messages
99 )
100
101 # Check if done
102 if response.stop_reason == "end_turn":
103 for block in response.content:
104 if hasattr(block, 'text'):
105 print(f"\nAnswer: {block.text}")
106 return response
107
108 # Handle tool calls
109 if response.stop_reason == "tool_use":
110 tool_results = []
111 for block in response.content:
112 if block.type == "tool_use":
113 print(f"\nSearching: {block.input['query']}")
114 result = search_documents(block.input["query"])
115 print(f" Found {len(result)} results")
116 tool_results.append({
117 "type": "tool_result",
118 "tool_use_id": block.id,
119 "content": json.dumps(result)
120 })
121
122 messages.append({"role": "assistant", "content": response.content})
123 messages.append({"role": "user", "content": tool_results})
124
125# ====== STEP 4: Test It! ======
126test_questions = [
127 "What fish dishes do you have?",
128 "Do you have any gluten-free options?",
129 "What's your cancellation policy?",
130 "What wine goes with steak?",
131 "Do you have outdoor seating?", # Not in docs!
132]
133
134for q in test_questions:
135 run_agent(q)
4 color-coded sections. Documents (cyan) — replace with your domain. Search tool (yellow) — finds relevant passages. Tool definition (magenta) — tells Claude when to search. Agent loop (green) — the retrieval-generation cycle.

Deliverable checklist

💡 Pro tip: Quality over quantity

Your documents don’t need to be long. A paragraph per document is fine. What matters is that they cover different aspects of your domain — the agent should be able to tell them apart when searching.

1
2
3
4

Stress-Test with Bad Data

Deliberately feed your agent bad data and document what breaks. This is how you learn where RAG systems fail — and every production system has these failure modes.

Outdated Data

Add a document with old pricing or policies. Does the agent quote the outdated info?

Add a 2022 menu with different prices alongside your 2024 menu

Observe: Which document does the agent cite? Does it notice the date difference?

Contradicting Documents

Add two documents that say different things about the same topic.

One doc says “No refunds after 30 days”, another says “Full refund within 60 days”

Observe: Does the agent blend the answers? Flag the conflict? Pick one?

Knowledge Gaps

Ask questions your documents DON’T cover.

Ask about competitor products when you only have your own catalog

Observe: Does the agent hallucinate? Admit it doesn’t know? Make something up?

Document your findings

Use this format (a Markdown table in your notebook or a simple list):

Test What You Tried What Happened Why It Happened How to Fix
Outdated data Added 2022 menu Agent quoted old prices No date prioritization Add date metadata, sort by recency
Contradiction Conflicting refund policies Agent blended both No conflict detection Add contradiction check before answering
Knowledge gap Asked about competitors Agent hallucinated pricing No guardrails on search results Add confidence threshold, refuse if no relevant docs
⚠ The point is to find failures

If your agent handles everything perfectly, you’re not trying hard enough. Real-world RAG systems fail in exactly these ways — that’s why companies spend millions on guardrails.

1
2
3
4

Score Accuracy on 10 Questions

Measure how well your agent actually performs. This is the same evaluation framework real companies use — just on a smaller scale.

Scoring rubric

Criteria Score Description
Retrieved right doc? Yes / No Did the search tool return the correct document?
Answer correct? Yes / Partial / No Is the answer factually accurate based on the documents?
Hallucinated? No / Yes Did the agent add information not in the documents?
Cited sources? Yes / No Did the agent mention which document it used?

Example evaluation

Question Right Doc? Correct? Hallucinated? Cited? Notes
“What’s the salmon price?” Yes Yes No Yes Found in menu_main_courses.md
“Do you have vegan options?” No No Yes No Agent invented vegan dishes not in our menu
“What are your hours?” No No Yes No Not in docs — agent should have said “I don’t know”

Your Agent’s Score Card

After testing 10 questions, tally your results:

X/10 correct  •  Y/10 cited sources  •  Z/10 no hallucination

Grading guidance:

7+ correct = Good
5–6 = Needs work
<5 = Major issues to fix
✅ A low score is not a bad grade

Your grade is based on the quality of your evaluation, not on how well your agent performs. A score of 4/10 with a thoughtful analysis of why it failed is worth more than 10/10 with no explanation.

1
2
3
4

1-Page Proposal for a Real Company

Write a 1-page proposal for a knowledge agent at a real company. This is the kind of deliverable consultants charge $50K+ to produce.

Proposal Template
1.
Company & Use Case
What company? What problem does the knowledge agent solve?
2.
Documents Needed
What documents would you load? List 5–10 specific document types.
3.
Target Users
Who would use this agent? How often? What questions would they ask?
4.
Risk Assessment
What happens if the agent is wrong? What’s the worst case?
5.
Build vs Buy
Would you build custom RAG or use a platform (Glean, Notion AI)? Why?
6.
Success Metrics
How would you measure if the agent is working? What’s the ROI?

Example proposal

🏨 Marriott Hotels — Front Desk Knowledge Agent

Company:
Marriott Hotels
Use Case:
Customer service agent for front desk staff answering guest questions
Documents:
Room types & rates, hotel amenities, cancellation policies, local attractions guide, loyalty program rules, accessibility info
Users:
500+ front desk staff across 50 properties, answering guest questions 24/7
Risk:
Wrong room rate quoted → revenue loss. Wrong cancellation policy → legal exposure. Wrong accessibility info → ADA compliance issue.
Build vs Buy:
Buy (Glean or similar) — 50 properties means thousands of documents across multiple systems. Too complex for simple RAG.
Success:
Reduce average call handling time by 30%, improve first-call resolution from 72% to 90%, eliminate policy misquotes.
💡 Pick a company you know

The best proposals come from personal experience. If you worked at Starbucks, write about Starbucks. If you follow Tesla, write about Tesla. Domain knowledge makes your risk assessment and success metrics more realistic.

FAQ & Common Issues

Click any question to expand the answer.

📄 How many documents do I need?
Minimum 5, but more is better. Aim for documents that cover different aspects of your domain. Quality matters more than quantity — a few well-structured documents beat dozens of messy ones.
📎 Can I use PDFs?
For this homework, convert PDFs to text and paste into your documents list. In production, you’d use PDF parsing libraries like PyPDF2 or pdfplumber. The point of this assignment is the agent architecture, not document parsing.
🚨 What if my agent never says “I don’t know”?
Update your system prompt to be more explicit: "If the search results don't contain relevant information, you MUST say: I don't have information about that in our knowledge base." You can also add: "NEVER make up information that is not in the search results."
🔍 How do I improve search accuracy?
Three things to try: (1) Better chunking — split docs by topic so each chunk has a clear subject. (2) Better queries — extract keywords from the question before searching. (3) Better scoring — weight title matches higher than content matches, or check for phrase matches instead of individual words.
🤔 What’s a good domain to choose?
Pick something you know well, so you can evaluate accuracy. Restaurant menus, course catalogs, and product specs work great because the information is structured and you can easily check if the agent’s answers are correct. Avoid domains where you can’t verify the output.

What You Just Built

You built a complete knowledge agent — from document loading to failure handling to business proposal. This is exactly what companies pay $100K+ for consultants to build. The architecture is identical; only the scale is different.

RAG is the #1 enterprise AI pattern. Every company deploying AI today — from Slack to Salesforce to internal tools at Goldman Sachs — uses retrieval-augmented generation. You now understand how it works, why it breaks, and how to evaluate it.

The proposal matters. Being able to articulate what to build, who it’s for, and what could go wrong is the difference between an engineer and a leader. That’s the skill companies pay for.

5+
docs loaded
10
questions tested
3
failures found
1
proposal written