Answer from runbooks with retrieval
In this task you'll let the agent look up your own runbooks and quote where its advice came from.
A model only knows what it was trained on, not your team's runbooks. Retrieval (often called RAG, retrieval-augmented generation) fixes that: split your documents into chunks, find the chunks that best match the question, and give those to the model as a tool result. The answer can then follow your runbook and cite the file it came from.
Production systems find chunks with embeddings (numbers that capture meaning) in a vector index such as Azure AI Search. This task uses plain word overlap so you can see every step; the shape is the same. Retrieval quality decides answer quality: chunks that are too big bury the fix, too small lose the context.
Example. Kestwick Hotels' runbooks lived in a wiki nobody searched at 3am. A retrieval tool over the wiki let the on-call agent answer 'the pool is exhausted' with the right runbook section and a link, and new starters began trusting the runbooks again.
Before you start
You need: Python 3.10 or newer (on Windows, use py where the steps say python). No AI account: the practice model runs on your computer. The agents folder from the earlier tasks. Time: 25 minutes.
Where to look
- agent.py
- search_runbooks()
- practice_model.py
- RUNBOOKS
Steps
- Read RUNBOOKS in practice_model.py: three short Example runbooks, each split into paragraphs.
- Add
search_runbooks()and its tool from Commands. - Run
python agent.py "Search the runbooks: what do we do when the pool is exhausted?". The answer quotes the db-pool.md paragraph with the file name in brackets. - Ask about something no runbook covers (for example disk space) and see the weak match it returns.Real systems set a minimum score and say 'no runbook found' rather than quote the wrong one.
Commands
def search_runbooks(query, top=2):
q = set(re.findall(r"[a-z]+", query.lower()))
chunks = [(name, part) for name, text in RUNBOOKS.items() for part in text.split("\n\n")]
scored = sorted(chunks, key=lambda c: -len(q & set(re.findall(r"[a-z]+", c[1].lower()))))
return "\n".join(f"[{name}] {part}" for name, part in scored[:top])
# Add to TOOLS:
{"name": "search_runbooks", "description": "Search the team runbooks for the fix to a problem, such as a pool exhausted error.",
"input_schema": {"type": "object", "properties": {"query": {"type": "string"}}, "required": ["query"]}}
python agent.py "Search the runbooks: what do we do when the pool is exhausted?"
Watch out for
- Cost
- Free here. Embeddings and a search service cost money in production; Azure AI Search has a free tier for small trials.
- Security
- Retrieved documents are untrusted input too: a runbook edited by anyone can carry injected text. Only index sources with an owner, and respect who may read each document.
- Common mistakes
- Indexing everything, including drafts and out-of-date pages, and answers with no source so nobody can check them.
Clean up
Keep the agents folder for the next tasks in this section; delete it at the end.
Words to know
- Retrieval
- Finding the documents that match a question.
- RAG
- Retrieval-augmented generation: answering with retrieved text in front of the model.
- Chunk
- A piece of a document small enough to retrieve on its own.
- Embedding
- A list of numbers representing the meaning of text, used to find similar text.
Tick off each step and track your progress in the guide.
Open this task in DevOps, Hands On