Agentic AI · Module 2 of 41 · 13 concepts · about 16 min

Anatomy of an AI Agent

Every building block of an agent named and placed: the model, the system prompt and goal, the environment, observations and actions, tools, memory, planning, state, feedback, policies and guardrails, and how they fit together.

At work: The whiteboard sketch an engineer draws before writing any code, and the vocabulary a team uses to say where a bug lives.

The reasoning model

What it is

The model (the LLM) is the part that decides. On each turn it is given text and it writes text. For an agent, that text is one of two things:

  • a request to run a tool, with the inputs to use;
  • a final answer, which ends the task.

It has no memory between calls, cannot run anything itself, and knows nothing that is not in the text it was just sent.

Why it matters

Understanding these limits removes most of the mystery.

  • Because it has no memory, your code must resend everything it needs on every turn.
  • Because it cannot run anything, every action passes through your code, where you can check it.
  • Because it only knows what it is sent, most bad decisions come from bad or missing input, not from a weak model.

In the real world: A team's agent improves more from rewriting three tool descriptions than from upgrading to the largest model, at a fifth of the cost.

Reaching for a bigger model when the agent misbehaves. First read exactly what the model was sent on the failing turn. In most cases the information it needed was missing, buried or contradicted.

System prompt and goal

What it is

Two pieces of text tell the model what it is doing:

  • The system prompt: standing instructions written by you. Who the agent is, what it may and may not do, how to behave when unsure. It is the same for every task.
  • The goal: the particular task, usually the user's message. It changes every time.

Why it matters

They do different jobs and should be kept apart.

  • The system prompt carries your rules, so it should be stable and tested.
  • The goal carries the user's wishes, which you do not control and must not let override your rules.
  • A model treats both as text. Only your code can make the rules binding, which is why real limits live in tools, not in the prompt.

In the real world: A retailer keeps its system prompt in version control and reviews changes like code, after a one-word edit doubled the agent's refund rate.

SYSTEM_PROMPT = """You are the support agent for Kirana Kart, a grocery delivery app.

Your job: resolve the customer's request completely, using the tools provided.

Rules
- Look up the order before you make any claim about it.
- Refunds: follow the policy returned by get_policy. Never invent a rule.
- If you cannot verify what the customer says, or the request is outside
  your tools, call hand_over with a short summary.
- When you are finished, say exactly what you did.
"""

goal = "My order came an hour late. I want the delivery fee back."

Writing safety rules only in the system prompt ('never refund more than ₹500'). A persuasive customer, or text hidden in a document, can talk a model past an instruction. The cap must also be enforced in the refund tool's code.

Environment, observations and actions

What it is

Three words describe how an agent meets the world:

  • The environment is everything outside the model that the agent can read or change: databases, APIs, files, websites, the user.
  • An observation is what comes back from the environment: a tool's result, an error, a new message from the user.
  • An action is something the agent asks to have done in the environment: run a search, issue a refund, send an email.

Why it matters

An agent is only as good as what it can see and do.

  • If an observation is missing or unclear, the next decision is a guess.
  • If an action can do more than the task needs, a mistake can do more damage than the task was worth.
  • Designing the environment (what is visible, what is possible) is the main design work.

In the real world: A travel agent's flight search returned 200 results per call; trimming each observation to the five best options halved the cost and stopped the agent picking flights it had 'lost' in the noise.

Returning raw system output as the observation: a five-hundred-line JSON blob or a stack trace. The model must read every word of it on every later turn. Return the few fields that matter, in plain words.

Practise it: Design an AI Agent for Customer Refunds, Design a Multi-Agent Incident Response System, Design an AI Customer Support Agent

Tools

What it is

A tool is a function in your code that the model can ask to have run. It has three parts the model sees:

  • a name, such as get_order;
  • a description in plain words: what it does and when to use it;
  • a schema: the inputs it accepts and their types.

Tools that only look things up are read tools. Tools that change something are write tools.

Why it matters

Tools are the agent's hands and eyes.

  • Without read tools it can only repeat what it learned in training.
  • Without write tools it can only talk.
  • Tools are also where you enforce rules, because they are ordinary code that you control. Module 3 covers them in depth.

In the real world: A support agent given five task-shaped tools outperforms the earlier version given the full thirty-endpoint orders API.

Exposing your whole internal API as tools. A model given forty overlapping tools chooses badly. Give it a handful shaped around the task, each doing one clear thing.

Memory

What it is

Memory is anything the agent can use that it was not just told. Two kinds matter at this stage:

  • Short-term memory: the record of the current task, resent to the model on each turn. It is lost when the task ends.
  • Long-term memory: facts saved in a store outside the model, and looked up when relevant: a customer's preferences, what happened last week.

Why it matters

