DEV Community

Abdeljabbar Elassali
Abdeljabbar Elassali

Posted on

intelligent agent in artificial intelligence examples: 5 Patterns You Can Code Yourself

Forget the textbook definitions for a minute. Every intelligent agent ever built is the same loop:

while True:
    perception = sensors.read()   # perceive
    action = decide(perception)   # decide
    actuators.execute(action)     # act
Enter fullscreen mode Exit fullscreen mode

That is it. Thermostats, self-driving cars, and AI coding agents all run this loop. The only thing that changes is what goes inside decide(). The five classic agent types are just five ways to write that function, from a five-line if statement to systems that rewrite their own behavior.

This post gives you all five as code you can read, run, and modify, with the real-world examples behind each pattern.

Pattern 1: Simple reflex agent

The simplest possible decide(): a set of condition-action rules applied to the current perception. No memory, no planning.

def decide(temperature):
    if temperature < 20:
        return "heater_on"
    if temperature > 24:
        return "heater_off"
    return "do_nothing"
Enter fullscreen mode Exit fullscreen mode

Real-world examples: a thermostat, an automatic supermarket door (motion detected means open, nothing detected means close).

When it breaks: the moment the current sensor reading is not enough. A thermostat that cannot remember it just turned the heater on will short-cycle itself to death. Reflex agents are fast and predictable, and they fail in exactly the situations where history matters.

Pattern 2: Model-based reflex agent

Same rules, but now the agent keeps an internal model of the world and updates it with every perception. It can act on things it cannot currently see.

class ModelBasedAgent:
    def __init__(self):
        self.world_model = {"door": "closed"}

    def decide(self, perception):
        # update the model first, then apply rules to the model
        if perception.get("door_sensor"):
            self.world_model["door"] = perception["door_sensor"]

        if perception.get("motion") and self.world_model["door"] == "closed":
            return "open_door"
        return "do_nothing"
Enter fullscreen mode Exit fullscreen mode

Real-world examples: a robot vacuum that maps your home on the first run and navigates the map afterward instead of bumping around blindly. A self-driving car that remembers the pedestrian behind the parked truck even when its cameras lose sight of them.

When it breaks: when the model drifts from reality. Stale maps and wrong beliefs produce confident, wrong actions. The maintenance of the model is the whole engineering problem.

Pattern 3: Goal-based agent

Now the agent knows where it wants to end up and searches for a sequence of actions that gets there.

def decide(start, goal, graph):
    path = search(start, goal, graph)  # BFS, A*, Dijkstra, your pick
    if not path:
        return "no_route"
    return f"move_to_{path[1]}"  # take the first step
Enter fullscreen mode Exit fullscreen mode

Real-world examples: a GPS route planner computing the full route before you move, then replanning when traffic changes. A chess engine searching millions of move sequences to find the line that leads to checkmate.

The tradeoff: flexibility costs compute. The same search machinery works for any goal, but planning is expensive, which is why goal-based agents pair the planner with heuristics and time limits in practice.

Pattern 4: Utility-based agent

Goal-based agents ask "does this reach the goal?" Utility-based agents ask "how good is each outcome?" and pick the action with the highest expected utility.

def decide(actions, state):
    scored = [(utility(state, action), action) for action in actions]
    return max(scored)[1]

def utility(state, action):
    # example: expected return minus risk penalty
    return expected_return(state, action) - risk_penalty(state, action)
Enter fullscreen mode Exit fullscreen mode

Real-world examples: an algorithmic trading bot balancing expected return against risk, fees, and timing instead of blindly chasing profit. A recommendation engine optimizing long-term satisfaction rather than raw clicks.

The catch: someone has to write the utility function, and a bad one produces confidently wrong behavior. A feed optimized purely for outrage is a utility-based agent doing exactly what it was told.

Pattern 5: Learning agent

The final pattern adds a feedback loop: the agent measures how well its actions worked and updates its own decision-making over time.

class LearningAgent:
    def __init__(self):
        self.policy = initial_policy()

    def decide(self, perception):
        action = self.policy(perception)
        reward = self.critic(perception, action)
        self.policy = self.update_policy(self.policy, perception, action, reward)
        return action
Enter fullscreen mode Exit fullscreen mode

Real-world examples: a spam filter that starts with generic rules and gets tuned to your inbox every time you mark something as spam or rescue it from junk. A voice assistant that stops asking questions you have already answered a hundred times.

The catch: learning needs feedback and time. A learning agent with no signal to learn from is just a complicated reflex agent.

How modern AI agents stack all five

Look at an AI coding agent like Claude Code or Codex through this lens and you can see every pattern at once:

  • It perceives tool outputs (file contents, test results, error logs).
  • It models your project (repo structure, conventions, open tasks).
  • It plans toward your goal ("add authentication", "fix this failure").
  • It trades off utility (fast patch vs. careful refactor, ask vs. act).
  • It learns from what worked across sessions.

The patterns did not get replaced by large language models. They got composed. The loop is the same one from the top of this post; decide() just got a lot more sophisticated.

The piece the loop is missing: memory

Here is the limitation every one of these patterns shares in practice. The loop restarts cold. New session, new tool, new device, and the agent knows nothing: not your project, not your conventions, not the decisions you argued through last week. decide() is only as good as what it can perceive and remember, and by default it remembers nothing between sessions.

The classic fix is a shared memory layer that lives outside any single agent, so every tool you use reads from and writes to the same context. That is what Vilix AI is built for. It is cloud-hosted with zero infrastructure to manage, and it connects to AI tools over MCP, so the same memory and context follows you across your phone, your laptop, and every connected AI tool.

Three things matter about how it works. First, it stores full conversation history, not just extracted facts, so the next session can revisit the actual reasoning behind a decision instead of a compressed summary of it. Second, your data is portable: list, update, export, or delete everything anytime. The free plan is free forever and the seven-day Pro trial needs no credit card. Third, the honest caveat: MCP memory tools only run when the model decides to call them, so occasionally you need to tell the agent "check Vilix AI for context first." That is a limitation of how MCP works today, not of any single product.

FAQ

Do I need machine learning to build an intelligent agent?

No. Patterns 1 through 4 need zero ML. A thermostat is an agent, a GPS planner is an agent, and both are plain code. ML enters at pattern 5, and even there, simple statistical updates count.

Which pattern should I start with?

Start with simple reflex, then add a model the first time you catch yourself saying "but it should remember X." Most real projects land on model-based reflex with a dash of goal-based planning. Add utility scoring when tradeoffs get painful, and learning when you have a feedback signal worth exploiting.

Can one agent mix patterns?

The useful ones always do. A self-driving car is model-based (world map) and utility-based (trading speed against safety) at the same time. Mixing patterns is normal; the five types are a vocabulary for talking about the pieces, not boxes to stay inside.

Try it

Copy the loop from the top of this post, pick one pattern, and build the smallest agent that does something real: a reflex agent that watches a folder and sorts files by extension, or a model-based one that remembers what it already processed. You will learn more from 30 lines of running code than from any definition.

And if you build agents that talk to you across sessions and tools, give them memory that persists. That is the difference between a clever demo and a working partner.

Top comments (0)