By Sagar Shankaran, Founder of CallSphere
Learn how to build LangChain agents that use tools to solve problems, understand the ReAct reasoning loop, and configure AgentExecutor for reliable agent behavior.
Key takeaways
A chain follows a fixed path: prompt goes in, response comes out. An agent, by contrast, decides at each step what action to take. It can call tools, inspect results, reason about what to do next, and repeat until it has enough information to answer the original question.
LangChain agents implement the ReAct (Reasoning + Acting) pattern. The model alternates between reasoning about the problem and taking actions (tool calls). This loop continues until the model decides it can produce a final answer.
Tools are functions that an agent can invoke. Each tool has a name, a description (used by the LLM to decide when to call it), and an implementation.
flowchart TD
USER(["User message"])
LLM["LLM call<br/>with tools schema"]
DECIDE{"Model wants<br/>to call a tool?"}
EXEC["Execute tool<br/>sandboxed runtime"]
RESULT["Append tool_result<br/>to messages"]
GUARD{"Output passes<br/>guardrails?"}
DONE(["Final reply"])
BLOCK(["Refuse and log"])
USER --> LLM --> DECIDE
DECIDE -->|Yes| EXEC --> RESULT --> LLM
DECIDE -->|No| GUARD
GUARD -->|Yes| DONE
GUARD -->|No| BLOCK
style LLM fill:#4f46e5,stroke:#4338ca,color:#fff
style EXEC fill:#ede9fe,stroke:#7c3aed,color:#1e1b4b
style GUARD fill:#f59e0b,stroke:#d97706,color:#1f2937
style DONE fill:#059669,stroke:#047857,color:#fff
style BLOCK fill:#dc2626,stroke:#b91c1c,color:#fff
from langchain_core.tools import tool
@tool
def multiply(a: float, b: float) -> float:
"""Multiply two numbers together."""
return a * b
@tool
def web_search(query: str) -> str:
"""Search the web for current information about a topic."""
# In production, this would call a search API
return f"Search results for: {query}"
print(multiply.name) # "multiply"
print(multiply.description) # "Multiply two numbers together."
The @tool decorator automatically extracts the function name, docstring, and parameter types to build the tool schema. The LLM sees the name, description, and parameter schema when deciding which tool to call.
Hear it before you finish reading
Talk to a live CallSphere AI voice agent in your browser — 60 seconds, no signup.
Modern LangChain agents use the create_tool_calling_agent function, which leverages native tool-calling capabilities of chat models rather than parsing text output.
from langchain_openai import ChatOpenAI
from langchain_core.prompts import ChatPromptTemplate, MessagesPlaceholder
from langchain.agents import create_tool_calling_agent, AgentExecutor
# Define the prompt with required placeholders
prompt = ChatPromptTemplate.from_messages([
("system", "You are a helpful math assistant."),
MessagesPlaceholder("chat_history", optional=True),
("human", "{input}"),
MessagesPlaceholder("agent_scratchpad"),
])
# Create model and bind tools
llm = ChatOpenAI(model="gpt-4o", temperature=0)
tools = [multiply, web_search]
# Build the agent
agent = create_tool_calling_agent(llm, tools, prompt)
# Wrap in AgentExecutor
executor = AgentExecutor(agent=agent, tools=tools, verbose=True)
The agent_scratchpad placeholder is where intermediate tool calls and results are stored during the reasoning loop.
result = executor.invoke({
"input": "What is 47.5 multiplied by 23.8?"
})
print(result["output"])
With verbose=True, you will see the full reasoning trace:
multiply tool with arguments a=47.5, b=23.81130.5Each iteration of the agent loop follows this pattern:
The AgentExecutor manages this loop and provides safeguards:
executor = AgentExecutor(
agent=agent,
tools=tools,
verbose=True,
max_iterations=10, # Prevent infinite loops
handle_parsing_errors=True, # Recover from malformed output
return_intermediate_steps=True, # Include tool call history
)
result = executor.invoke({"input": "Search for LangChain news"})
# Access intermediate steps
for step in result["intermediate_steps"]:
action, observation = step
print(f"Tool: {action.tool}, Input: {action.tool_input}")
print(f"Result: {observation}")
Here is a more complete agent that combines calculation and search capabilities.
Still reading? Stop comparing — try CallSphere live.
CallSphere ships complete AI voice agents per industry — 14 tools for healthcare, 10 agents for real estate, 4 specialists for salons. See how it actually handles a call before you book a demo.
from langchain_core.tools import tool
from langchain_openai import ChatOpenAI
from langchain_core.prompts import ChatPromptTemplate, MessagesPlaceholder
from langchain.agents import create_tool_calling_agent, AgentExecutor
@tool
def calculate(expression: str) -> str:
"""Evaluate a mathematical expression. Use Python syntax."""
try:
result = eval(expression)
return str(result)
except Exception as e:
return f"Error: {e}"
@tool
def get_current_date() -> str:
"""Get today's date."""
from datetime import date
return date.today().isoformat()
tools = [calculate, get_current_date]
prompt = ChatPromptTemplate.from_messages([
("system", "You are a helpful assistant with access to tools. "
"Always use tools when a calculation is needed."),
("human", "{input}"),
MessagesPlaceholder("agent_scratchpad"),
])
agent = create_tool_calling_agent(
ChatOpenAI(model="gpt-4o-mini", temperature=0),
tools,
prompt,
)
executor = AgentExecutor(agent=agent, tools=tools, verbose=True)
response = executor.invoke({
"input": "What day is it and what is 2 raised to the 20th power?"
})
print(response["output"])
The agent will call both tools — get_current_date and calculate("2**20") — then combine the results into a coherent answer.
create_tool_calling_agent uses the native tool-calling API supported by modern LLMs, which returns structured tool calls in the response. create_react_agent relies on text-based parsing of the ReAct format. The tool-calling approach is more reliable and is the recommended default.
Set max_iterations on the AgentExecutor. The default is 15. If the agent exceeds this limit, it returns an error message. You can also set max_execution_time (in seconds) as a wall-clock timeout.
Yes. The agent can call any tool any number of times across iterations. It can also call multiple tools in a single step if the model supports parallel tool calling.
#LangChain #AIAgents #ReAct #ToolUse #Python #AgenticAI #LearnAI #AIEngineering

