Artificial Intelligence

Foundations Of AI

The Agent Framework

An agent is anything that perceives its environment through sensors and acts upon that environment through actuators — this is the same core structure Chapter 1

JrCodex·9 min read

Jr Codex AI Notes

Level: Beginner–Intermediate Prerequisites: Chapter 3 Time to complete: ~25 minutes


Table of Contents

  1. What is an Agent?
  2. The Agent Function vs the Agent Program
  3. The PEAS Framework
  4. Types of Environments
  5. Rationality — What Makes an Agent "Good"?
  6. A Minimal Agent in Code
  7. Summary & Next Steps

1. What is an Agent?

An agent is anything that perceives its environment through sensors and acts upon that environment through actuators — this is the same core structure Chapter 1 used to define AI itself, now made precise.

The Agent Loop
─────────────────────────────────────────
  Environment
      │
      ▼ (percepts)
   [SENSORS]
      │
      ▼
   [AGENT]  ← internal state, knowledge, decision logic
      │
      ▼
  [ACTUATORS]
      │
      ▼ (actions)
  Environment (changed)
─────────────────────────────────────────
Agent Examples
─────────────────────────────────────────
  Human:            sensors = eyes, ears, skin
                      actuators = hands, legs, mouth

  Chess AI:            sensors = board state (read)
                          actuators = move selection

  Self-driving car:       sensors = cameras, lidar, GPS
                             actuators = steering, brake, gas

  Spam filter:               sensors = email content
                                actuators = label (spam/not spam)
─────────────────────────────────────────

Every module from here forward — search (Module 2), planning (Module 3), probabilistic reasoning (Module 4), and multi-agent systems (Module 5) — describes different ways an agent can be built to decide what action to take, given what it perceives.


2. The Agent Function vs the Agent Program

A subtle but important distinction for thinking clearly about agents:

Agent Function (mathematical concept)
─────────────────────────────────────────
  f: Percept sequences → Actions

  A purely abstract MAPPING from every possible history of
  percepts to an action. It doesn't specify HOW the mapping
  is computed — just that, in principle, it exists.
─────────────────────────────────────────

Agent Program (practical implementation)
─────────────────────────────────────────
  The actual running code that implements (an approximation of)
  the agent function — using search, rules, learned models, or
  any other technique.
─────────────────────────────────────────
# The AGENT FUNCTION for a thermostat could be described as a table:
#   percept "too cold" → action "turn on heater"
#   percept "too hot"  → action "turn on AC"
#   percept "just right" → action "do nothing"
 
# The AGENT PROGRAM is the actual code implementing that mapping:
def thermostat_agent(percept: str) -> str:
    rules = {
        "too_cold": "turn_on_heater",
        "too_hot": "turn_on_AC",
        "just_right": "do_nothing",
    }
    return rules.get(percept, "do_nothing")

This distinction matters because it separates what an agent should ideally do (the function — often impossible to write out explicitly for anything complex) from how we actually build something that approximates it (the program — using the techniques across this curriculum).


3. The PEAS Framework

Before designing any agent, PEASPerformance measure, Environment, Actuators, Sensors — gives a structured way to specify exactly what it needs to do.

Filling in PEAS — Four Questions
─────────────────────────────────────────
  1. Performance measure  → How do we evaluate SUCCESS?
  2. Environment            → What does the agent operate WITHIN?
  3. Actuators                → What can the agent DO?
  4. Sensors                    → What can the agent PERCEIVE?
─────────────────────────────────────────
AgentPerformance MeasureEnvironmentActuatorsSensors
Self-Driving CarSafety, speed, legality, comfortRoads, traffic, pedestriansSteering, throttle, brakesCameras, radar, lidar, GPS
Medical Diagnosis AIDiagnostic accuracy, patient outcomesPatient records, lab resultsDiagnosis report, treatment planSymptom input, test results
Chess AIWin/draw/loss, ratingChess boardPiece movementsBoard state
Email Spam FilterPrecision/recall, user complaintsEmail inboxLabel (spam/not), deleteEmail headers, body text
from dataclasses import dataclass, field
from typing import List
 
@dataclass
class PEASSpecification:
    """A formal PEAS specification for an AI agent."""
    agent_name: str
    performance_measures: List[str]
    environment: List[str]
    actuators: List[str]
    sensors: List[str]
 
    def display(self):
        print(f"\n{'='*50}\n  PEAS: {self.agent_name}\n{'='*50}")
        print(f"  Performance : {', '.join(self.performance_measures)}")
        print(f"  Environment : {', '.join(self.environment)}")
        print(f"  Actuators   : {', '.join(self.actuators)}")
        print(f"  Sensors     : {', '.join(self.sensors)}")
 
delivery_robot = PEASSpecification(
    agent_name="Warehouse Delivery Robot",
    performance_measures=["Delivery speed", "Items not damaged", "Battery efficiency"],
    environment=["Warehouse floor", "Shelves", "Other robots", "Human workers"],
    actuators=["Wheels/motors", "Robotic arm", "Status lights"],
    sensors=["Lidar", "Camera", "Battery meter", "Package weight sensor"],
)
delivery_robot.display()

