AI Agents Explained: When LLMs Start Using Tools
The Basics: What Is an AI Agent
A standard LLM is a brain in a jar β it can reason but cannot act.
π― System Scenario & Requirements
System Goal
A standard LLM is a brain in a jar β it can reason but cannot act.
Actors & Components
- Application or API tier
- Core service under design
- Operator or SRE observer
Functional Requirements
- TLDR: A standard LLM is a brain in a jar β it can reason but cannot act.
- An AI Agent connects that brain to tools (web search, code execution, APIs).
- Instead of just answering a question, an agent executes a loop of Thought β Action β Observation until the goal is reached.
Constraints & Assumptions
- Agents introduce new failure modes beyond plain LLMs: Hallucinated tool calls β the model invents arguments or calls a non-existent tool.
- Fix: validate tool schemas strictly; use structured outputs.
β‘ Execution Stages & Decision Trace
Stage 1 β The ReAct Loop: Thought β Action β Observation
The dominant pattern for agents is ReAct (Reasoning + Acting).
The model cycles through three steps until the task is complete: This loop continues until the model decides it has enough information to answer.
Deep Dive
Scenario context
A standard LLM is a brain in a jar β it can reason but cannot act. An AI Agent connects that brain to tools (web search, code execution, APIs). Instead of just answering a question, an agent executes a loop of Thought β Action β Observation until the goal is reached.
An AI agent is a program that uses a large language model as its reasoning engine and connects it to external tools so it can take reβ¦
This walkthrough focuses on The Basics: What Is an AI Agent.
Before making a design choice, clarify:
- An AI agent is a program that uses a large language model as its reasoning engine and connects it to external tools so it can take real actions in the world.
- Where a plain LLM is purely generative β text in, text out β an agent augments the LLM with capabilities like web search, code execution, file reading, database queries, and API calls.
- Three components make up almost every agent: 1.
Concepts in play: llm-agents-and-langgraph, sorting-and-searching-algorithms, llm-prompt-engineering, database-indexing
Stage 1 β The ReAct Loop: Thought β Action β Observation
- The dominant pattern for agents is ReAct (Reasoning + Acting).
- The model cycles through three steps until the task is complete: This loop continues until the model decides it has enough information to answer.
flowchart TD
Start([User Goal]) --> T[Thought: What do I need?]
T --> A[Action: Call a Tool]
A --> O[Observation: Tool Result]
O --> D{Goal reached?}
D -- No --> T
D -- Yes --> Answer([Return Final Answer])
sequenceDiagram
participant U as User
participant L as LLM
participant T as Tool
U->>L: User request
L-->>L: Thought: what to do
L->>T: Action: call tool
T-->>L: Observation: result
L-->>L: Next thought
L-->>U: Final Answer
Observable outcome: The model cycles through three steps until the task is complete: This loop continues until the model decides it has enough information to answer.
Stage 2 β How an Agent Processes a Request
- When a user sends a question to an agent-powered system, several layers of logic activate before any answer is returned.
- Understanding this flow helps you reason about latency, costs, and failure points.
- What happens at each node: - Inject Tool Schemas β before the first LLM call, all tool names, descriptions, and input types are serialised into the system prompt so the model knows what it can use.
- LLM Reasoning β the model outputs a structured "Thought" explaining its next move.
flowchart TD
U([User Request]) --> SYS[Inject Tool Schemas into LLM Context]
SYS --> LLM1[LLM Reasoning: Thought What do I need?]
LLM1 --> TC[Tool Selection: Which tool and args?]
TC --> TE[Tool Execution: Call external function]
TE --> OBS[Observation: Capture tool output]
OBS --> CHK{Goal reached?}
CHK -- No --> LLM1
CHK -- Yes --> ANS([Return Final Answer to User])
Observable outcome: - LLM Reasoning β the model outputs a structured "Thought" explaining its next move.
Stage 3 β Tool Definitions: How an Agent Knows What It Can Do
- A tool is a function the model can call.
- In LangChain you define tools with a name, description, and input schema: The model receives the tool descriptions in its system prompt and decides which to call (and with what arguments) based on the task.
- It never sees the implementation β only the name and docstring.
from langchain.tools import tool
@tool
def search_web(query: str) -> str:
"""Search the web for current information. Use this for recent events or facts."""
return web_search_api(query)
@tool
def run_python(code: str) -> str:
"""Execute Python code and return the output. Use this for calculations."""
return exec_sandbox(code)
flowchart TD
I[Input from User] --> LM[LLM Reasoning]
LM --> D{Need a tool?}
D -- No --> R[Return Answer]
D -- Yes --> TS[Select Tool]
TS --> EX[Execute Tool]
EX --> OB[Observe Output]
OB --> LM
Observable outcome: It never sees the implementation β only the name and docstring.
Stage 4 β Deep Dive: Building a Simple Agent with LangChain
- The verbose=True flag shows you the full Thought/Action/Observation chain β invaluable for debugging.
from langchain.agents import create_react_agent, AgentExecutor
from langchain_openai import ChatOpenAI
from langchain import hub
llm = ChatOpenAI(model="gpt-4o-mini")
tools = [search_web, run_python]
prompt = hub.pull("hwchase17/react") # standard ReAct prompt
agent = create_react_agent(llm, tools, prompt)
executor = AgentExecutor(agent=agent, tools=tools, verbose=True)
result = executor.invoke({"input": "Who was US president when Titanic was released?"})
print(result["output"])
Observable outcome: The verbose=True flag shows you the full Thought/Action/Observation chain β invaluable for debugging.
Stage 5 β Real-World Applications: Real-World Agent Use Cases
Observable outcome: You can explain how βReal-World Applications: Real-World Agent Use Casesβ changes the architecture or implementation path.
Stage 6 β Practical: Debugging Agent Behavior
- Agents fail in opaque ways that are hard to catch without systematic debugging.
- Here is a practical workflow for diagnosing misbehaving agents.
- Step 1 β Enable verbose output.
- In LangChain, set verbose=True on the AgentExecutor .
executor = AgentExecutor(agent=agent, tools=tools, verbose=True, max_iterations=10)
Observable outcome: In LangChain, set verbose=True on the AgentExecutor .
End-to-end outcome
An AI agent is a program that uses a large language model as its reasoning engine and connects it to external tools so it can take real actions in the world.
Alternatives considered
Brain in a Jar vs Brain with Arms
Give it "What is the weather in Tokyo today?" and it will: - Answer from training data (which is months or years old).
Decision Guide: When to Use an AI Agent
Use an agent when the task requires multiple steps, tool use, or dynamic decision-making that can't be scripted upfront.
Failure and degraded modes
Trade-offs & Failure Modes: When Agents Fail: Hallucinations, Loops, and Cost Blowouts
- Detection: Agents introduce new failure modes beyond plain LLMs: Hallucinated tool calls β the model invents arguments or calls a non-existent tool.
- User or operator impact: Agents introduce new failure modes beyond plain LLMs: Hallucinated tool calls β the model invents arguments or calls a non-existent tool.
