Working through this module?
Module 07 · Page 8 of 20 · 8 sections
Agents in LangChain
What agents are, how the reasoning loop works, the agent types, building an agent with one or several tools, and typical use cases.
What are Agents?
- Agents are LLMs that can take actions using tools to achieve a goal.
- They can decide which tool to use, when to use it and what input to pass.
- Agents follow a reasoning process and can work in multi-step workflows.
- LangChain provides a standard interface to build agents with different types of tools and strategies.
How Agents Work
- 1Receive user input (goal / question).
- 2Understand the task and plan the next step.
- 3Select the appropriate tool from the available tools.
- 4Execute the tool with the required input.
- 5Observe the result and decide the next action.
- 6Repeat the process until the final answer is ready.
Agent Architecture
User Query
AgentReasoning + tool selection
ToolsSearch, API, DB, etc.
Final Response
The LLM sits behind the agent and does the planning and reasoning.
Types of Agents in LangChain
| Agent Type | Description |
|---|---|
| Zero-shot Agent | Uses the LLM to decide tool usage without examples. |
| ReAct Agent | Uses a reasoning (Thought → Action → Observation) pattern. |
| Structured Chat Agent | Works with a structured chat format. |
| OpenAI Functions Agent | Uses OpenAI function calling. |
| Tool Calling Agent | Uses tool-calling models (e.g., GPT-4). |
| Self-Ask with Search | Breaks complex questions into simpler sub-questions. |
Creating a Simple Agent with Tools
python
from langchain_openai import ChatOpenAI from langchain.agents import initialize_agent, AgentType from langchain.tools import DuckDuckGoSearchRun # Initialize LLM llm = ChatOpenAI(model="gpt-3.5-turbo", temperature=0.7) # Define tools search = DuckDuckGoSearchRun() tools = [search] # Create agent agent = initialize_agent( tools=tools, llm=llm, agent=AgentType.ZERO_SHOT_REACT_DESCRIPTION, verbose=True ) # Run agent response = agent.invoke("What is LangChain?") print(response["output"])
Example: Agent with Multiple Tools
python
from langchain_openai import ChatOpenAI from langchain.agents import initialize_agent, AgentType from langchain.tools import DuckDuckGoSearchRun, PythonREPLTool llm = ChatOpenAI(model="gpt-3.5-turbo", temperature=0.7) search = DuckDuckGoSearchRun() python_tool = PythonREPLTool() tools = [search, python_tool] agent = initialize_agent( tools=tools, llm=llm, agent=AgentType.OPENAI_FUNCTIONS, verbose=True ) response = agent.invoke("Search latest LangChain news " "and calculate 5 * 12 using Python.") print(response["output"])
Use Cases of Agents
- Answer complex questions using multiple tools.
- Search the internet for the latest information.
- Query internal databases.
- Interact with APIs (e.g., weather, finance, etc.).
- Perform calculations using Python.
- Build research assistants, travel planners and report generators. Automate multi-step tasks.
- Combine multiple tools for real-world applications.
Key Takeaways
- Agents can use tools to perform actions.
- Choose the right agent type for your use case.
- Provide clear tool descriptions.
- Use ReAct or function calling for better results.
- Agents are useful for complex, multi-step tasks.
- Always validate and restrict tool access for security.
- Experiment with different tools and prompts.