A model has no memory of its own, so every kind of memory is something you build.

  • Short-term memory lets the agent follow a task across many steps.
  • Long-term memory lets it pick up where it left off and treat a returning user as known.
  • Both must fit in the limited space the model can read at once. Module 5 covers this.

In the real world: A customer mentions a nut allergy once; the agent saves it, and three weeks later warns them about a substitution containing almonds.

Believing the model 'remembers' earlier conversations. It does not. If something from last week appears in a reply, your code fetched it and put it in front of the model.

Planning

What it is

Planning is deciding the steps before, or while, doing them. There are two basic styles:

  • Step by step: decide one action, see the result, decide the next.
  • Plan first: write the whole list of steps, then carry them out, changing the list if something unexpected happens.

Why it matters

Without a plan, an agent on a long task wanders.

  • It repeats work, forgets parts of the request, or stops early.
  • A written plan keeps the goal in view and lets a person see what is intended before it happens.
  • Too much planning wastes money on simple tasks. Module 7 covers the trade-off.

In the real world: A research agent writes a five-item plan, discovers on item two that the company was acquired, and rewrites items three to five instead of pressing on with stale questions.

Treating the plan as fixed. The first plan is written before the agent has seen anything. A good agent revises it when a result contradicts it.

Practise it: Design a Multi-Agent AI System, Design an Autonomous Research Agent

State

What it is

State is the complete record of where a task stands right now. It includes:

  • the messages and tool results so far;
  • the current plan and which steps are done;
  • values the code tracks: steps used, money spent, approvals pending.

If you saved the state and the machine crashed, you could load it and continue from the same point.

Why it matters

State is what makes an agent dependable over time.

  • It lets a task survive a crash or a restart.
  • It lets a task pause for hours while a person approves something.
  • It lets you inspect exactly what the agent knew when it made a decision. Module 6 covers it.

In the real world: A claims agent waits two days for a customer to upload a photo, then resumes at the same step, because its state was saved and not held in memory.

Keeping state only in a running program's variables. The first deployment or crash wipes every task in progress. Save it to a database after each step.

Practise it: Design a Multi-Agent AI System, Design a Multi-Agent Incident Response System

Feedback

What it is

Feedback is information about how well things are going. An agent gets it from three places:

  • The environment: the tool succeeded, failed, or returned something unexpected.
  • Checks you run: tests pass, a total reconciles, a second model reviews the draft.
  • People: a correction, an approval, a rating.

Why it matters

Feedback is what turns a string of guesses into a process that converges.

  • An agent that cannot tell whether a step worked will build on a failure.
  • The stronger the feedback, the more freedom you can safely give. Coding agents are trusted because tests give precise feedback.
  • Where there is no natural check, you must build one or keep a person involved.

In the real world: A coding agent runs the tests after every change; a failing test's message becomes the next observation, and it fixes its own mistake without anyone noticing there was one.

Accepting the agent's own report as feedback. 'I have issued the refund' is text. Check the payment system to learn whether a refund exists.

Policies and guardrails

What it is

Two related ideas keep an agent inside its limits:

  • A policy is a rule about what is allowed: 'refunds above ₹500 need a person', 'never email outside the company'.
  • A guardrail is the code that enforces a policy: a check on the input, on a tool call before it runs, or on the output before it is shown.

Why it matters

A model can be persuaded; code cannot.

  • Policies written only in the prompt hold most of the time, which is not good enough for money or private data.
  • Guardrails written as code hold every time, whatever the model was told.
  • The design question is always: what is the worst thing this agent can do, and which line of code prevents it? Modules 31 and 32 cover this.

In the real world: A refund tool rejects any amount above the order total, whatever the model asked for; the one time the model is talked into ₹50,000, the customer receives an error message, not the money.

Adding guardrails after an incident. By then the design assumes they are not there. Decide the worst case and its guard while you design the tools.

Practise it: Design a Self-Correcting Code Agent

How the parts work together

What it is

One turn of an agent, with every part in its place:

  • The orchestrator (your loop code) loads the state and builds the text to send: system prompt, goal, tool definitions, relevant memory, and the history so far.
  • Input guardrails check that text.
  • The model reads it and returns either a tool request or a final answer.
  • For a tool request, policy checks decide whether it may run; the tool then acts on the environment.
  • The result comes back as an observation, the state is saved, and the loop goes round again.
  • For a final answer, output guardrails check it before the user sees it.

Why it matters

Seeing the whole machine shows where each kind of problem lives.

  • A bad decision is a model, prompt or context problem.
  • A wrong look-up is a tool problem.
  • Running for ever is a loop problem.
  • Forgetting is a memory or state problem.
  • Doing something forbidden is a missing guardrail.