Written by
Sagar Shankaran· Founder, CallSphere
LinkedInSagar Shankaran is the founder of CallSphere, where he builds production AI voice and chat agents deployed across healthcare, hospitality, real estate, and home services. He writes about agentic AI, LLM engineering, and shipping voice agents that handle real calls in production.
See how AI voice agents work for your industry. Live demo available -- no signup required.
A founder's guide to the personal AI assistant market: best AI assistant apps, business-grade options, and how CallSphere's voice agent fits in.
A founder's guide to free AI agents, low-code AI agent builders, and how to know when you should pay for a real platform like CallSphere.
Graphiti is the open-source temporal knowledge graph for AI agents in 2026. Learn how bi-temporal memory beats vector RAG for voice agents and long-running LLMs.
Chatbot app vs ChatGPT in 2026: a founder's clear take on the difference, when to use which, and how a real AI chatbot app development works.
How we built a fault-tolerant HVAC emergency triage and tech-dispatch platform on Kubernetes — three-tier CQRS, 11 micro-agents on the OpenAI Agents SDK + LangGraph, NATS JetStream, DTMF/SMS/WebSocket acceptance, circuit breakers, and an evaluation pipeline that catches regressions before they wake a tech at 3 AM.
GPT-Realtime-2 brings GPT-5-class reasoning into voice. What that means for tool-call reliability, structured output, and production agent design.
© 2026 CallSphere Inc. All rights reserved.
Made within San Francisco
Watch how CallSphere handles real customer calls, schedules appointments, and processes payments — live.