Concepts / Tool Use in Language Models

Tool Use in Language Models

The model has no direct access to the document, so any answer it gives about the policy is at best a guess.

  • Programming

A Question the Model Cannot Answer

Suppose you ask a language model a question about your company's internal leave policy. The model was trained on a large body of text, but that training did not include your company's private handbook, and it certainly did not include last month's revision. The model has no direct access to the document, so any answer it gives about the policy is at best a guess.

A natural fix is to paste the handbook into the prompt. That does not always work, because a model can only read a limited amount of text at once, and a long document may not fit. Two families of techniques address this kind of gap. Both work the same way at a high level: they change what the model sees at the moment it answers, rather than changing the model itself. This article follows that idea from its simplest form, retrieval-augmented generation, to the broader idea of an agent.

Tracing a Question Through a RAG System

Retrieval-augmented generation, usually called RAG, is defined as retrieving relevant information from external memory first, and then using that information to generate more accurate responses. The word order in that definition matters. Retrieval comes first, and generation depends on its output.

Step one is retrieval. The user's question is passed to a retriever, which searches an external memory. The external memory can be any store of documents that the system can search, which is why RAG lets a model draw on material that was never part of its training. The retriever returns the pieces of information most likely to be relevant to the question.

Step two is generation. The retrieved pieces are handed to the language model, and the model writes its answer from those pieces. The model is not asked to remember everything. It is asked to write well from the material it has been given.

User questionRetrieverExternal memorysearchable documentsRetrieved passagesLanguage modelGenerated answerquerysearchedselectsadded to promptwrites
What happens to the user question first, where the retrieved information comes from, and how it reaches the language model before the answer is produced?
Worked example

Following the Leave Policy Question

A worker asks: how many days of leave carry over after the latest revision of the handbook?

  1. 1
    Retrieval begins

    The question goes to the retriever, which searches the external memory holding the company's documents.

  2. 2
    Relevant pieces are chosen

    The retriever returns the passages most likely to be relevant, such as the section of the current handbook that discusses carry-over.

  3. 3
    Pieces reach the model

    The retrieved passages are placed in the prompt, so the model can read them at the moment it answers.

  4. 4
    The answer is written

    The model writes its answer from the passages. Its training did not contain the handbook, yet the answer can still draw on it.

Result

The improvement comes entirely from what the model was shown before it wrote, not from any change to the model.

Why Retriever Quality Sets the Ceiling

Because generation depends on retrieval, the retriever decides which information the model ever sees. If the retriever returns the wrong passages, the model is writing from the wrong material. If it returns the right passages, the model has what it needs to answer well. This is why the source states that retriever quality determines how well a RAG system performs.

The source identifies two broad kinds of retriever, term-based and embedding-based, and notes that they trade lightness against potential effectiveness. Choosing between them is a design decision about this trade-off. The important point for a learner is that the retriever is not a minor detail. It is the gate through which all of the model's evidence must pass.

QuestionIrrelevant passageVague answerQuestionRelevant passageGrounded answerweak retrievermodel writesgood retrievermodel writes
How does a poor retriever versus a good retriever change which documents reach the model, and therefore the final answer?

Anatomy of an Agent

RAG is one pattern. An agent is a broader pattern, and the source defines it by two things: its environment and the tools it can access. In an AI-powered agent, the AI acts as the planner. The planner analyzes the task it has been given, considers different solutions, and picks the most promising one.

AgentEnvironmentRetrieverone tool among othersToolsOther toolsAI plannerAnalyzes taskPicks best solutionhashashasincludesincludesdoesdoes
What contains what, and how do the environment, the set of tools, and the planner relate to one another?

Read the diagram as a containment picture. The agent is defined by its environment and its tools, and the planner is the part that decides what to do with them. The retriever from the RAG example appears here as one tool among others. Nothing in the agent's structure requires the retriever to be the only tool, or even the most important one.

Following the Agent Control Loop

Some tasks cannot be solved in one move. The source notes that a complex task can require many steps, and solving it therefore requires a powerful model to plan. Solving such a task means the planner must repeatedly pick a tool, let that tool act on the environment, and then decide what to do next, while keeping track of where the task stands.

Task receivedPlanner picks a toolTool acts onenvironmentPlanner reviewsprogressTask complete?Final resultstartcallsresult returnscheckyesno, next step
How does the AI planner pick a tool, act on the environment, check where the task stands, and decide the next step?

Walk the cycle with a simple made-up task: an agent is asked to prepare a summary of several documents. The planner first picks the retriever tool to locate the documents. The retriever acts on the environment by searching the store. The planner then reviews progress and sees that only some documents are gathered, so the task is not complete. It picks the next step and continues until the check says the task is complete. The loop repeats until the planner decides it is finished.

