Module 9

AI agent fundamentals


With standardised tools, the model can now decide for itself which steps to take and in which order, rather than just running the tool you ask for.

Reading time: 4 min

Agent persona

An agent is a system in which the model decides which steps to take, and in which order, to reach a goal, using tools, instead of following a fixed programmed flow. The agent's persona is the definition of its role: who it is, what goal it has, which tools it can use, what its limits are and how it should behave.

A well-defined persona is specific and narrow: "you review tickets that have gone unanswered for more than 48 hours and propose an action for each one; you never reply to the customer directly". The broader the scope, the less predictable the behaviour. Before building an agent, it is worth asking whether a fixed flow with one or two model calls would be enough: it often is.

ReAct

ReAct (Reasoning + Acting) is the pattern in which the model alternates between reasoning and acting: it thinks about what it needs, calls a tool, observes the result and thinks again, until it can answer. It is the basis of most tool-using agents.

Its value is that the plan adapts to what the agent finds along the way: if an order doesn't exist, it searches by customer name; if the policy isn't clear, it escalates. Its risk is the loop: without a maximum number of steps, an agent can keep trying variations indefinitely while burning tokens.

Planning and task decomposition

Planning is the agent's ability to break a complex goal into subtasks before executing them, or as it goes along. "Review the backlog and propose actions" becomes listing the pending tickets, classifying them, reviewing their history and drafting a proposal for each one.

An explicit plan makes the agent more predictable and easier to debug, because it can be reviewed before it runs and you can see at which step it failed. For long tasks, the plan should be correctable along the way and each subtask should have a clear criterion for when it is done.

Short- and long-term memory

Short-term memory is what the agent remembers within a task or a conversation: the history and the tool results, which live in the context window. Long-term memory is what persists across sessions: user preferences, decisions made or facts learned, stored outside the model and retrieved when needed.

The context window fills up quickly on long tasks, and whatever is no longer useful has to be summarised or discarded. Long-term memory must be designed like any other data: what is stored, who can see it, how it is corrected if it is wrong and when it expires. A memory that stores a mistake will repeat it indefinitely.

Reflection

Reflection (self-critique) means having the agent review its own output before accepting it: checking whether it answers the request, whether it follows the rules or whether it contains errors, and correcting it if needed. It can be done by the same model in a second step or by another model acting as reviewer.

It improves quality in tasks where errors are detectable (a wrong format, a policy not followed, a miscalculation), but it doubles or triples cost and latency. It works better when the review uses concrete criteria or code checks than when you just ask it, in general terms, to "review and improve it".

Guardrails and human-in-the-loop

Guardrails are the controls that limit what an agent can do or say: input and output validation, step and spending limits, lists of allowed actions and content filters. Human-in-the-loop is the point where the agent stops and waits for a person to approve, correct or reject what it wants to do.

The key is where you put them. An instruction in the prompt ("ask for confirmation before acting") is not a real guardrail, because the model can ignore it. An approval implemented in code, which halts the tool's execution until someone approves it, is. The more irreversible an action, the closer a person needs to be.

Test yourself on this module

Copy this prompt and paste it into your AI (ChatGPT, Claude, Gemini…). It will give you a 20-question multiple-choice test on the module's concepts and then suggest a hands-on exercise.

Act as the examiner for Dani Pérez's "AI Engineering Guide". Examine me on the module "AI agent fundamentals" (https://daniperez.pro/en/resources/ai-engineering-guide/ai-agents).

Concepts covered by the exam:
- Agent persona
- ReAct
- Planning and task decomposition
- Short- and long-term memory
- Reflection
- Guardrails and human-in-the-loop

Exam:
1. 20 multiple-choice questions, each with 4 options (a, b, c, d) and a single correct answer.
2. Ask about understanding and judgement (what each thing is for and when NOT to use it), not about memorising definitions.
3. Spread the position of the correct answer evenly across a, b, c and d.
4. Ask me the questions in 4 rounds of 5. Don't give any example answer (like "1a 2b 3c 4d 5a"): I already know to answer with the letters. Don't tell me whether I got them right until I have answered all 20.
5. At the end, mark them all: for each question, my answer, the correct one and a short explanation. Give me my score out of 20 and tell me which concepts I should review.

Hands-on exercise (after marking):
6. Ask me what application I have or want to build, and which language and framework I work with. If I don't have one, use this: a customer support application for an online shop, with tickets, customers, orders and a knowledge base (FAQ and return policies). In that case, focus the exercise on: an agent that reviews the backlog of unanswered tickets and proposes an action for each one.
7. Suggest an exercise that applies this module's concepts to that application: goal, requirements, criteria to consider it done and common mistakes to avoid.
8. Don't solve it for me. When I bring you my solution, review it against those criteria.