Module 10 of 12
Custom Tools & Function Calling
Current Study Module
LangChain - Autonomous Agents & ReAct Loop
LangGraph & Multi-Agent Workflows
Module 09 • Autonomous Reasoning

Autonomous Agents & The ReAct Loop

Unlike static linear chains where the sequence of steps is hardcoded by the developer,Autonomous Agents use Large Language Models as reasoning engines to dynamically determine which actions to execute, observe intermediate results, and iteratively solve complex multi-step objectives.

1. The ReAct (Reasoning + Acting) Framework

Autonomous agents operate on the ReAct paradigm pioneered by Yao et al. The agent executes a continuous feedback loop:

  1. Thought: The model evaluates the user’s overall goal and formulates an internal plan.
  2. Action: The model selects a specific tool and emits exact parameter values.
  3. Observation: The execution runtime runs the tool and returns the result back to the model.
  4. Evaluation: The model assesses whether the goal is achieved. If not, it loops back to Thought!

2. Constructing an Autonomous Agent with AgentExecutor

Let us build an autonomous agent equipped with live web search and custom financial calculators:

from langchain_openai import ChatOpenAI
from langchain_core.prompts import ChatPromptTemplate, MessagesPlaceholder
from langchain_core.tools import tool
from langchain.agents import create_tool_calling_agent, AgentExecutor
from langchain_community.tools.tavily_search import TavilySearchResults

# 1. Define Tools
@tool
def multiply(a: float, b: float) -> float:
    """Multiplies two floating point numbers together with mathematical precision."""
    return a * b

search = TavilySearchResults(max_results=2)
tools = [search, multiply]

# 2. Agent Prompt Template with intermediate scratchpad placeholder
prompt = ChatPromptTemplate.from_messages([
    ("system", "You are an autonomous research and financial calculation agent. Always verify current facts via search before calculating."),
    ("human", "{input}"),
    MessagesPlaceholder(variable_name="agent_scratchpad")  # Stores Thoughts, Actions & Observations!
])

# 3. Initialize Model and Tool-Calling Agent
llm = ChatOpenAI(model="gpt-4o", temperature=0)
agent = create_tool_calling_agent(llm, tools, prompt)

# 4. AgentExecutor manages the execution loop and error handling
agent_executor = AgentExecutor(
    agent=agent,
    tools=tools,
    verbose=True,           # Prints intermediate Thoughts and Actions in console
    max_iterations=5,       # Prevents infinite execution loops
    handle_parsing_errors=True
)

# 5. Execute Multi-Step Autonomous Goal
query = "What is the current stock price of Apple (AAPL) and how much would 75 shares cost?"
result = agent_executor.invoke({"input": query})

print("Final Agent Answer:")
print(result["output"])

3. Graceful Tool Failure Handling

In production, external APIs frequently timeout or throw HTTP 500 errors. Configure tools with handle_tool_error=True so the agent can catch the error, reflect on the failure, and try an alternative approach instead of crashing the entire application:

@tool(handle_tool_error="Database temporarily unreachable. Try querying the read replica instead.")
def query_primary_db(sql_query: str) -> str:
    """Executes SQL against primary database cluster."""
    raise ConnectionError("Primary cluster connection timeout.")

Explore Related Tools

Hand-picked utilities and calculators related to this tool.

Related Tools