LangChain is a framework for building applications with Large Language Models (LLMs). Its core components are Tools and Agents. Tools extend the capabilities of LLMs, while agents orchestrate tools to solve complex tasks intelligently.
Tools: External functions, APIs or logic that an agent can call.
Agents: LLM-powered entities that reason, plan and decide which tools to use to solve a query.
An Agent is an LLM-powered system that plans, reasons and decides which tools to use to solve user queries. Agents are more intelligent than a standalone LLM because they can:
Select tools based on task requirements.
Chain multiple steps.
Observe outputs and adjust decisions dynamically.
Types of Agents
1. OpenAI Function Agent: Uses OpenAI's function-calling API. This agent can structure outputs, call predefined functions and receive structured responses. Use Case: Form-filling, structured API queries or validated output generation.
Implementation
@tool decorator: Registers greet function as a tool that the agent can call.
ChatOpenAI: Initializes the LLM with the GPT-4 function-calling model.
ChatPromptTemplate: Sets up the conversation prompt for the agent.
create_openai_functions_agent: Creates an agent that uses OpenAI function-calling to invoke tools.
AgentExecutor: Wraps the agent to execute and manage tool calls.
invoke({"input": "Greet Alice"}): The agent receives a user query and decides to call the greet tool.
2. OpenAI Tools Agent: Uses the LLMβs reasoning to choose which tool to invoke. Use Case: Dynamic tasks where the agent decides which API or function is needed for the query.
Implementation
@tool add(a,b): Registers a simple addition function as a tool.
ChatOpenAI(model="gpt-3.5-turbo"): Uses a GPT-3.5 LLM for reasoning.
create_openai_tools_agent: Creates an agent that can choose tools dynamically based on the query.
AgentExecutor: Manages the agentβs execution and tool invocation.
3. ReAct Agent: Combines Reasoning + Acting. The agent uses observations from tools to update its reasoning iteratively. Use Case: Complex problem solving, multi-step planning or tasks that require trial-and-error reasoning.
Implementation
REACT_PROMPT_TEMPLATE: Defines the ReAct reasoning format with Thought β Action β Observation.
@tool echo: Registers a tool to simply return input as output.
create_react_agent: Initializes an agent that iteratively reasons and acts.
AgentExecutor(handle_parsing_errors=True): Ensures the agent handles output parsing robustly.
A Tool is any function, API or computational module that an agent can call. Tools extend the capabilities of LLMs beyond simple text generation, enabling dynamic computation, data retrieval and document processing.
Tools are modular and reusable.
Each tool has a name, function and description.
Tools can be simple (calculator) or complex (flight booking, real-time web search).
Agents rely on tool descriptions to decide which tool to invoke.
Types of Tools
1. Calculator Tool: Handles numerical calculations and simple logic. Useful for financial, scientific or mathematical tasks.
Code:
calculator(expression: str): Function evaluates a math expression.
Tool(...): Registers the function as a LangChain tool with name and description.
calc_tool.run("125 * 12"): Invokes the tool with input "125 * 12".
Output:
Calculator Tool Output: 1500
2. Web Search Tool: Provides real-time access to knowledge or news. Important for questions requiring up-to-date information.
Code:
DuckDuckGoSearchRun(): Provides a lightweight web search tool.
Tool(...): Wraps the search function as a LangChain tool.
search_tool.run("LangChain Python tutorials"): Executes the search query.
Output:
Web Search Tool Output: In this tutorial, we'll walk through a basic RAG flow using Python, LangChain, ChromaDB and OpenAI ...
3. PDF Reader Tool: Extracts content from documents, enabling agents to reason over structured or unstructured text. Useful for summarization, document Q&A or knowledge extraction.
Code:
PyPDFLoader(file_path): Loads PDF and splits it into pages.
read_pdf(...): Combines all page content into a single string.
Tool(...): Registers PDF reader as a LangChain tool.
pdf_tool.run("sample.pdf"): Extracts content from the PDF file.
A multitool agent in LangChain is an agent that has access to and can intelligently use multiple tools to solve a given task or answer a userβs query, rather than being limited to just one external function or data source.
It uses a large language model (LLM) to reason about which tools to use at each step, deciding the best tools' to call from a set of registered functions or APIs.
Tools can include search engines, calculators, code execution, document retrieval, API calls, and much more.
The agent can use several tools in sequence (or in parallel, depending on the workflow), chaining their outputs to answer complex, multi-step questions.