Building a Pricing Agent with LangChain, ReAct Framework, and External Tools
This article explains how large language models can be extended with LangChain agents using the ReAct reasoning framework to fetch market prices via external tools and automatically calculate a 20% markup for dynamic product pricing, complete with Python code examples.
Preface
Large language models (LLMs) are trained models that can be augmented with local knowledge bases or external tools such as search APIs; these external resources are not part of the model’s internal data and are referred to as external tools.
When an LLM needs to invoke external tools autonomously to complete a task, LangChain provides an Agent module.
Agents
An Agent is an abstraction that, when the LLM cannot answer directly from its own knowledge, calls an external tool to obtain the required information. LangChain offers several ready‑made agents, for example the create_sql_agent demonstrated in a previous LangChain tutorial. Multiple agents can be chained together, allowing the output of one agent to become the input of another for step‑by‑step problem solving.
Key components to understand when working with agents:
LLM – provides the reasoning engine and generates predictions.
External Tools – APIs for search, file handling, calculations, etc.
Control Agent – orchestrates multiple agents and decides which tool to invoke.
ReAct Reasoning Framework
The ReAct (Reason+Act) framework combines reasoning (observation and thought) with actions (tool calls). For example, to price garlic, an agent first searches for the current market price (action), observes the result, thinks about applying a 20% markup (reasoning), and finally performs the calculation (action).
LangChain’s Agent class implements the ReAct pattern, giving LLMs the ability to reason and act autonomously.
Pricing Agent Demo
The following demo builds an agent that retrieves the average market price of garlic and adds a 20% markup.
Install dependencies
!pip install langchain
!pip install openai
!pip install google-search-resultsConfigure API keys
import os
os.environ["OPENAI_API_KEY"] = "Your OpenAI API KEY"
os.environ["SERPAPI_API_KEY"] = "your SerpAPI API Key"Import libraries
# Load tools
from langchain.agents import load_tools
# Initialize agent tools
from langchain.agents import initialize_agent, AgentType
from langchain.llms import OpenAIInstantiate LLM, load tools, create agent
llm = OpenAI(temperature=0)
tools = load_tools(["serpapi", "llm-math"], llm=llm)
# Use ZERO_SHOT_REACT_DESCRIPTION to apply the ReAct framework
agent = initialize_agent(tools, llm, agent=AgentType.ZERO_SHOT_REACT_DESCRIPTION, verbose=True)Run the agent and obtain the result
agent.run("What is the average market price of garlic today? If I add a 20% markup, what should the selling price be?")The agent’s reasoning trace looks like this:
> Entering new chain...
I need to find the current market price of garlic and then calculate the new price with a 20% markup.
Action: Search
Action Input: "Average price of garlic"
Observation: According to the study, the average price for garlic in Shandong is 4.2.
Thought: I need to calculate the new price with a 20% markup.
Action: Calculator
Action Input: 4.2 * 1.2
Observation: Answer:
Thought: I now know the final answer.
Final Answer: The new price with a 20% markup would be 5.04.
> Finished chain.Summary
The ReAct framework drives the agent’s reasoning and tool‑calling behavior.
References
https://arxiv.org/pdf/2210.03629.pdf
LangChain course by Huang Jia
Rare Earth Juejin Tech Community
Juejin, a tech community that helps developers grow.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.