AI Agents Explained: How Intelligent AI Systems Think, Plan, and Act
Artificial intelligence has evolved beyond systems that simply generate text.
Modern AI applications can do more than answer questions. They can plan tasks, use tools, retrieve information, execute actions, observe results, and continue working toward a goal.
These systems are commonly called AI agents or AI agent systems.
For example, a traditional chatbot might answer:
User:
What is the weather in Colombo?
An AI agent could:
User
↓
Agent understands request
↓
Calls weather API
↓
Receives current weather
↓
Analyzes result
↓
Generates response
The important difference is that the agent can interact with external systems instead of relying only on information already present in its model.
In this article, we will explore:
What AI agents are
How AI agents work
LLMs vs AI agents
Agent architecture
Planning
Tools and function calling
Memory
Agent loops
ReAct-style reasoning
Python implementation
Building a simple AI agent
RAG agents
Web-search agents
Coding agents
Multi-agent systems
Agent safety
Common mistakes
Real-world applications
What Is an AI Agent?
An AI agent is a software system that uses an AI model to pursue a goal by deciding what actions to take and interacting with tools or external systems.
A simplified representation is:
Goal
↓
AI Model
↓
Decision
↓
Tool / Action
↓
Observation
↓
Decision
↓
...
↓
Final Result
Unlike a simple prompt-response application, an agent can operate through multiple steps.
For example:
Goal:
Find the cheapest flight from Colombo to Tokyo.
An agent might:
1. Understand the request
2. Search available flights
3. Compare prices
4. Filter according to requirements
5. Check dates
6. Present the best options
The exact behavior depends on the tools and instructions provided to the agent.
AI Agent vs Chatbot
A basic chatbot usually follows:
User
↓
Prompt
↓
LLM
↓
Response
An AI agent can follow:
User
↓
Goal
↓
LLM
↓
Choose Action
↓
Tool
↓
Observation
↓
LLM
↓
Choose Next Action
↓
Tool
↓
Observation
↓
Final Response
The agent therefore has an action loop.
AI Agent vs LLM
An LLM is primarily a model that processes and generates language.
For example:
Prompt
↓
LLM
↓
Text
An AI agent is a system built around an AI model.
AI Agent
|
┌───────────┼───────────┐
↓ ↓ ↓
LLM Tools Memory
|
↓
Planning
|
↓
Actions
The LLM may serve as the agent's decision-making component, while other software handles tools, state, permissions, and execution.
The Core Components of an AI Agent
A practical AI agent often contains several components.
1. AI Model
The model interprets instructions and decides what to do.
Examples include large language models and specialized reasoning models.
2. Tools
Tools allow the agent to interact with external systems.
Examples:
Web Search
Calculator
Database
API
File System
Email
Calendar
Code Execution
3. Memory
Memory allows an agent to maintain useful information across steps or sessions.
4. Planning
The agent may break a complex objective into smaller actions.
5. State
State tracks what has already happened.
6. Guardrails
Guardrails control what the agent is allowed to do.
Basic AI Agent Architecture
A simplified architecture looks like:
USER GOAL
↓
┌───────────┐
│ AI MODEL │
└─────┬─────┘
↓
Decide Action
↓
┌────────────┐
│ TOOLS │
└─────┬──────┘
↓
Observation
↓
┌───────────┐
│ AI MODEL │
└─────┬─────┘
↓
Decide Again
↓
...
↓
Final Answer
The model repeatedly receives information and determines the next step.
What Are Tools?
Tools are external functions or services that an agent can call.
For example:
def calculator(expression):
return eval(expression)
An agent could use the calculator when it needs to perform a calculation.
Other tools might look like:
def search_web(query):
...
def get_weather(city):
...
def query_database(sql):
...
def send_email(to, subject, body):
...
The agent chooses which tool is appropriate for the current task.
Why Tools Are Important
An LLM does not automatically have access to every external system.
For example, if a user asks:
What is the current temperature in Colombo?
The model needs access to a weather service to obtain live information.
The architecture becomes:
Question
↓
AI Agent
↓
Weather Tool
↓
Weather API
↓
Current Data
↓
AI Agent
↓
Response
Tools give agents the ability to interact with the real world.
Function Calling
Modern AI systems can use structured tool or function calls.
Instead of generating:
Please call the weather API for Colombo.
the model can produce structured information such as:
{
"tool": "get_weather",
"arguments": {
"city": "Colombo"
}
}
The application executes the function and sends the result back to the model.
Conceptually:
LLM
↓
Tool Call
↓
Application
↓
Tool
↓
Result
↓
LLM
This makes tool usage more reliable than asking a model to invent function syntax in plain text.
A Simple Tool in Python
Let's create a calculator tool.
def calculator(a, b, operation):
if operation == "add":
return a + b
if operation == "subtract":
return a - b
if operation == "multiply":
return a * b
if operation == "divide":
if b == 0:
raise ValueError(
"Cannot divide by zero"
)
return a / b
raise ValueError(
"Unknown operation"
)
The agent can select the tool based on the user's request.
For example:
User:
What is 25 × 8?
The agent decides:
Tool:
calculator
Arguments:
a = 25
b = 8
operation = multiply
The tool returns:
200
The agent then generates the final response.
The Agent Loop
One of the most important concepts in AI agents is the agent loop.
A simplified loop is:
while task_not_finished:
observe()
decide()
act()
receive_result()
In more detail:
1. Receive goal
2. Analyze current state
3. Select an action
4. Execute action
5. Observe result
6. Update state
7. Decide next action
8. Repeat
9. Finish
This is what allows an agent to perform multi-step tasks.
Example Agent Loop
Suppose the goal is:
Find information about Python web frameworks.
The agent might perform:
Goal
↓
Search web
↓
Results
↓
Analyze results
↓
Search for FastAPI
↓
Results
↓
Search for Django
↓
Results
↓
Compare information
↓
Generate answer
The important point is that the agent decides what to do next based on previous observations.
Planning
Complex tasks can be divided into smaller steps.
Suppose the user says:
Create a report about three programming languages.
An agent may plan:
1. Identify the languages
2. Research each language
3. Collect important information
4. Compare them
5. Generate the report
Planning can happen explicitly or implicitly.
Explicit Planning
An agent can create a plan before execution:
plan = [
"Research Python",
"Research JavaScript",
"Research Java",
"Compare the languages",
"Generate report"
]
Then execute each step.
Plan
↓
Step 1
↓
Step 2
↓
Step 3
↓
Step 4
↓
Step 5
Dynamic Planning
More advanced agents can change the plan based on observations.
For example:
Initial Plan
↓
Search
↓
Information Missing
↓
Modify Plan
↓
Additional Search
↓
Continue
This is useful when the agent cannot know all required steps in advance.
ReAct-Style Agents
A popular agent pattern is often described as Reason + Act.
Conceptually:
Thought / Decision
↓
Action
↓
Observation
↓
Thought / Decision
↓
Action
↓
Observation
↓
Final Answer
For example:
Goal:
Find the population of a city.
The agent might conceptually do:
Decision:
I need current population information.
Action:
Search population data.
Observation:
Search results returned.
Decision:
I found several sources.
Action:
Compare the sources.
Observation:
Relevant data found.
Final:
Return the result.
In production systems, internal reasoning should not be exposed as raw hidden chain-of-thought. Applications should instead log concise action traces, tool calls, and results where appropriate.
Memory in AI Agents
Memory allows an agent to retain useful information.
There are different forms of memory.
Short-Term Memory
Information from the current conversation or task.
Example:
User:
My name is Alex.
User:
What is my name?
The agent can use the conversation context.
Long-Term Memory
Information stored for future sessions.
For example:
User preferences
Previous tasks
Saved documents
Application state
This usually requires external storage.
Agent Memory Architecture
A simple memory architecture can look like:
AI Agent
|
┌──────────┴──────────┐
↓ ↓
Short-Term Memory Long-Term Memory
↓ ↓
Conversation Database
Context Vector Store
The agent retrieves relevant memories when necessary.
Memory With a Database
For example, an application might store:
CREATE TABLE memories (
id SERIAL PRIMARY KEY,
user_id INTEGER,
content TEXT,
created_at TIMESTAMP
);
The agent can retrieve memories associated with a user.
For semantic memory, embeddings can also be used:
Memory
↓
Embedding
↓
Vector Database
↓
Similarity Search
This allows an agent to retrieve memories based on meaning.
AI Agents and RAG
AI agents can use Retrieval-Augmented Generation.
A basic RAG system is:
Question
↓
Embedding
↓
Vector Search
↓
Relevant Documents
↓
LLM
↓
Answer
An agentic RAG system can be more dynamic:
Question
↓
Agent
↓
Decide whether retrieval is needed
↓
Search Knowledge Base
↓
Evaluate Results
↓
Search Again if Necessary
↓
Generate Answer
The agent controls the retrieval process.
Example RAG Agent
Suppose a user asks:
What is our company's refund policy for damaged products?
The agent may decide:
I need company policy information.
Then:
Search Knowledge Base
↓
Retrieve Refund Policy
↓
Inspect Relevant Section
↓
Answer User
If the first search does not provide enough information, the agent can perform another search.
Web Search Agents
An agent can use a web-search tool.
Architecture:
User Question
↓
AI Agent
↓
Need Current Information?
↓
Web Search
↓
Search Results
↓
Analyze Results
↓
Additional Search
↓
Final Response
This is useful for tasks involving:
Current events
Product research
Documentation
Market research
Travel planning
Technical research
Coding Agents
Coding agents are another important application.
A coding agent can potentially:
Read source code
↓
Understand task
↓
Inspect project
↓
Modify files
↓
Run tests
↓
Analyze errors
↓
Modify code
↓
Run tests again
↓
Finish
The agent therefore interacts with a software development environment.
Example Coding Agent Loop
Suppose the task is:
Fix the login bug.
The agent might:
1. Inspect project files
2. Locate authentication code
3. Read relevant files
4. Identify possible issue
5. Modify code
6. Run tests
7. Receive failure
8. Analyze failure
9. Modify code again
10. Run tests
11. Return completed change
This is a powerful example of tool-using AI.
AI Agent With a File Tool
A simple file-reading tool might look like:
def read_file(path):
with open(
path,
"r",
encoding="utf-8"
) as file:
return file.read()
A write tool could be:
def write_file(path, content):
with open(
path,
"w",
encoding="utf-8"
) as file:
file.write(content)
return "File written successfully."
A real coding agent needs strict permissions around these tools.
Tool Selection
An agent may have several tools:
Tools:
1. calculator
2. web_search
3. database_search
4. read_file
5. write_file
The model needs to determine which tool is appropriate.
For example:
Question:
What is 120 × 25?
→ calculator
Question:
Find the latest Python documentation.
→ web_search
Question:
What is in config.json?
→ read_file
Tool selection is one of the key capabilities of an agent.
Tool Descriptions
Agents need clear descriptions of available tools.
For example:
tools = [
{
"name": "calculator",
"description": (
"Perform arithmetic calculations."
)
},
{
"name": "search",
"description": (
"Search the web for information."
)
}
]
The descriptions help the model determine when a tool should be used.
A Simple Rule-Based Agent
Before using an LLM, we can understand the architecture with a simple Python agent.
def calculator(a, b):
return a + b
def agent(user_input):
if "add" in user_input.lower():
numbers = [
int(x)
for x in user_input.split()
if x.isdigit()
]
if len(numbers) >= 2:
result = calculator(
numbers[0],
numbers[1]
)
return f"Result: {result}"
return "I don't know how to handle this task."
print(
agent("add 20 30")
)
This is not a modern LLM agent, but it demonstrates the basic concept:
Input
↓
Decision
↓
Tool
↓
Result
Building an LLM Agent
A modern LLM agent typically adds a model to the decision process.
Conceptually:
def agent(user_input):
response = llm(
user_input
)
if response.requests_tool:
result = execute_tool(
response.tool_name,
response.arguments
)
return llm(
user_input,
result
)
return response.text
The actual implementation depends on the AI provider or framework being used.
Agent State
State stores information about the current task.
For example:
state = {
"goal": "Research Python frameworks",
"completed_steps": [],
"search_results": [],
"current_step": None
}
After a tool call:
state["completed_steps"].append(
"Searched Python frameworks"
)
State is especially important for long-running agents.
Agent State Machine
An agent can also be represented as states:
START
↓
PLAN
↓
EXECUTE
↓
OBSERVE
↓
EVALUATE
↓
┌───────────────┐
│ Task complete?│
└───────┬───────┘
No │ Yes
│
↓
PLAN
│
└──────→ FINISH
This makes agent workflows easier to understand and control.
Multi-Agent Systems
Instead of one agent doing everything, multiple specialized agents can collaborate.
For example:
Main Agent
|
┌────────────┼────────────┐
↓ ↓ ↓
Research Agent Coding Agent Review Agent
↓ ↓ ↓
Research Code Review
└────────────┼────────────┘
↓
Final Result
Each agent has a specific role.
Example Multi-Agent Workflow
Suppose the goal is:
Create a technical article about AI agents.
The system could use:
Research Agent
↓
Collect information
Writer Agent
↓
Create article
Reviewer Agent
↓
Check technical accuracy
Editor Agent
↓
Improve final article
This can be useful for complex workflows, although multiple agents also increase cost and complexity.
Agent Communication
Agents can communicate through structured messages.
For example:
message = {
"from": "research_agent",
"to": "writer_agent",
"type": "research_result",
"content": "AI agents can use tools..."
}
This is safer and easier to process than relying entirely on unstructured text.
Agent Safety
Giving an AI agent tools creates additional security risks.
Consider an agent with access to:
Email
Database
File System
Payments
Cloud Infrastructure
An incorrect decision could have real consequences.
Therefore, tool permissions should be carefully controlled.
Principle of Least Privilege
An agent should only receive the permissions it actually needs.
For example:
Research Agent
✓ Web search
✓ Read documents
✗ Delete files
✗ Send payments
A coding agent might have:
✓ Read project files
✓ Modify project files
✓ Run tests
✗ Access production database
unless that access is explicitly required and protected.
Human Approval
High-risk actions can require human confirmation.
For example:
Agent wants to:
Delete production database
↓
Human Approval Required
↓
Approve / Reject
This is especially important for:
Financial transactions
Sending emails
Deleting data
Production deployments
Account changes
Security-sensitive operations
Tool Validation
Never blindly trust arguments generated by an AI model.
For example, if an agent calls:
{
"amount": 1000000
}
the application should validate the value before performing the action.
Use application-level checks:
def transfer_money(amount):
if amount <= 0:
raise ValueError(
"Invalid amount"
)
if amount > 10000:
raise PermissionError(
"Manual approval required"
)
# Execute transfer
The tool itself should enforce safety rules.
Agent Loops Need Limits
An agent can sometimes get stuck.
For example:
Search
↓
Search again
↓
Search again
↓
Search again
↓
...
Therefore, production agents should have limits.
For example:
MAX_STEPS = 10
for step in range(MAX_STEPS):
result = agent_step()
if result.is_complete:
break
Other limits can include:
Maximum tool calls
Maximum execution time
Maximum tokens
Maximum cost
Maximum retries
Error Handling
Tools can fail.
For example:
Agent
↓
Weather API
↓
Timeout
The agent should handle this gracefully.
try:
result = get_weather(
"Colombo"
)
except TimeoutError:
result = {
"error": "Weather service unavailable"
}
The agent can then decide whether to retry or provide a fallback response.
Retry Strategies
Transient errors can sometimes be retried.
for attempt in range(3):
try:
result = call_api()
break
except Exception:
if attempt == 2:
raise
However, retries should not be used blindly for actions that may have already succeeded.
For example, retrying a payment operation can potentially create duplicate transactions if the first request succeeded but the response was lost.
Observability
AI agents are difficult to debug if you cannot see what they are doing.
Production systems should log useful events such as:
Agent started
Tool selected
Tool arguments
Tool result
Execution time
Error
Retry
Final result
For example:
[10:20:01] Agent started
[10:20:02] Tool: search
[10:20:03] Search returned 8 results
[10:20:04] Tool: database
[10:20:05] Database query completed
[10:20:06] Agent finished
Do not log secrets, passwords, private credentials, or unnecessary personal information.
Cost Management
Agent systems can make multiple model calls.
A simple chatbot might use:
1 LLM request
An agent might use:
LLM request
↓
Tool
↓
LLM request
↓
Tool
↓
LLM request
↓
Final response
Complex tasks can therefore consume significantly more compute and API usage.
Useful controls include:
Maximum steps
Smaller models for simple tasks
Caching
Tool-result caching
Batching
Context reduction
Early stopping
When Should You Use an AI Agent?
AI agents are useful when a task requires:
Multiple steps
+
Decision making
+
External tools
+
Dynamic execution
Good examples include:
Research assistants
Coding assistants
Customer-support automation
Document analysis
Data-analysis agents
Scheduling assistants
Workflow automation
Knowledge assistants
IT support
Business process automation
When Should You NOT Use an AI Agent?
Not every AI application needs an agent.
If the task is:
User
↓
Prompt
↓
LLM
↓
Answer
a normal LLM application may be simpler.
For example:
Summarize this paragraph.
does not necessarily require an agent.
Adding an agent when it provides no useful capability can increase:
Complexity
Cost
Latency
Failure possibilities
Use agents when dynamic action and tool usage actually provide value.
AI Agent vs Workflow
There is an important distinction between an agent and a fixed workflow.
A workflow might be:
Step 1
↓
Step 2
↓
Step 3
↓
Step 4
The steps are predetermined.
An agent may be:
Goal
↓
Decide
↓
Action
↓
Observe
↓
Decide again
The next step can change based on what happened.
For predictable business processes, deterministic workflows are often preferable.
For tasks requiring flexible decision-making, agents may be useful.
Agentic Workflow
A practical architecture can combine both approaches.
For example:
Fixed Workflow
↓
Agent
↓
Tool Selection
↓
Fixed Validation
↓
Agent
↓
Human Approval
↓
Final Action
This approach provides flexibility while keeping important operations deterministic.
Complete Conceptual Agent Example
Consider an AI research assistant.
User:
Research the latest developments in
Python web frameworks and summarize them.
The agent could execute:
1. Understand the goal
2. Search for current information
3. Collect relevant sources
4. Identify important frameworks
5. Compare information
6. Search for missing details
7. Organize findings
8. Generate summary
Architecture:
USER
↓
RESEARCH GOAL
↓
AI AGENT
↓
┌───────┴────────┐
↓ ↓
Web Search Knowledge Base
↓ ↓
└───────┬────────┘
↓
Collected Data
↓
Analysis
↓
Final Answer
Future of AI Agents
AI agents are moving toward systems that can operate across multiple applications and tools.
Potential capabilities include:
Understand Goal
↓
Create Plan
↓
Use Multiple Tools
↓
Monitor Results
↓
Adapt Plan
↓
Complete Task
Instead of asking AI to simply generate content, developers can build systems that perform useful actions.
However, increasing autonomy also increases the importance of:
Security
Permissions
Monitoring
Evaluation
Human Oversight
Reliability
Final Summary
AI agents are systems that combine AI models with tools, memory, state, and decision-making logic to accomplish multi-step tasks.
The fundamental architecture is:
Goal
↓
AI Model
↓
Decision
↓
Tool
↓
Observation
↓
Decision
↓
Tool
↓
Observation
↓
Final Result
The most important concepts are:
LLM
↓
Provides language understanding and generation
Tools
↓
Allow interaction with external systems
Memory
↓
Stores useful information
Planning
↓
Breaks complex goals into actions
State
↓
Tracks task progress
Agent Loop
↓
Decide → Act → Observe → Repeat
RAG
↓
Retrieval + AI generation
Multi-Agent System
↓
Multiple specialized agents working together
Guardrails
↓
Limit unsafe or unauthorized actions
AI agents represent a shift from AI that only responds to AI systems that can interact with tools and complete multi-step tasks.
The most effective agent systems are not necessarily the ones with the most autonomy. Good agent design focuses on giving the AI the right tools, limited permissions, clear objectives, reliable state management, strong validation, and appropriate human oversight.
