Learn & Build Agents

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

    02 Building the agentLesson 6 of 814 minIntermediateHelps to have seen:Context and memory

    Search and RAG

    Base sizeunder 2 thousand tokens
    What goes in the contextthe whole policy
    How to check—
    • + whole policy
    • + message
    • + agent
    Return policywhole, 12 sections"Exchange the T-shirt""from 4802 for an M"Agentthe loop: model and tools12 sections
    LLMcodedataperson

    Step 1 of 9: The whole policy goes on every call

    You will learn
    • When to use RAG, and why the store's policy does not need it
    • What an embedding is and why close means similar
    • How the question finds the passages and goes into the prompt
    • Why similar is not right, and the filter that fixes it
    In the lab

    Answers with the rule in force in the test conversations that depend on the policy: whole context × passage search

    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 whole policy goes on every call

      The customer wants to exchange the T-shirt from order 4802 for an M. To answer, the agent needs the exchange rule.

      The simplest way is to paste the whole policy, all 12 sections, on every call. Is that wasteful? It depends on the size, and that is the question of this lesson.

      Before searching for passages, ask whether the whole document already fits.

    2. When RAG pays off, and when it does not

      The store's policy has fewer than 2 thousand tokens. It fits easily in the 1 million window, and with each call pays about US$ 0,0002 for it on Sonnet 5.5. Anthropic suggests putting in whole any base under 200 thousand tokens, about 500 pages.

      , searching only the passages that matter, pays off when the base does not fit or is expensive to repeat, when it changes all the time, or when each person may only see part of it.

      Small, stable base: whole context with caching. Large, live or with per-person access: RAG.

    3. The store grew: cutting the base into chunks

      Now imagine the store with 3 thousand products, each with a manual and FAQs. That does not fit: time for RAG. It starts before any conversation, by cutting the base into .

      A chunk that only says "hold the button for 3 seconds" does not say which product, or which model. That is why each chunk carries where it came from. Anthropic showed that this extra context reduces the searches that miss.

      Each chunk needs to carry where it came from: product, model and topic.

    4. Each chunk becomes a point: the embedding

      An model reads each chunk and returns a vector: a list of hundreds of numbers that sums up the meaning of the text. It is not the model that chats; it is another one, just for this.

      The vectors are stored in an index. Chunks that talk about similar things land close together: pairing the headphones, pairing the speaker. The drawing flattens hundreds of dimensions into two.

      An embedding turns meaning into position: similar lands close.

    5. The question lands in the same space

      In the conversation, the question goes through the same embedding model and becomes a point in the same space. The search takes the k points closest to it, here the top 3.

      Look at the neighbors: the manual for the 2025 headphones, an old model, and the speaker pairing. They talk about the same thing, so they land close.

      RAG searches by closeness of meaning, not by exact word.

    6. The passages go into the prompt, and two are from the wrong product

      The agent pastes the 3 passages into the prompt, together with the question, and the model answers from them. That is the "generation" in RAG.

      But only one passage is from the headphones the customer bought. The model mixes them and answers with the step by step for the 2025 model. The customer tries it and it does not work.

      Similar is not right. The search does not know which product the customer has.

    7. Product and version are data, and the filter is code

      Each chunk keeps the product and model it came from. Before measuring distance, a filter in code keeps only what belongs to the customer's order.

      The question lands in the same place, but the wrong neighbors no longer count. And the answer cites the manual used, so it can be checked.

      What you can know for sure (product, version, owner) is filtered in code, not left to the search.

    8. Making the search miss less

      In Anthropic's tests, adding context to each chunk cut search failures by 35%. Adding word search (BM25) and reordering the results, the drop reached 67%.

      Our reading: the metadata filter comes before all of that. If you know the product, the version or the owner of the document, do not leave it for the search to guess.

      Filter on known data first; then, better search.

    9. Whole when it fits, search when it does not

      In the example store, the policy goes in whole, cached. When the base grows, it is cut into chunks with the context of where they came from, and the search brings only the question's neighbors, filtered by product and version.

      How to check in your case: run the test conversations that depend on the documents with whole context and with search, and compare accuracy and cost.

      RAG is a solution for a large base, not a default.

    Questions in this lesson

    The search brought back the manual for the old headphones model. What stops that?
    Product and model in each chunk, and a filter in code. The search only knows what is similar. What you can know for sure is filtered in code.
    Your knowledge base has 20 pages. Where do you start?
    Put everything in the context with caching and measure. A small base fits whole. RAG comes in when the eval shows it is needed.
    Why does the question "the headphones won't connect" find the pairing manual, without the word "pair"?
    Vector search compares meaning. Texts with similar meaning become vectors close to each other.