Introduction to AI Agents
What AI Agents are, types of agents, and when to use them
What Are AI Agents?
AI Agents are systems that enable Large Language Models (LLMs) to perform actions by extending their capabilities through tools and knowledge.
Letβs break this down:
Core Components
-
System β An agent is not a single component but a system of many parts:
- Environment: The space where the agent operates (e.g., booking system, codebase, database)
- Sensors: Gather information from the environment (API responses, user input, file system)
- Actuators: Perform actions that change the environment (call APIs, write files, send emails)
-
LLM β The key differentiator from traditional agents: the ability to understand natural language and data. LLMs interpret environmental information and create action plans.
-
Tools β LLMs are βextendedβ with tools: function calling, API calls, database queries, file operations.
-
Memory & Knowledge β Short-term memory (conversation context) and long-term memory (databases, vector stores, external knowledge).
Types of AI Agents
| Agent Type | Description | Example |
|---|---|---|
| Simple Reflex | Acts based on predefined rules | Chatbot that auto-routes complaint emails to support |
| Model-Based Reflex | Acts based on a model of the world and changes | Agent prioritizes routes with price changes based on historical data |
| Goal-Based | Creates plans to achieve specific goals | Agent books a trip by determining necessary transportation |
| Utility-Based | Weighs preferences and trade-offs | Agent optimizes between convenience and cost |
| Learning | Improves over time through feedback | Agent adjusts recommendations based on user ratings |
| Hierarchical | Multiple agents in tiers, higher-level breaks tasks for lower-level | Agent cancels a trip by dividing into subtasks for other agents |
| Multi-Agent Systems | Multiple agents working independently, cooperatively or competitively | Multiple agents booking hotels, flights, entertainment |
When Should You Use AI Agents?
AI Agents work best for:
1. Open-Ended Problems
Problems where the steps canβt be hardcoded into a workflow. The agent needs to determine its own approach.
// Example: Vague user request
const userRequest = "Help me optimize my website's performance";
// Agent needs to:
// 1. Analyze codebase
// 2. Run lighthouse audit
// 3. Identify bottlenecks
// 4. Propose and implement fixes
2. Multi-Step Processes
Complex tasks requiring multiple steps, using tools across multiple turns instead of single-shot retrieval.
3. Improvement Over Time
Tasks where the agent can improve over time by receiving feedback from the environment or users.
LLM vs AI Agent
βββββββββββββββββββ¬ββββββββββββββββββββββββββββ
β Plain LLM β AI Agent β
βββββββββββββββββββΌββββββββββββββββββββββββββββ€
β Input β Output β Input β Plan β Act β Loop β
β Stateless β Has Memory β
β Generate only β Generate + Execute β
β Single turn β Multi-turn autonomous β
βββββββββββββββββββ΄ββββββββββββββββββββββββββββ
Practical Example: Simple Agent with TypeScript
import OpenAI from "openai";
const client = new OpenAI();
// Define tools
const tools: OpenAI.ChatCompletionTool[] = [
{
type: "function",
function: {
name: "get_weather",
description: "Get current weather information",
parameters: {
type: "object",
properties: {
location: { type: "string", description: "City name" }
},
required: ["location"]
}
}
}
];
// Agent loop
async function agent(userMessage: string) {
const messages: OpenAI.ChatCompletionMessageParam[] = [
{ role: "system", content: "You are a weather assistant." },
{ role: "user", content: userMessage }
];
const response = await client.chat.completions.create({
model: "gpt-4o-mini",
messages,
tools,
tool_choice: "auto"
});
// Agent decides whether to call tools
const toolCalls = response.choices[0].message.tool_calls;
if (toolCalls) {
for (const call of toolCalls) {
console.log(`Agent calling tool: ${call.function.name}`);
console.log(`Arguments: ${call.function.arguments}`);
}
}
return response.choices[0].message.content;
}
Summary
- AI Agent = LLM + Tools + Memory + Environment
- Multiple agent types suit different use cases
- Agents work best for open-ended, multi-step tasks that need continuous improvement
- In this series, weβll build agents with TypeScript and Python using the OpenAI API