Practical value of PEAS: working through these four questions before writing any code forces clarity about what "success" actually means and what the agent can realistically perceive/do — skipping this step is a common source of poorly-scoped AI projects (a theme revisited in Module 7's capstone).


4. Types of Environments

The kind of environment an agent operates in directly determines which techniques (from later modules) are appropriate.

PropertyQuestionExample
Fully vs Partially ObservableCan the agent see the ENTIRE relevant state?Chess (full) vs Poker (partial — opponent's hand is hidden)
Deterministic vs StochasticAre outcomes of actions predictable?Chess (deterministic) vs self-driving (stochastic — other drivers are unpredictable)
Episodic vs SequentialDoes the CURRENT decision depend on past ones?Spam filter (episodic — each email independent) vs Chess (sequential — every move affects the future)
Static vs DynamicDoes the environment change WHILE the agent is deciding?A crossword puzzle (static) vs stock trading (dynamic)
Discrete vs ContinuousIs there a finite or infinite number of distinct states/actions?Chess (discrete) vs steering a car (continuous)
Single vs Multi-agentIs the agent alone, or do other agents' actions matter?Solving a puzzle alone (single) vs Chess (multi, competitive)
Environment Classification Examples
─────────────────────────────────────────────────────────
Task              Obs.   Determ.  Episodic  Static  Discrete  Agents
────────────────  ─────  ───────  ────────  ──────  ────────  ──────
Chess              Full   Det.     Seq.      Static   Disc.    Multi
Poker                Part.  Stoc.    Seq.      Static   Disc.    Multi
Self-driving car       Part.  Stoc.    Seq.      Dyn.     Cont.    Multi
Spam filter              Full   Det.     Epis.     Static   Disc.    Single
─────────────────────────────────────────────────────────

Key insight: the hardest environments are partially observable, stochastic, sequential, dynamic, continuous, and multi-agent — which describes exactly the messy, real physical world. Much of AI's technical difficulty comes from environments sitting toward this "hard" end of every dimension at once.


5. Rationality — What Makes an Agent "Good"?

A rational agent is one that, given its percepts and knowledge, selects the action expected to best achieve its performance measure — this is a precise, technical definition of "good," distinct from "correct" or "human-like."

Rationality ≠ Perfection or Omniscience
─────────────────────────────────────────
  A rational agent is NOT expected to know everything or
  never make mistakes — it's expected to make the BEST
  decision given:
    1. Its performance measure (what defines success)
    2. Its prior knowledge of the environment
    3. The actions actually available to it
    4. Its percept sequence so far (what it has observed)
─────────────────────────────────────────
# A rational agent can still make what LOOKS like a "wrong" choice in
# hindsight, if the outcome was genuinely unpredictable given what
# it knew AT THE TIME the decision was made.
 
def rational_umbrella_decision(forecast_rain_probability: float) -> str:
    """A simple rational agent: decide based on available information, not the outcome."""
    # If it's rational to bring an umbrella whenever rain probability > 30%...
    if forecast_rain_probability > 0.30:
        return "bring umbrella"
    return "leave umbrella"
 
# Even if it DOESN'T rain today, bringing the umbrella at 70% forecasted
# probability was still the RATIONAL choice given the information available —
# rationality is about the DECISION PROCESS, not guaranteed good outcomes.
print(rational_umbrella_decision(0.70))      # "bring umbrella" — rational, regardless of today's actual weather

This distinction — judging an agent's rationality by its decision process given available information, not by whether things happened to work out — becomes especially important in Module 4's discussion of decision-making under uncertainty.


6. A Minimal Agent in Code

Bringing Sections 1-5 together into a single, complete (if simple) working agent:

class VacuumCleanerAgent:
    """
    A classic AI textbook example: an agent cleaning a two-room environment.
    PEAS:
      Performance: amount of dirt cleaned, minimal movement
      Environment: two rooms (A and B), each either clean or dirty
      Actuators: move left, move right, suck
      Sensors: current room, whether it's dirty
    """
    def __init__(self):
        self.location = "A"
 
    def perceive_and_act(self, room_status: dict) -> str:
        """room_status example: {"A": "dirty", "B": "clean"}"""
        if room_status[self.location] == "dirty":
            return "suck"
        elif self.location == "A":
            self.location = "B"
            return "move_right"
        else:
            self.location = "A"
            return "move_left"
 
 
agent = VacuumCleanerAgent()
world = {"A": "dirty", "B": "dirty"}
 
for step in range(4):
    action = agent.perceive_and_act(world)
    print(f"Step {step+1}: location={agent.location}, action={action}")
    if action == "suck":
        world[agent.location] = "clean"

This tiny agent already demonstrates the full loop from Section 1 (perceive → decide → act) and satisfies a PEAS specification (Section 3) — every more sophisticated technique in this curriculum is, at its core, a more powerful way to implement this same perceive_and_act decision logic.


7. Summary & Next Steps

Key Takeaways

  • An agent perceives its environment via sensors and acts via actuators — the universal structure underlying every AI system in this curriculum.
  • The agent function is the abstract, ideal mapping from percepts to actions; the agent program is the actual code approximating it.
  • PEAS (Performance, Environment, Actuators, Sensors) is the standard framework for precisely specifying what an agent needs to do before building it.
  • Environments vary along several axes (observable/stochastic/episodic/static/discrete/multi-agent) — the hardest real-world problems sit at the difficult end of nearly every axis simultaneously.
  • Rationality means making the best decision given available information and goals — not guaranteed perfect outcomes, and not necessarily human-like reasoning.

Concept Check

  1. What's the difference between an agent function and an agent program?
  2. Using PEAS, how would you specify a warehouse inventory-scanning robot?
  3. Why can a rational agent still end up with a bad outcome, and why doesn't that make its decision irrational?

Next Chapter

Chapter 5: The AI/ML/DL/NLP Landscape


Jr Codex — 1-on-1 Personalized Coaching | Back to Module Index