Exploring Agent with LangChain
Data Mastery Series — Episode 41: LangChain Website (Part 16)
Exploring Agent with LangChain
Data Mastery Series — Episode 41: LangChain Website (Part 16)

Connect with me and follow our journey: Linkedin, Facebook
Welcome back to the Data Mastery Series! If you’ve been following along, you know we’ve journeyed from the fundamentals of LangChain to more complex techniques, building a strong foundation for creating intelligent applications. Our path so far has included:
- Part 1: LangChain Model I/O Basics
- Part 2–3: Prompt Templates and Few-Shot Prompts
- Part 4–6: Deep Dive into Chat Models (Part 1, Part 2, Part 3)
- Part 7: LLM Fundamentals
- Part 8: Output Parsers
- Part 9: Document Loaders
- Part 10: Text Splitter
- Part 11: Embeddings and Vector Stores
- Part 12–14: Retrievers (Part 1, Part 2, Part3)
- Part 15: Agent (Part 1)
Note:This post is inspired by insights from the official LangChain documentation and represents my practical learning journey.
In this episode, we will delve deeper into agent concepts, examine the types of agents LangChain supports, and provide practical examples of their usage.
Agent Concepts: More Than Just Chains
angChain Agents are more dynamic and flexible than chains, serving as the core decision-making framework in AI applications. Unlike chains, which follow predefined action sequences, agents use language models to decide their next steps and the order of execution dynamically. This makes agents particularly suited for handling complex and multi-step tasks.
Key components of agents include:
1) Schema Abstractions: Simplify working with agents.
- AgentAction: Specifies the tool to invoke and its input.
- AgentFinish: Contains the agent’s final output.
- Intermediate Steps: Tracks previous actions and outputs to inform subsequent steps.
2) Agent: Determines the next action using a language model, prompt, and output parser.
- Agent Inputs: A key-value mapping with a required
intermediate_stepskey, typically formatted by aPromptTemplate. - Agent Outputs: The next action(s) to take or final response, typed as
Union[AgentAction, List[AgentAction], AgentFinish]. An output parser converts raw LLM output into these formats.
3) AgentExecutor: Executes agent logic, handles errors, logs decisions, and repeats actions until completion.
4) Tools: Functions an agent can invoke.
- Input schema: Defines parameters and descriptions for the LLM to understand tool usage.
- Functionality: Generally Python functions to execute specific tasks.
- Consideration:
- Ensure the agent has access to appropriate tools.
- Provide clear and helpful tool descriptions.
5) Toolkits: A collection of related tools designed for specific purposes, such as GitHub issue tracking or file management.
Agent Types: A Categorized Overview
To select the right agent type for your application, consider the following criteria:
- Intended Model Type: Compatibility with Chat Models (message-based) or LLMs (string-based).
- Chat History Support: Whether the agent can maintain conversational history or is suited for single tasks.
- Multi-Input Tool Support: Ability to work with tools requiring multiple inputs.
- Parallel Function Calling: Capability to execute multiple tool calls simultaneously for efficiency.
- Parameter Requirements: Whether the agent needs specific model parameters or relies solely on prompting.
Now, let’s dive into the specifics of each agent type:
1. Tool Calling Agent
- Intended Model Type: Chat
- Chat History: Supported
- Multi-Input Tools: Supported
- Parallel Function Calling: Supported
- Required Model Params: Requires tools
- When to Use: Ideal when using a model that supports tool calling natively.
This agent dynamically chooses tools to use based on the user input. Let’s see it in action using Tavily Search:
Example - Tool Calling Agent (using Tavily)
Set up model and Tools
llm = ChatOpenAI(model="gpt-3.5-turbo-0125", api_key=OPENAI_API_KEY)
tools = [TavilySearchResults(max_results=1)]
Create a prompt, initialize the agent, and run it!
prompt = ChatPromptTemplate.from_messages(
[
("system", "You are a helpful assistant. Make sure to use the tavily_search_results_json tool for information."),
("placeholder", "{chat_history}"),
("human", "{input}"),
("placeholder", "{agent_scratchpad}"),
]
)
agent = create_tool_calling_agent(llm, tools, prompt) # Tool Calling Agent
agent_executor = AgentExecutor(agent=agent, tools=tools, verbose=True)
agent_executor.invoke({"input": "what is LangChain?"})
Output:
'''
Entering new AgentExecutor chain...
Invoking: tavily_search_results_json with {'query': 'LangChain'}
[{'url': 'https://python.langchain.com/docs/introduction/', 'content': "How to use tools in a chain How to migrate from legacy LangChain agents to LangGraph How to use chat models to call tools LangChain is a framework for developing applications powered by large language models (LLMs). Development: Build your applications using LangChain's open-source building blocks, components, and third-party integrations. langchain: Chains, agents, and retrieval strategies that make up an application's cognitive architecture. LangServe: Deploy LangChain chains as REST APIs. LangSmith: A developer platform that lets you debug, test, evaluate, and monitor LLM applications. Build stateful, multi-actor applications with LLMs. Integrates smoothly with LangChain, but can be used without it. LangChain is part of a rich ecosystem of tools that integrate with our framework and build on top of it."}]LangChain is a framework for developing applications powered by large language models (LLMs). It provides open-source building blocks, components, and third-party integrations for building applications. LangChain consists of chains, agents, and retrieval strategies that form an application's cognitive architecture. Developers can use LangServe to deploy LangChain chains as REST APIs and LangSmith as a developer platform for debugging, testing, evaluating, and monitoring LLM applications. LangChain allows for building stateful, multi-actor applications with LLMs and integrates smoothly with other tools in its ecosystem. You can find more information about LangChain on their official website.
Finished chain.
{'input': 'what is LangChain?',
'output': "LangChain is a framework for developing applications powered by large language models (LLMs). It provides open-source building blocks, components, and third-party integrations for building applications. LangChain consists of chains, agents, and retrieval strategies that form an application's cognitive architecture. Developers can use LangServe to deploy LangChain chains as REST APIs and LangSmith as a developer platform for debugging, testing, evaluating, and monitoring LLM applications. LangChain allows for building stateful, multi-actor applications with LLMs and integrates smoothly with other tools in its ecosystem. You can find more information about LangChain on their official website."}
'''
Example - Tool Calling Agent (Using with chat history)
agent_executor.invoke(
{
"input": "what's my name? Don't use tools to look this up unless you NEED to",
"chat_history": [
HumanMessage(content="hi! my name is bob"),
AIMessage(content="Hello Bob! How can I assist you today?"),
],
}
)
Output:
'''
Entering new AgentExecutor chain...
Your name is Bob.
Finished chain.
{'input': "what's my name? Don't use tools to look this up unless you NEED to",
'chat_history': [HumanMessage(content='hi! my name is bob', additional_kwargs={}, response_metadata={}),
AIMessage(content='Hello Bob! How can I assist you today?', additional_kwargs={}, response_metadata={})],
'output': 'Your name is Bob.'}
'''
2. OpenAI Tools Agent
- Intended Model Type: Chat
- Chat History: Supported
- Multi-Input Tools: Supported
- Parallel Function Calling: Supported
- Required Model Params: Requires tools
- When to Use: Designed for OpenAI models
This agent was specifically designed for OpenAI’s models that support function calling. It’s similar to the generic tool calling agent, but is tailored to work with OpenAI’s API.
Example - OpenAI Tools Agent (using Tavily)
Set up Tools, model and initialize the agent
tools = [TavilySearchResults(max_results=1)]
prompt = hub.pull("hwchase17/openai-tools-agent")
llm = ChatOpenAI(model="gpt-3.5-turbo-0125", temperature=0, api_key=OPENAI_API_KEY)
agent = create_openai_tools_agent(llm, tools, prompt) # OpenAI Tools Agent
agent_executor = AgentExecutor(agent=agent, tools=tools, verbose=True)
agent_executor.invoke({"input": "what is LangChain?"})
Output:
'''
Entering new AgentExecutor chain...
Invoking: tavily_search_results_json with {'query': 'LangChain'}
[{'url': 'https://python.langchain.com/docs/introduction/', 'content': "How to use tools in a chain How to migrate from legacy LangChain agents to LangGraph How to use chat models to call tools LangChain is a framework for developing applications powered by large language models (LLMs). Development: Build your applications using LangChain's open-source building blocks, components, and third-party integrations. langchain: Chains, agents, and retrieval strategies that make up an application's cognitive architecture. LangServe: Deploy LangChain chains as REST APIs. LangSmith: A developer platform that lets you debug, test, evaluate, and monitor LLM applications. Build stateful, multi-actor applications with LLMs. Integrates smoothly with LangChain, but can be used without it. LangChain is part of a rich ecosystem of tools that integrate with our framework and build on top of it."}]LangChain is a framework for developing applications powered by large language models (LLMs). It provides open-source building blocks, components, and third-party integrations for building applications using chains, agents, and retrieval strategies that make up an application's cognitive architecture. LangChain includes tools like LangServe for deploying chains as REST APIs and LangSmith, a developer platform for debugging, testing, evaluating, and monitoring LLM applications. It allows the development of stateful, multi-actor applications with LLMs and integrates smoothly with LangChain but can also be used independently. LangChain is part of a rich ecosystem of tools that integrate with the framework and build on top of it. You can find more information about LangChain on their official documentation.
Finished chain.
{'input': 'what is LangChain?',
'output': "LangChain is a framework for developing applications powered by large language models (LLMs). It provides open-source building blocks, components, and third-party integrations for building applications using chains, agents, and retrieval strategies that make up an application's cognitive architecture. LangChain includes tools like LangServe for deploying chains as REST APIs and LangSmith, a developer platform for debugging, testing, evaluating, and monitoring LLM applications. It allows the development of stateful, multi-actor applications with LLMs and integrates smoothly with LangChain but can also be used independently. LangChain is part of a rich ecosystem of tools that integrate with the framework and build on top of it. You can find more information about LangChain on their official documentation."}
'''
Example - OpenAI Tools Agent (Using with chat history)
agent_executor.invoke(
{
"input": "what's my name? Don't use tools to look this up unless you NEED to",
"chat_history": [
HumanMessage(content="hi! my name is bob"),
AIMessage(content="Hello Bob! How can I assist you today?"),
],
}
)
Output:
'''
Entering new AgentExecutor chain...
Your name is Bob.
Finished chain.
{'input': "what's my name? Don't use tools to look this up unless you NEED to",
'chat_history': [HumanMessage(content='hi! my name is bob', additional_kwargs={}, response_metadata={}),
AIMessage(content='Hello Bob! How can I assist you today?', additional_kwargs={}, response_metadata={})],
'output': 'Your name is Bob.'}
'''
3. Structured Chat Agent
- Intended Model Type: Chat
- Chat History: Supported
- Multi-Input Tools: Supported
- Parallel Function Calling: Not Supported
- Required Model Params: None
- When to Use: This is your go-to agent when you need to support tools that have multiple inputs.
This agent is designed to handle tools with multiple inputs and is good for complex task management.
Example - Structured Chat Agent (using Tavily)
Set up Tools, model and initialize the agent
tools = [TavilySearchResults(max_results=1)]
prompt = hub.pull("hwchase17/structured-chat-agent")
llm = ChatOpenAI(temperature=0, model="gpt-3.5-turbo-0125", api_key=OPENAI_API_KEY)
agent = create_structured_chat_agent(llm, tools, prompt) # Structured Chat Agent
agent_executor = AgentExecutor(
agent=agent, tools=tools, verbose=True, handle_parsing_errors=True
)
agent_executor.invoke({"input": "what is LangChain?"})
Output:
'''
Entering new AgentExecutor chain...
Could not parse LLM output: Action:
{
"action": tavily_search_results_json,
"action_input": "LangChain"
}
For troubleshooting, visit: https://python.langchain.com/docs/troubleshooting/errors/OUTPUT_PARSING_FAILURE Invalid or incomplete responseAction:
{
"action": "tavily_search_results_json",
"action_input": "LangChain"
}
```[{'url': 'https://python.langchain.com/docs/introduction/', 'content': "How to use tools in a chain How to migrate from legacy LangChain agents to LangGraph How to use chat models to call tools LangChain is a framework for developing applications powered by large language models (LLMs). Development: Build your applications using LangChain's open-source building blocks, components, and third-party integrations. langchain: Chains, agents, and retrieval strategies that make up an application's cognitive architecture. LangServe: Deploy LangChain chains as REST APIs. LangSmith: A developer platform that lets you debug, test, evaluate, and monitor LLM applications. Build stateful, multi-actor applications with LLMs. Integrates smoothly with LangChain, but can be used without it. LangChain is part of a rich ecosystem of tools that integrate with our framework and build on top of it."}]Action:
{
"action": "Final Answer",
"action_input": "LangChain is a framework for developing applications powered by large language models (LLMs). It provides open-source building blocks, components, and third-party integrations for building applications using chains, agents, and retrieval strategies. LangChain includes tools like LangServe for deploying chains as REST APIs and LangSmith for debugging, testing, evaluating, and monitoring LLM applications. It allows the development of stateful, multi-actor applications with LLMs and integrates with a rich ecosystem of tools."
}
> Finished chain.
{'input': 'what is LangChain?',
'output': 'LangChain is a framework for developing applications powered by large language models (LLMs). It provides open-source building blocks, components, and third-party integrations for building applications using chains, agents, and retrieval strategies. LangChain includes tools like LangServe for deploying chains as REST APIs and LangSmith for debugging, testing, evaluating, and monitoring LLM applications. It allows the development of stateful, multi-actor applications with LLMs and integrates with a rich ecosystem of tools.'}
'''
# Example - Structured Chat Agent (Using with chat history)
agent_executor.invoke(
{
"input": "what's my name? Do not use tools unless you have to",
"chat_history": [
HumanMessage(content="hi! my name is bob"),
AIMessage(content="Hello Bob! How can I assist you today?"),
],
}
)
# Output:
'''
> Entering new AgentExecutor chain...
{
"action": "Final Answer",
"action_input": "Your name is Bob."
}
> Finished chain.
{'input': "what's my name? Do not use tools unless you have to",
'chat_history': [HumanMessage(content='hi! my name is bob', additional_kwargs={}, response_metadata={}),
AIMessage(content='Hello Bob! How can I assist you today?', additional_kwargs={}, response_metadata={})],
'output': 'Your name is Bob.'}
'''
### 4\. JSON Chat Agent
* **Intended Model Type**: Chat
* **Chat History**: Supported
* **Multi-Input Tools**: Not Supported
* **Parallel Function Calling**: Not Supported
* **Required Model Params**: None
* **When to Use**: Ideal when using a model that is particularly good at generating JSON data.
This agent is optimized to handle JSON formatting of its outputs.
# Example - JSON Chat Agent (using Tavily)
# Set up Tools, model and initialize the agent
tools = [TavilySearchResults(max_results=1)]
prompt = hub.pull("hwchase17/react-chat-json")
llm = ChatOpenAI(temperature=0, model="gpt-3.5-turbo-0125", api_key=OPENAI_API_KEY)
agent = create_json_chat_agent(llm, tools, prompt) # JSON Chat Agent
agent_executor = AgentExecutor(
agent=agent, tools=tools, verbose=True, handle_parsing_errors=True
)
agent_executor.invoke({"input": "what is LangChain?"})
# Output:
'''
> Entering new AgentExecutor chain...
```json
{
"action": "tavily_search_results_json",
"action_input": "LangChain"
}
```[{'url': 'https://python.langchain.com/docs/introduction/', 'content': "How to use tools in a chain How to migrate from legacy LangChain agents to LangGraph How to use chat models to call tools LangChain is a framework for developing applications powered by large language models (LLMs). Development: Build your applications using LangChain's open-source building blocks, components, and third-party integrations. langchain: Chains, agents, and retrieval strategies that make up an application's cognitive architecture. LangServe: Deploy LangChain chains as REST APIs. LangSmith: A developer platform that lets you debug, test, evaluate, and monitor LLM applications. Build stateful, multi-actor applications with LLMs. Integrates smoothly with LangChain, but can be used without it. LangChain is part of a rich ecosystem of tools that integrate with our framework and build on top of it."}]```json
{
"action": "Final Answer",
"action_input": "LangChain is a framework for developing applications powered by large language models (LLMs). It includes open-source building blocks, components, and third-party integrations for building applications. LangChain consists of chains, agents, and retrieval strategies that form an application's cognitive architecture. Additionally, LangChain offers tools like LangServe for deploying chains as REST APIs and LangSmith for debugging, testing, evaluating, and monitoring LLM applications. It allows for building stateful, multi-actor applications with LLMs and integrates with a rich ecosystem of tools that complement the framework."
}
Finished chain.
{'input': 'what is LangChain?',
'output': "LangChain is a framework for developing applications powered by large language models (LLMs). It includes open-source building blocks, components, and third-party integrations for building applications. LangChain consists of chains, agents, and retrieval strategies that form an application's cognitive architecture. Additionally, LangChain offers tools like LangServe for deploying chains as REST APIs and LangSmith for debugging, testing, evaluating, and monitoring LLM applications. It allows for building stateful, multi-actor applications with LLMs and integrates with a rich ecosystem of tools that complement the framework."}
'''
Example - JSON Chat Agent (Using with chat history)
agent_executor.invoke(
{
"input": "what's my name?",
"chat_history": [
HumanMessage(content="hi! my name is bob"),
AIMessage(content="Hello Bob! How can I assist you today?"),
],
}
)
Output:
'''
Entering new AgentExecutor chain...
{
"action": "Final Answer",
"action_input": "Your name is Bob."
}
Finished chain.
{'input': "what's my name?",
'chat_history': [HumanMessage(content='hi! my name is bob', additional_kwargs={}, response_metadata={}),
AIMessage(content='Hello Bob! How can I assist you today?', additional_kwargs={}, response_metadata={})],
'output': 'Your name is Bob.'}
'''
5. ReAct Agent
- Intended Model Type: LLM
- Chat History: Supported
- Multi-Input Tools: Not Supported
- Parallel Function Calling: Not Supported
- Required Model Params: None
- When to Use: Best for simple models where a reasoning process is needed.
The ReAct agent (Reasoning and Acting) makes decisions in a step-by-step manner. It first “thinks” about what it needs to do, then “acts” on that thought. It is good for simple model.
Example - ReAct Agent (using Tavily)
Set up Tools, model and initialize the agent
tools = [TavilySearchResults(max_results=1)]
prompt = hub.pull("hwchase17/react")
llm = ChatOpenAI(temperature=0, model="gpt-3.5-turbo-0125", api_key=OPENAI_API_KEY)
agent = create_react_agent(llm, tools, prompt) # ReAct Agent
agent_executor = AgentExecutor(agent=agent, tools=tools, verbose=True)
agent_executor.invoke({"input": "what is LangChain?"})
Output:
'''
Entering new AgentExecutor chain...
I should use tavily_search_results_json to find information about LangChain.
Action: tavily_search_results_json
Action Input: LangChain[{'url': 'https://python.langchain.com/docs/introduction/', 'content': "How to use tools in a chain How to migrate from legacy LangChain agents to LangGraph How to use chat models to call tools LangChain is a framework for developing applications powered by large language models (LLMs). Development: Build your applications using LangChain's open-source building blocks, components, and third-party integrations. langchain: Chains, agents, and retrieval strategies that make up an application's cognitive architecture. LangServe: Deploy LangChain chains as REST APIs. LangSmith: A developer platform that lets you debug, test, evaluate, and monitor LLM applications. Build stateful, multi-actor applications with LLMs. Integrates smoothly with LangChain, but can be used without it. LangChain is part of a rich ecosystem of tools that integrate with our framework and build on top of it."}]LangChain is a framework for developing applications powered by large language models.
Final Answer: LangChain is a framework for developing applications powered by large language models.
Finished chain.
{'input': 'what is LangChain?',
'output': 'LangChain is a framework for developing applications powered by large language models.'}
'''
Example - ReAct Agent (Using with chat history)
agent_executor.invoke(
{
"input": "what's my name? Only use a tool if needed, otherwise respond with Final Answer",
"chat_history": "Human: Hi! My name is Bob\nAI: Hello Bob! Nice to meet you",
}
)
Output:
'''
Entering new AgentExecutor chain...
/usr/local/lib/python3.10/dist-packages/langsmith/client.py:261: LangSmithMissingAPIKeyWarning: API key must be provided when using hosted LangSmith API
warnings.warn(
Thought: Do I need to use a tool? No
Final Answer: Your name is Bob
Finished chain.
{'input': "what's my name? Only use a tool if needed, otherwise respond with Final Answer",
'chat_history': 'Human: Hi! My name is Bob\nAI: Hello Bob! Nice to meet you',
'output': 'Your name is Bob'}
'''
Key Takeaways
In this episode, we expanded our understanding of LangChain Agents by exploring various agent types, their use cases, supported features, and ideal scenarios. Here’s a quick recap:
- Tool Calling Agents: Best for newer models that natively support tool usage.
- OpenAI Tools Agent: A legacy agent tailored for OpenAI but being replaced by Tool Calling agents
- Structured Chat Agent: The right choice for tools that require multiple inputs.
- JSON Chat Agent: Optimal when dealing with models that excel at JSON outputs.
- ReAct Agent: Good for simple models that need to think through a problem step by step.
In the next episode, we’ll dive deeper into Tools — examining their design, functionality, and how they enhance the power of LangChain Agents. 🚀
Data Science Explore the world of data science with Donato_Story
Dashboard Discover the power of data visualization with Donato_Story
Donato_Journey Join me on my journey (Thai version)
Course_Review Discover the training courses with Donato_Story (Thai version)
Let’s Connect!
Your thoughts and feedback are invaluable. Feel free to share them in the comments or connect with me on
- Medium: medium.com/donato-story
- Facebook: web.facebook.com/DonatoStory
- Linkedin: linkedin.com/in/nattapong-thanngam
Originally published on Medium
Related
Corrective RAG
Data Mastery Series — Episode 53: RAG ที่ “คิด” ก่อน “ตอบ” และ “แก้ไข” เมื่อผิดพลาด
Exploring LangGraph with LangChain
Data Mastery Series — Episode 44: LangChain Website (Part 19)
Hierarchical Multi-Agent Systems
Data Mastery Series — Episode 59: การสร้างระบบ AI ทีมงานด้วย Supervisor Agent กับทีมย่อย
LangGraph Introduction
Data Mastery Series — Episode 50: Next-Level Chat with Document