Introduction
Since the release of GPT-3.5 and GPT-4, text generation and other natural language capabilities have become significantly more powerful and widely used. Despite these advances, large language models (LLMs) still face limitations — they can’t directly access real-time information, interact with APIs, or reason through multi-step problems on their own.
LangChain, developed by Harrison Chase and maintained by LangChain Inc., addresses this gap by offering a framework to connect LLMs with tools. It supports tasks such as web searches, mathematical calculations, file parsing, and database queries, enabling models to move beyond simple prompt-response patterns.
Architecture of Our Code Demo
The demo code mainly focuses on an agent and initialized with a set of tools and a language model. The tools we used are:
- SerpAPI: Real-time Google Search
- LLMMathTool: Math computations using GPT
- ChatOpenAI: OpenAI’s GPT-3.5 model
Demo Code
from dotenv import load_dotenv
from langchain_community.agent_toolkits.load_tools import load_tools
from langchain_openai import ChatOpenAI
from langchain.agents import create_react_agent, AgentExecutor
from langchain_core.prompts import PromptTemplate
from contextlib import redirect_stdout
import os
load_dotenv()
def lang_chain_demo():
llm = ChatOpenAI(
temperature=0,
model_name="gpt-4o",
api_key=os.getenv("OPENAI_API_KEY")
)
print("???? Loading tools...")
raw_tools = load_tools(["serpapi", "llm-math"], llm=llm)
tools = []
for t in raw_tools:
if not t or not hasattr(t, "name"):
continue
tools.append(t)
if not tools:
raise ValueError("No valid tools found.")
print(f"✅ Loaded tools: {[t.name for t in tools]}")
# Required prompt format includes "tools"
template = """Answer the following questions as best you can. You have access to the following tools:
{tool_names}
TOOLS DESCRIPTION:
{tools}
Use the following format:
Question: the input question you must answer
Thought: you should always think about what to do
Action: the action to take, should be one of [{tool_names}]
Action Input: the input to the action
Observation: the result of the action
... (this Thought/Action/Action Input/Observation can repeat N times)
Thought: I now know the final answer
Final Answer: the final answer to the original input question
Begin!
Question: {input}
{agent_scratchpad}"""
prompt = PromptTemplate(
template=template,
input_variables=["input", "agent_scratchpad", "tool_names", "tools"]
)
print("???? Creating agent...")
agent = create_react_agent(llm=llm, tools=tools, prompt=prompt)
agent_executor = AgentExecutor(agent=agent, tools=tools, verbose=True)
query = "What is the square root of the population of Germany, and who is its current Chancellor?"
response = agent_executor.invoke({"input": query})
print("\n???? Final Answer:\n", response['output'])
Results
To evaluate our agent, we submitted a multi-part question asking for the square root of Germany’s population and the identity of its current chancellor. The agent followed a clear reasoning process.
- It first used the SerpAPI tool to fetch up-to-date information about Germany’s population and its political leadership.
- The population was returned as approximately 83 million, and Olaf Scholz was identified as the chancellor for the year 2023.
- Next, the agent invoked the
LLMMathTool
to calculate the square root of the population. The result was around 9,125.79. - Finally, the agent compiled everything into a readable answer:
“The square root of the population of Germany is approximately 9,125.79, and the current Chancellor of Germany in 2023 is Olaf Scholz.”
This output shows that the agent was able to combine real-time data retrieval and use a simple math computation, and present the final result in a natural, coherent sentence. It demonstrates how LangChain agents can handle multi-step reasoning using external tools.
Our experiment demonstrated that LangChain significantly simplifies the process of coordinating multiple tools for complex reasoning tasks. Instead of manually handling each API or response, LangChain lets developers focus on designing the logic, while the framework manages the orchestration behind the scenes. By combining tool usage with natural language-driven planning, it becomes much easier to build workflows that are both modular and reusable.
Discussion Of Results
One of the main strengths is that LangChain can easily integrates with external APIs. It also supports agent-based reasoning, allowing the model to decide which tools to use and when.
In our experiment, this became evident when the user asked for both the square root of Germany’s population and the name of the current Chancellor.
- The agent first invoked the Search tool to gather up-to-date information about Germany’s population and its political leadership.
- Then, recognizing the need for a numerical computation, it switched to the Calculator tool to compute the square root. This decision-making process, where the model reflects, selects a tool, and then acts, is how LangChain enables multi-step reasoning.
- It demonstrates that LangChain agents go beyond prompt completion; they can intelligently orchestrate a sequence of actions using external tools in a context-aware manner.
However, there are some limitations for LangChain.
- The cost of using LLMs like OpenAI’s GPT models and external APIs will be added up quickly, especially in frequent or large-scale deployments.
- Handling multi-step queries leads to slower response times.
- Parsing the results returned from tools often requires careful handling to avoid errors or confusion.
Future Work
- integrating memory capabilities, allowing the agent to retain conversational context across multiple turns.
- adding retrievers could enable more advanced document or file-level searches
- creating custom tools tailored to specific needs, for example, PDF analyzers.
Conclusion
LangChain represents a meaningful shift from traditional prompt-response language models toward systems capable of autonomous, tool-augmented reasoning. By combining APIs, tools, and LLMs into one framework, it enables agents to perform complex, multi-step tasks. Our implementation confirms that LangChain agents are capable of executing meaningful, multi-step tasks using natural language as the control layer, which demonstrates that LangChain is a powerful tool for building intelligent assistants, research tools, and workflow automation systems.
References
- LangChain Documentation (https://python.langchain.com/docs/introduction/)
- Serp API(https://serpapi.com/)
- OpenAI API (https://platform.openai.com/docs/overview)