In the real world: During an incident review a team walks the diagram box by box and finds the fault in ninety seconds: the state was saved before, not after, the refund tool ran.

Visual example

Architecture

Only the third box is the model. The rest is your code.

One turn through every part of an agent

  1. Load stateThe task so far
  2. Build contextPrompt, goal, tools, memories
  3. Model decidesthe only model stepTool request or final answer
  4. Check policyCode decides if allowed
  5. Tool actsReads or changes the world
  6. Observe and saveRecord, save, repeat

A final answer passes an output check and goes to the user.

Thinking all the intelligence is in the model. Only one box in the diagram is the model. Everything else is ordinary code you write and control, and that is where most reliability comes from.

Which part to blame

What it is

A table for finding the faulty part from the symptom. Most agent debugging is this look-up.

Why it matters

Without it, every problem looks like 'the model is not smart enough'.

  • Naming the part tells you what to change: a description, a limit, a stored fact, a check.
  • It is also how senior engineers answer 'how would you debug this?' in interviews.

In the real world: A new engineer pins this table above their desk and resolves their first three agent bugs without changing a prompt.

Visual example

Diagnosis

Start in the right-hand column, not with a bigger model.

From symptom to the part at fault
symptomlikely partfirst thing to check
Picks the wrong toolToolsDo two descriptions overlap?
Passes bad inputsToolsIs the schema strict and are inputs explained?
Forgets part of the requestPlanning, contextIs the goal still near the end of the text?
Repeats the same stepLoop, feedbackDid the tool return anything useful?
Never stopsLoopIs there a step and cost limit?
Contradicts last weekMemoryWas the old fact retrieved, and is it still true?
Loses work on restartStateIs state saved after every step?
Does something forbiddenGuardrailsIs the rule enforced in code?

Interview questions: the anatomy of an agent

What it is

The questions interviewers ask about the anatomy of an agent, from the first screening call to the senior round. Cover the answer, say yours aloud, then compare.

Why it matters

Reading an idea and explaining it are different skills.

  • Each answer below is the two or three sentences an interviewer is listening for.
  • The reasoning behind each one is in the cards above. If an answer surprises you, go back to that card.

In the real world: A design interview asks the candidate to draw an agent; the strong answers label the loop and the policy check, not only 'LLM' and 'tools'.

Q: What are the main components of an agent?

A model that decides; tools that read and change the environment; a loop that runs steps; a system prompt and goal; memory; state; and guardrails that enforce policy in code. Only the model is the LLM; the rest is ordinary code.

Q: What is the difference between memory and state?

State is the full record of one task in progress, saved so it can resume. Memory is what is kept beyond the task and looked up later, such as a customer's preferences. State is exact and belongs to a run; memory is selective and belongs to a user or the system.

Q: What is an observation?

What the environment returns after an action: a tool result, an error or a new user message. It is added to what the model reads on the next turn, and its quality largely decides the next decision.

Q: Where should a spending limit be enforced?

In the tool's code, checked against the real order, with the user's identity taken from the session. The prompt may also state it, but a prompt is a request and code is a guarantee.

Q: Does the model execute tools?

No. It writes a structured request naming a tool and its inputs. Your code decides whether to run it, runs it, and returns the result. That gap is where permissions, limits and approvals go.

Q: Your agent misbehaves. Where do you look first?

At the exact text sent to the model on the failing turn. Then map the symptom to a part: wrong tool means descriptions, looping means loop limits or empty results, forgetting means context or memory.

Exercise: draw an agent before you build it

What it is

A short build that uses this module's ideas. Write it yourself before looking anything up; the checks at the end tell you whether it works.

Why it matters

An idea you have only read fades in a week.

  • Building it once, even in fifty lines, is what makes it yours.
  • Interviewers can tell the difference between someone who has read about a loop and someone who has debugged one.

In the real world: An architect refuses to approve any agent project without this one-page drawing; it has stopped two projects that had no answer for 'where is the limit enforced?'.

Your task. Draw the full anatomy of the Kirana Kart support agent on one page, with every part named and every arrow labelled.

Steps

  1. Draw boxes for: user, orchestrator loop, model, tools, environment (orders database, payment system, policy documents), memory store, state store.
  2. Label each arrow with what travels along it: for example 'tool request: refund_fee(order=88213)' from model to orchestrator.
  3. Mark in red each place a rule must be enforced in code, and write the rule beside it.
  4. Write the system prompt (under 150 words) and list five tools with a one-line description each.
  5. Trace the late-delivery example through your drawing, turn by turn.

You are done when

  • No arrow goes from the model straight to a tool or database: every one passes through your code.
  • There are at least three red marks, including one on the refund tool.
  • A colleague can follow the late-delivery request through the drawing without your help.

Stretch. Add what happens when the payment system is down, and when the customer asks for two things at once.