The agent loop
A typical agent loop has four steps:
- Sense: the agent receives input, such as a prompt, a file, or the result of a previous action.
- Plan: the agent decides what to do next, often by breaking the goal into smaller tasks.
- Act: the agent takes an action, such as calling a tool, running code, or asking a question.
- Observe: the agent reads the result of the action and updates what it knows.
Then the loop repeats. The agent keeps going until the goal is met, it reaches a limit, or a human stops it.
Key idea: an agent is a program that chooses actions in a loop, guided by a goal and feedback from the world.
State, tools, and memory
Three concepts keep an agent from being just a fancy chatbot:
- State: the current situation the agent is working with, like a task list, files, or variables.
- Tools: functions the agent can call, such as reading a file, running a test, or searching the web.
- Memory: a record of what the agent has already done and learned, so it can avoid repeating mistakes.
Good agent design separates these clearly. If an agent forgets what it did, it can loop forever or overwrite its own work.
Agents vs scripts
A script follows a fixed sequence of steps. An agent decides what to do based on the current state and feedback. Scripts are predictable; agents are flexible but harder to debug.
This tradeoff is important. Agents are great when the problem has many possible paths or when the environment changes. Scripts are better when the steps are always the same and must be exactly repeated.
| Feature | Script | Agent |
|---|---|---|
| Steps | Fixed | Chosen at runtime |
| Feedback | Ignored or used for errors only | Used to replan |
| Best for | Repetitive, well-defined tasks | Open-ended, changing tasks |
| Risk | Brittle when inputs change | Can loop or make wrong choices |
Simulate the loop
Give the agent a goal, then watch it sense, plan, act, and observe. This demo is simplified, but it shows the rhythm of an agent loop.
Step: idle
A multi-agent loop
Agentic engineering is building systems where multiple agents work together. This Python example has a researcher, writer, and reviewer.
Check yourself
Give an example of a task that should probably be a script, and a task that is better suited to an agent.
Your answer:
Sample answer: A script is good for backing up files every night. An agent is better for debugging a failing test, because it may need to inspect logs, search code, try a fix, and check the result in a loop.
Quick quiz
1. Which of these is part of the agent loop?
2. Why does an agent need memory?
3. When is an agent a better choice than a script?
Keep learning
You have seen the agent loop. Next, learn how agents search for solutions and the classic hill-climbing algorithm that hides inside many planning systems.
First tutorial in the “Agentic Engineering & AI Software Topics” series.