Learn & Build Agents

    ↑ ↓ to choose · Enter to open · Esc to close

    02 Building the agentLesson 7 of 812 minIntermediateHelps to have seen:Search and RAG

    Search that gets it right

    E4 passage9th
    Right search (example)—
    How it searchesby meaning
    • + question with a code
    • + search by meaning
    • + results
    "Error E4 on headphones"customer questionAgentthe loop: model and toolsSearch by meaningembeddings: similarResultswhat the search returnssearch by meaning1 Headphones: won't connectsimilar2 Speaker: connectingsimilar3 Headphones: batterysimilar4 Headphones: pairingsimilar5 Headphones: cleaningsimilar9 Headphones: error E4outside top 5the right passage landed 9th (example)your code searches5 similaranswer without E4
    LLMcodedataperson

    Step 1 of 9: The customer typed the code, the search did not find it

    You will learn
    • Measure the search separately from the answer
    • Why to combine search by word and by meaning
    • What the reranker does in the second stage
    • When the agent itself should search
    In the lab

    Right passage in the top 5, per answer-key question: by meaning, hybrid and with reranker

    Coming soon: I will measure this on the example agent and publish the number with the report.

    The whole lesson as textEvery step, to read at once, search or print.
    1. The customer typed the code, the search did not find it

      A customer writes: "my headphones show error E4, what do I do?". The help base has a page just about E4.

      The search by meaning from the last lesson returns five passages about headphones: connection, battery, pairing. All similar. The E4 page landed 9th, outside what goes to the agent, and the answer comes out generic.

      Similar is not what the customer asked.

    2. Measure the search before the answer

      Before changing anything, gather test questions and write down, for each one, the passage from the base that should come back. That is the search's .

      The math is simple: in how many questions did the right passage come in the top 5? Here, 3 of 5. It does not depend on the model or the prompt, so it shows whether the problem is in the search.

      The search has its own score: did the right passage come back?

    3. Search by word finds the exact term

      Another search looks at words, not meaning. Algorithms like BM25 give a higher score to the passage that has the question's terms, and even more when the term is rare in the base, like a code.

      It finds "E4", "CX-20" and model names. But it does not understand synonyms: "connect to my phone" does not find the page that says "pair".

      Codes, model names and rare terms: search by word.

    4. Hybrid search: both lists together

      Hybrid search runs both and merges the lists. A common way is fusion by position: a passage ranked well in both lists moves up.

      For the E4 question, the right page moves up to 2nd. In 1st is the watch, which also has an E4 error: the term matches, the product does not.

      Each search misses what the other finds. Combine both.

    5. The reranker takes a second look

      The searches are fast because they compare the question with passages already processed. The reranker is a second stage: a model reads the question and each candidate together and gives a relevance score.

      It is more expensive per passage. That is why it runs only on the candidates the search brought, and the best ones go to the agent.

      First find many, fast. Then order a few, carefully.

    6. Wrong answer, wrong culprit

      If you only measure the final answer, you see two identical failures: "the agent got it wrong". One came from the search, which did not bring the passage. The other came from the model, which got the right passage and used it wrong.

      With the search score next to the answer score, each error points to its own fix.

      Two scores, search and answer, say where to fix.

    7. Sometimes, the agent itself searches

      Until now the search ran before the model. Another way is to give the search to the agent as a : it searches "E4", sees headphones and a watch came back, searches again with "headphones" and reads the whole page.

      It is more accurate and easier to see what it did, with more turns, time and cost. Anthropic suggests starting there and adding search by meaning if you need speed.

      Search as a tool: the agent retries until it finds it.

    8. Each layer took off a piece of the failure

      What the source says: in Anthropic's tests, search failure in the top 20 passages dropped from 5.7% to 3.7% with context in each chunk, 2.9% adding BM25 and 1.9% with reranking. Passing 20 passages was better than 10 or 5.

      Our reading: the layers add up, but each one costs. Measure on your answer key before adding the next.

      The layers add up: each one took off another piece of the failure.

    9. The right search before the right answer

      Search by meaning understands synonyms and misses exact terms; search by word does the opposite. Together they cover more, and the reranker puts the best on top. When accuracy is worth more than speed, the agent can search on its own.

      How to check in your case: build an answer key with the right passage for each question and measure the search separately from the answer.

      Measure the search, combine word and meaning, reorder the candidates.

    Questions in this lesson

    The agent got the answer wrong, and the right passage came 1st in the search. Where do you fix it?
    In the prompt or the model: the search got it right. With a separate search score, you can see the passage arrived. The error was in using it.
    Customers often type the product code, like "CX-20". What helps most?
    Adding search by word. Search by word gives weight to the exact, rare term. The model does not even see the passage if the search does not bring it.
    Why does the reranker run only on the candidates, and not on the whole base?
    Because it reads question and passage together, and that costs per passage. The search compares against passages already processed, fast. The reranker rereads each pair: better order, more cost.