esc
  • WorkflowsPages
  • Case studiesPages
  • Machine Learning FoundationsLearn
  • AI EngineeringLearn
  • AI Guardrails & Red TeamingLearn
  • UI/UXLearn
  • Design SystemsLearn
  • Production System DesignLearn
  • AI papersPages
  • Interview KitPages
  • Book a callContact
Let’s talk

I’ll build the agent for it.

Take-Home: A Document Agent With Retrieval and Tools

How to build a document agent for a take-home test, with retrieval, tools, checks and the trade-offs reviewers look for, explained simply for your AI interview.

In short

A take-home is a small build you do on your own time. For a document agent, build retrieval first, give the agent only the tools it needs, and test it on real questions before you trust it.

12 min readIntermediateCommon · estimateFacts checked 27 November 2026

Say it out loud

Read it once, then say it without looking. Start with the short one; use the long one when they ask you to go deeper.

A take-home is a small project you finish at home. For this one I would build a document agent. First, retrieval: split the files into chunks, search them by keyword and by meaning, then merge the results. Second, tools: the agent can read, and anything that changes a record waits for a person to approve it. Last, I would test it on real questions with known answers, and say plainly what it cannot read yet.

What is a take-home, in plain words?

A take-home is a small project a company sends you to finish on your own time. The team reads your choices as closely as your code, so a small, clear build with honest limits scores well.

This one is a document agent. It answers questions about a pile of files, and it can take a few actions, like opening a file or drafting a task for a person. Picture a new hire at a law firm, given a shared drive of 40,000 vendor contracts and asked which ones renew without notice. A good new hire searches, opens the right contract and quotes the clause they found. The AI agent should work the same way.

So the agent has three parts. Retrieval finds the passages. Tools let it ask for actions. The answer has to point back to its sources.

Key words

Retrieval
Searching your own documents first, so the model answers from them.
Token
A small piece of text, roughly a word or part of one.
Chunk
A piece of a document that gets its own search index entry.
Tool
A function the model can ask for, such as a search. Your code runs it.
Citation
The file and clause an answer points to, so a reviewer can check it.

How does the agent find the right passage?

Retrieval means searching your documents before the model writes a word, so the answer comes from the files themselves. The usual build cuts each document into chunks. Anthropic's write-up on retrieval says chunks are "usually no more than a few hundred tokens", which keeps each one small enough to search and to fit in the prompt.

From question to cited answer

Analyst's question

asked in plain words

Find passages

Keyword search

exact words, clause numbers

Meaning search

same idea, other words

Merge the lists

rank fusion: best of both

Model

reads only the merged passages

Answer with sources

each claim names its file

  1. 1The question goes to the keyword search.
  2. 2The same question goes to the meaning search.
  3. 3Keyword hits go into the merge.
  4. 4Meaning hits go into the merge too.
  5. 5The merged top passages go to the model.
  6. 6The model answers, and each claim names its file.
A question goes to two searches at once. Their two lists are merged, and the model answers only from the merged top passages, naming the file for each claim.

Keyword search, usually a scoring method called BM25 that rewards exact word matches, is strong at exact strings like a clause number, an error code or a date. Meaning search, built on embeddings (lists of numbers that capture meaning), finds the same idea written in other words, such as a clause that rolls over instead of renewing. Neither wins alone, so the usual build merges both lists with rank fusion. Each passage gets a score from where it ranks in each list, and the best total wins.

Anthropic describes Contextual Retrieval, a method that adds a short note to each chunk before it is indexed. The note says what the chunk is about and which document it came from. Plain chunks lose that context, so a search can miss the right passage. Anthropic's own figures are on the card below.

  • “This method can reduce the number of failed retrievals by 49% and, when combined with reranking, by 67%”

    Anthropic, on Contextual Retrieval

  1. 1

    Chunk the contracts

    Split each contract by its clauses, so a clause is never cut in half by a fixed character count.

  2. 2

    Index every chunk twice

    Store it for exact-word search and as an embedding for meaning search. Exact clause numbers are matched most reliably by the first index, so keep it.

  3. 3

    Merge and keep the top few

    Fuse the two lists and send only the best few passages to the model, not the whole folder.

  4. 4

    Answer with the source

    The model names the contract and the clause number. Your code rejects any answer that has no source.

Here is the merge step in plain Python. No search engine runs. The two lists are typed in, so the code shows only the fusion.

Python
# Merge a keyword search and a meaning search with rank fusion.
# No real search engine here: both lists are typed in, as if two searches had returned them.
keyword_hits = ["MSA-14", "SOW-22", "NDA-03"]  # exact words: "renew", "notice"
meaning_hits = ["SOW-22", "AMD-07", "MSA-14"]  # same idea, other words: "rolls over"
K = 60  # a damping constant: how fast lower ranks lose weight