The source also explains why the planner must keep track of its position. A model's ability to plan can be strengthened with reflection, and with a memory system that helps it keep track of its progress. Both support the same goal: the agent must remember where it is in a multi-step task and what it has already tried.

Memory matters for a second reason too. Both RAG and agents work with a lot of information, and that information often exceeds the maximum context length of the underlying model. A memory system is what lets the model manage and use all of the information it has, including material that cannot fit into a single prompt at once.

RAG as a Special Case of an Agent

RAG and agents are closely related. The source describes the RAG pattern as a special case of an agent, one in which the retriever is the tool. Both patterns are prompt-based. Seen this way, RAG is the agent idea narrowed to a single kind of tool, used for a single purpose, which is searching external memory.

One tool: retrieverSet of toolsSearches externalmemoryEnvironmentFixed retrieve thengeneratePlanner chooses amongsolutions
How does a RAG system map onto the agent parts, and what is it missing compared with a full agent?

The comparison shows what RAG gives up. A full agent has a planner that analyzes the task and chooses among solutions, and it can draw on many tools. A RAG system has one tool, the retriever, and follows the fixed two-step pattern of retrieving and then generating. The narrower design is easier to understand and control, but it does not have the flexibility of an agent that picks its own path.

Mistakes About Retrieval and Agents

  • Common mistake

    Believing that a larger or better-written model makes a RAG system accurate on its own

    What it looks like

    A strong model gives a confident answer about the leave policy, but the retriever returned an old section of the handbook.

    Why it fails

    Generation depends on retrieval. The model writes from the passages it receives, so wrong evidence produces a wrong answer.

    Do this instead

    Evaluate the retriever as well as the model, because retriever quality determines how well the system performs.

  • Common mistake

    Thinking that a RAG system's memory is the same as the model's training data

    What it looks like

    Assuming that a new revision of the handbook is known to the model because it is stored in the document store.

    Why it fails

    The external memory is any store of documents the system can search. It is separate from what the model was trained on, and that separation is what allows material outside training to be used.

    Do this instead

    Treat the external memory as a searchable source that is consulted at answer time.

  • Common mistake

    Treating a planner as optional when a task has many steps

    Why it fails

    The source notes that complex tasks can require many steps and a powerful model to plan. Without tracking progress, the agent may lose its place or repeat attempts.

    Do this instead

    Give the agent a way to remember where it is in the task and what it has already tried.

  • Common mistake

    Assuming that an agent with a retriever is automatically a full agent. A RAG system is a special case, with a single tool and a fixed pattern.

More Tools, More Automation, More Risk

A natural instinct is to give an agent more tools. The source supports part of that instinct: the more tools a model is given, the more capabilities it has, which lets it solve more challenging tasks. Each tool widens what the agent can accomplish.

The same source warns about the other side of the trade. The more automated an agent becomes, the more catastrophic its failures can be. Each added tool and each step taken without a person checking it widens the range of things that can go wrong. Extra capability and extra risk arrive together, because both come from the same freedom to act without stopping.

Predicting the Effect of a Better Retriever

What do you think happens?

The leave policy question is still asked, and the model is unchanged. Does an answer improve if only the retriever is changed to return the correct handbook section?

  • Yes, because the model now sees the right evidence

  • No, because the model must be retrained first

Reveal answer
Answer:

Yes, because the model now sees the right evidence

The source's worked example says the improvement comes entirely from what the model is shown before it writes. Changing the retriever changes the passages the model receives, without changing the model itself.

MEDIUM

A company builds an agent with a retriever, a calendar tool, and an email tool. A reviewer asks why the agent is more capable than the RAG system but also riskier. Explain the answer using the ideas of tools, automation, and checkpoints.

Hints
  • Count the tools and think about what each one allows the agent to do.
  • Think about what happens when a step is taken without a person checking it.

Summary of Tool Use and Agents

  1. 1
    RAG has two steps: retrieve relevant information from external memory first, then generate the answer from that information.
  2. 2
    The retriever is the gate through which evidence reaches the model, so its quality determines how well a RAG system performs.
  3. 3
    An agent is defined by its environment and tools, with an AI planner that analyzes tasks and picks solutions. RAG is a special case in which the retriever is the only tool.
  4. 4
    More tools and more automation raise capability, but they also widen the range of possible failures, especially where no person checks a step.

At a glance

  • RAG retrieves relevant information from external memory first, then uses it to generate more accurate responses.
  • Retriever quality determines how well a RAG system performs, because the model writes from whatever passages the retriever supplies.
  • An agent is defined by its environment and tools, with an AI planner that analyzes tasks and picks among solutions.
  • RAG is a special case of an agent in which the retriever is the tool, and more tools and more automation increase both capability and risk.

Related concepts

More concepts starting with T