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 TypeDescriptionExample
Simple ReflexActs based on predefined rulesChatbot that auto-routes complaint emails to support
Model-Based ReflexActs based on a model of the world and changesAgent prioritizes routes with price changes based on historical data
Goal-BasedCreates plans to achieve specific goalsAgent books a trip by determining necessary transportation
Utility-BasedWeighs preferences and trade-offsAgent optimizes between convenience and cost
LearningImproves over time through feedbackAgent adjusts recommendations based on user ratings
HierarchicalMultiple agents in tiers, higher-level breaks tasks for lower-levelAgent cancels a trip by dividing into subtasks for other agents
Multi-Agent SystemsMultiple agents working independently, cooperatively or competitivelyMultiple 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