Working through this module?0 of 17 done
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.

7.1

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.
7.2

How Agents Work

  1. 1Receive user input (goal / question).
  2. 2Understand the task and plan the next step.
  3. 3Select the appropriate tool from the available tools.
  4. 4Execute the tool with the required input.
  5. 5Observe the result and decide the next action.
  6. 6Repeat the process until the final answer is ready.
7.3

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.

7.4

Types of Agents in LangChain

Agent TypeDescription
Zero-shot AgentUses the LLM to decide tool usage without examples.
ReAct AgentUses a reasoning (Thought → Action → Observation) pattern.
Structured Chat AgentWorks with a structured chat format.
OpenAI Functions AgentUses OpenAI function calling.
Tool Calling AgentUses tool-calling models (e.g., GPT-4).
Self-Ask with SearchBreaks complex questions into simpler sub-questions.
7.5

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"])
7.6

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"])
7.7

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.
7.8

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.