def fuse(*ranked_lists):
    scores = {}
    for hits in ranked_lists:
        for rank, doc in enumerate(hits, start=1):
            scores[doc] = scores.get(doc, 0) + 1 / (K + rank)
    return sorted(scores, key=lambda d: -scores[d])

for place, doc in enumerate(fuse(keyword_hits, meaning_hits)[:3], start=1):
    print(place, doc)

Output

1 SOW-22
2 MSA-14
3 AMD-07

Which tools should the agent get, and who runs them?

A tool is a function the model may ask for. In this build the tools are search_documents, open_contract and draft_renewal_notice. The model never runs them itself. It writes a request, and your code decides whether to run it.

  • “Client tools (including user-defined tools and tools with Anthropic-defined schemas, such as bash and text_editor) run in your application”

    Anthropic, Claude tool use guide

The agent loop with a tool

Analyst asks

one question at a time

Model

picks the next step

Search documents

your code runs it, returns passages

Answer with sources

stops when sources back it

  1. 1The analyst's question goes to the model.
  2. 2The model asks your code to run a search.
  3. 3Your code runs it and sends the result back.
  4. 4With enough sources, the model writes the answer.
The loop: the model asks for a search, your code runs it, and the result goes back. The model stops once it has enough sources to answer, and the answer names them.
  • Read tools

    Search and open

    Cannot change anything, so safe to loop. Their text still reaches the model, so treat results as untrusted, and log every call.

  • Act tools

    Draft a notice

    Changes something outside the app. It only writes a draft, and waits for a person to approve the exact text.

Tool design matters as much as the prompt. Anthropic's agent guide says: "A good tool definition often includes example usage, edge cases, input format requirements, and clear boundaries from other tools."

Set hard limits in code as well: a maximum number of steps, a timeout for each search, and a cap on how many files one question can open. A loop with no limit can run up a large bill and never stop.

How would you stop a planted line in a contract from triggering a tool?

A vendor could hide a line in its PDF, an order that tells the agent to draft a notice to a new address. The model reads it as text, so it cannot reliably tell an order from data. OWASP's 2026 guidance says there is "no reliable prevention mechanism exists today".

So the design limits what a fooled model can do. Search and open tools return text and cannot send anything. Any action that leaves the system goes through a check in your code, and then a person approves the exact action. OWASP asks for this: "Require explicit human confirmation before any privileged, irreversible, or externally visible action".

How do you know it works, and when is a simpler build enough?

Build a small test set before the agent: 30 to 50 real questions, each with the right answer and the right contract. Test the search and the answer separately. If the right clause never reaches the model, no prompt will fix it.

  • Log every tool call

    Keep each call and its result, so a bad answer can be traced.

  • Re-run the same test set

    Run it after every change, so a fix in one place cannot break another.

  • Show the clause used

    A reviewer checks the answer against the clause in one click.

  • Track the failures

    Write down which questions fail, and why, before you change anything.

Anthropic's guide says to start simple, and to add agent steps only when simpler solutions fall short.

Tip

In the write-up, say which agent steps the task really needed, and which a single search and answer could have done. Reviewers notice a build that knows its own limits.

What does the interviewer want to hear?

A strong answer covers

  • A plain definition: the agent searches your files, uses tools, and cites its sources.
  • Retrieval first: keyword and meaning search, merged, over chunks.
  • Tools split into read and act, with a person approving any act.
  • Honest limits: what it cannot read yet, and what it costs to run.
  • A test set of real questions, scored in two parts.
  • The simplest build first, with a reason for each agent step.

Common traps

  • Trap"The model checks its own answers, so the output is safe."

    BetterA second model call can make the same mistake. Check the answer against the source file and the rules in code.

  • Trap"Vector search is enough, so skip the keyword index."

    BetterMeaning search is weak at exact clause numbers and codes. Keep both indexes and merge them.

  • Trap"Add more tools and the agent gets better."

    BetterEach extra tool is one more thing to get wrong. Add one only when a test question needs it.

Can you spot the risky design?

Spot the risky tool

A teammate's version of this agent has four tools. Which one is the risk?

Quick check

Keyword search missed a clause that says 'rolls over' when the analyst typed 'renew'. What is the most direct fix?

Can you answer these from memory?

Flash cards · tap a card to flip it

1 / 6

What will they ask next?

  1. Q133What are common RAG failure modes?11 min
  2. Q135How do you add citations?11 min
  3. Q140How do you enforce access control in RAG?11 min
  4. Q177What makes a good tool set?13 min
  5. Q172When and how do you add a human in the loop?12 min
  6. Q181How do you evaluate agents?11 min

Learn it in depth

Last updated on 27 November 2026

Building an agent that answers from your own documents?

I design and build AI agents, including the retrieval and the tools behind them, with checks and human approval for anything risky. Book a short call and tell me what your documents and actions are.