Video summary

Tools in LangChain | Generative AI using LangChain | Video 16 | CampusX

Main summary

Key takeaways

Educational

Summary

The video introduces tools in LangChain as a foundation for building AI agents. It explains what tools are, how they complement language models, how to use built-in tools, and three ways to create custom tools. It also demonstrates how related tools can be grouped into a toolkit.

Why LLMs Need Tools

The presenter describes two core abilities of a large language model (LLM):

  • Reasoning: Interpreting a question and working out how to respond.
  • Language generation: Producing an answer in natural language.

On their own, LLMs generally cannot reliably retrieve live information, run code, call external APIs, send messages, or interact with databases. The video compares an LLM to a body that can think and speak but lacks the means to act.

A tool provides that ability to act. Technically, it is a function that contains the logic for a task and is packaged so an LLM can understand when and how to use it. The LLM can select a tool and provide its inputs; the tool performs the task and returns its result.

How Tools Relate to Agents

An AI agent combines:

  • An LLM’s reasoning and decision-making.
  • Tools’ ability to carry out actions.

The LLM helps decide what should happen; tools enable the system to do it. The presenter describes this combination as central to building agents with LangChain or LangGraph. Tool calling—the connection that lets an LLM use tools—is covered in a later video in the playlist.

Built-in Tools

LangChain includes prebuilt tools for common tasks, so developers can import and use them rather than implement their functionality from scratch. Examples mentioned include:

  • DuckDuckGo Search: Retrieves web results, including for questions that require current information.
  • Wikipedia query: Searches Wikipedia and returns information about a topic.
  • Python REPL: Runs Python code, which is useful for calculations an LLM might not perform reliably on its own.
  • Shell: Executes command-line instructions on the machine running the code.
  • HTTP request tools: Make web requests.
  • Gmail and Slack tools: Support messaging tasks.
  • SQL database tools: Run database queries.

The video demonstrates invoking DuckDuckGo Search and a shell tool in Google Colab. The shell example runs commands such as whoami and ls. The presenter cautions that shell access can be risky, particularly in production, because commands may affect or delete files.

LangChain’s documentation is recommended for browsing available tools and their setup instructions.

When to Create Custom Tools

Custom tools are useful when an application needs capabilities not covered by a built-in tool, especially when an agent must:

  • Call the application’s own APIs.
  • Apply business logic specific to the company or product.
  • Interact with the application’s own database, product, or infrastructure.

The video uses a travel-booking service as an example: an agent would need tools that connect to the service’s APIs or database to make bookings.

Three Ways to Create Custom Tools

1. Use the @tool Decorator

The presenter recommends this as the simplest and most commonly sufficient method:

  1. Write a Python function containing the task’s logic.
  2. Add a docstring describing what the function does. This is strongly recommended because it helps the LLM understand the tool.
  3. Add type hints to identify the expected input and return types. These clarify the tool’s interface.
  4. Apply the @tool decorator to make the function available as a LangChain tool.
  5. Invoke the tool by calling its invoke method with a dictionary containing the input values.

The example creates a multiplication tool and invokes it with two integers. The presenter notes that tools expose attributes such as name, description, and args. The description comes from the docstring, while the argument information reflects the function’s type hints.

What the LLM Receives

The LLM is not given the tool’s Python implementation. Instead, it receives a schema describing the tool, including its name, description, inputs, and required fields. This schema helps the LLM determine when and how to use the tool.

2. Use StructuredTool with Pydantic

This approach provides a more explicit and structured input schema:

  1. Define the function that performs the task.
  2. Create a Pydantic model that specifies the inputs, their types, and any field descriptions or requirements.
  3. Use StructuredTool.from_function, providing the function, tool name, description, and Pydantic model as the argument schema.

The presenter characterizes this as a stricter, more mature approach that can be useful for production-ready applications.

3. Inherit from BaseTool

For deeper customization, a developer can create a class that inherits from LangChain’s BaseTool:

  1. Define a class that inherits from BaseTool.
  2. Set the tool’s name and description.
  3. Provide an argument schema, such as a Pydantic model.
  4. Implement the required run method to perform the task.
  5. Instantiate the class and invoke the resulting tool.

This method offers more control and can support an asynchronous version of a tool, which may be useful when an application needs concurrency. The presenter suggests that the decorator approach is sufficient for many experiments and common cases, while the other approaches can suit more customized or production-level needs.

Toolkits

A toolkit is a collection of related tools grouped for convenience and reuse. For example, tools for Google Drive—such as searching, reading, and uploading files—could be grouped into a Google Drive toolkit.

The video’s custom example groups addition and multiplication tools into a math toolkit:

  1. Create the related tools.
  2. Define a toolkit class.
  3. Add a get_tools method that returns the tools included in the toolkit.
  4. Instantiate the toolkit and retrieve or iterate through its tools.

Toolkits help organize related functionality and make it easier to reuse tools across applications.

Sources and Speakers Featured

  • Presenter: Nitesh/Nitish Singh, associated with CampusX. The subtitles give the name as “Nitesh”; the video metadata identifies the presenter as Nitish Singh.
  • LangChain: The library and its tool documentation are discussed and demonstrated.
  • Google Colab: Used for the code demonstrations.
  • DuckDuckGo Search: Used in the built-in search-tool example.
  • Pydantic: Used to define structured input schemas for custom tools.

Rate this summary

Your feedback will help improve summaries.

Improve this summary

Reprocess with a stronger model when the summary feels incomplete or inaccurate.

Pro

Translate summary in another language

Pro

Ask questions to this video

Chat for follow-up questions, clarifications, and source-backed answers.

Coming soon

Share this summary

Original video