Coursify

Retrieval-Augmented Generation (RAG) — From Fundamentals to Production-Ready Agentic RAG Systems

Creating Retriever Tools with LangGraph

Create retriever tools using create_retriever_tool for agent use. Learn to wrap vector store retrievers as LangGraph tools that agents can call dynamically.

Learning Goals

  • Create retriever tools with create_retriever_tool
  • Wrap vector store retrievers as agent-callable tools

Creating Retriever Tools

In an agentic system, the LLM does not just "receive" context; it chooses to "retrieve" context when it needs it. To enable this choice, we must wrap our vector store retrievers into a standardized Tool format. In the LangChain ecosystem, a Tool is an object that has a name, a description, and a function to run. The agent uses the Description to understand when and how to use the tool.

In this lesson, we will learn how to use the create_retriever_tool utility to bridge the gap between static retrieval and agentic tool usage.

Learning Goals

  • Convert a LangChain retriever into a structured Agent Tool.
  • Craft high-fidelity tool descriptions to guide agent reasoning.
  • Manage multiple specialized retrieval tools in a single agent.

Core Concepts

1. The Tool Wrapper

A raw retriever is just a function. A Retriever Tool adds metadata that the LLM can "read" during its planning phase.

  • Name: A unique identifier (e.g., search_hr_manual).
  • Description: A detailed explanation of what the tool contains and when it should be called.

2. The Power of the Description

The description is the most critical part of an agentic tool. If the description is vague (e.g., "Search documents"), the agent might call it for the wrong reasons.

  • Bad Description: "Search the database."
  • Good Description: "Search the 2024 Employee Benefits Handbook. Use this for questions about insurance, time off, and retirement plans."

3. Specialization vs. Generalization

You can create one general search tool or multiple specialized tools (e.g., search_legal, search_technical, search_finance). Multiple specialized tools allow the agent to perform Targeted Retrieval, reducing noise and improving accuracy.

Tool Architecture

Building a Retriever Tool

  1. 1
    Step 1

    Start with any standard LangChain retriever (Chroma, Pinecone, etc.):

    1retriever = vector_store.as_retriever()
  2. 2
    Step 2

    Use the specialized constructor from langchain.tools.retriever:

    1from langchain.tools.retriever import create_retriever_tool
  3. 3
    Step 3

    Wrap the retriever with its metadata:

    1retriever_tool = create_retriever_tool( 2 retriever, 3 "search_state_of_union", 4 "Searches and returns excerpts from the 2023 State of the Union address.", 5)
  4. 4
    Step 4

    Check how the agent will 'see' the tool:

    1print(retriever_tool.name) 2print(retriever_tool.description)

Example: Multi-Tool Agent

Imagine a medical assistant agent. You can provide it with two separate tools:

  1. search_medical_codes: "Search the ICD-10 database for specific diagnostic codes."
  2. search_patient_history: "Search the private patient records for previous visits and medications."

When the user asks "What is the code for the patient's heart condition?", the agent will first call search_patient_history to find the condition name, and then call search_medical_codes to find the corresponding code. This Dynamic Tool Chaining is only possible when retrievers are wrapped as tools.

Common Mistakes

  • Descriptions that are too long: LLMs have finite attention. Keep descriptions concise but informative (1-3 sentences).
  • Missing usage instructions: If a tool requires specific input formatting (e.g., "Keywords only"), state that clearly in the description.

Recap

  • Tools are the primary way agents interact with the world and your data.
  • create_retriever_tool is the standard LangChain utility for wrapping retrievers.
  • High-fidelity descriptions are the "programming language" for agentic behavior.

Knowledge Check

Question 1 of 3
Q1Single choice

Why does an agent need a 'Description' for a retriever tool?