Leonardo Castro

Anatomy of an AI agent: what happens between the request and the response

The parts of an AI agent and the loop that connects them: perception, memory, reasoning, tools, evaluation, limits, guardrails and observability.

By Leonardo CastroPublished on 9 min read
  • AI Agents
  • LLMs
  • AgentOps
  • Architecture
  • MCP

An LLM on its own responds to text with text. An agent goes further: it reads the request, looks up what it knows, decides whether it needs to act, uses tools, checks the result and repeats until it is done. Frameworks change, but the parts tend to be the same. A survey of LLM-based agents sums them up in three parts: brain, perception and action (Xi et al., 2023). Here they become six steps.

Throughout the text, one request serves as the example: “Move my Thursday meeting with Ana to next week and let her know by email.”

This article shows what the agent is made of. The runtime, the code that drives the loop and executes the tools, is part of what the AI Harness article calls the harness. Permissions, sandboxing, limits and evaluation are covered there.

Diagram of the anatomy of an AI agent. The top band is the environment: inputs (user request, events, systems and APIs, files and images), the response and the tools (MCP, APIs, code, databases, files, web and services). Below it sits the agent, separated from the environment by a boundary with four guardrails: input, action, result and output. Inside the agent: perception, context (working memory), long-term memory (episodic, semantic and procedural), the reasoning patterns, the decision 'answer right away?', planning, the tool request, the runtime that executes it, observation, the self-check and the decisions 'goal met?' and 'limit reached?', which close the loop. The bottom band holds the observability panel with the trace of one run, the guardrails for each crossing and the conceptual basis.
The agent and its environment: every flow arrow that crosses the boundary goes through a guardrail.
View the image at full size

1. Perceive

The agent can be triggered by a user request, by an event (such as a webhook, the automatic notice another system sends, a schedule or an alert), by an API or by a file. Perception turns that input into something the model understands: it reads text, images or audio, extracts the intent and the data, and tags where each piece came from. That tag matters later, because whatever comes from third parties, such as emails, web pages and files, is data, not instructions.

Diagram detail: the four inputs go through the input guardrail and reach perception, which reads text, images and audio, extracts intent and data and tags where each piece came from.
1. Inputs cross the boundary and become context.

In the example: the intent is “reschedule and notify”. The data is the Thursday meeting, Ana and “next week”.

2. Remember

The model only sees what is in its context: instructions, the goal, the conversation so far, what came back from memory, tool results and the list of available tools. That context works as working memory, which Weng (2023) calls short-term memory.

What needs to last beyond a single run goes into long-term memory. CoALA, a framework for describing language agents, separates three types (Sumers et al., 2024):

TypeWhat it keeps
EpisodicWhat happened: history and past runs
SemanticWhat is known: facts, documents, knowledge base
ProceduralHow to do it: instructions, prompts and code

A vector database is not a type of memory. It is a way to store and search semantic or episodic memory, as in RAG architectures.

Diagram detail: the context, or working memory, stores to and retrieves from long-term memory, split into episodic, semantic and procedural memory, kept in a vector database, SQL or files.
2. The context stores to and retrieves from long-term memory.

In the example: episodic memory recalls that, the last time a meeting was moved, Ana turned down an afternoon slot. Semantic memory supplies her email address.

3. Decide: answer or plan

The first question in the loop is “answer right away?” If the answer is already in the context, the agent replies without using any tool. If not, it plans: it splits the goal into subtasks and picks the next action.

How it reasons depends on the pattern in use:

PatternHow it works
Chain-of-ThoughtThinks step by step before answering
ReActInterleaves reasoning, action and observation
Plan-and-ExecutePlans everything first, then executes

Chain-of-Thought comes from Wei et al. (2022) and ReAct from Yao et al. (2023). Plan-and-Execute was described by LangChain (2023), inspired by Plan-and-Solve (Wang et al., 2023) and BabyAGI.

Diagram detail: the 'How the model reasons' box, with Chain-of-Thought, ReAct and Plan-and-Execute, and the decision 'answer right away?'. If not, planning splits the goal into subtasks and the model emits a tool request without executing anything.
3. With no ready answer, the agent plans and requests an action.

In the example: it can’t answer right away. The plan has three steps: find the meeting in the calendar, move it to a free morning and send the email.

4. Act: the model requests, the runtime executes

The model executes nothing. It returns a structured request, such as move_event(id, new_date). The runtime executes it: it checks whether the action is allowed and whether the arguments make sense, calls the tool and handles errors and timeouts.

Tools can be APIs, database queries, code running in a sandbox (an isolated environment), reading and writing files, web search or services such as email. MCP (Model Context Protocol) standardizes that connection: an MCP server exposes tools, resources and prompts, and any compatible application can use them.

Diagram detail: the tools and systems in the environment. The runtime checks permission and arguments and calls the tool through the action guardrail. The result comes back through the result guardrail to observation, which records it as data.
4. The runtime executes, and the result comes back as data.

In the example: reading the calendar and moving the event are allowed. Sending the email requires human approval, because it speaks on the user’s behalf.

5. Evaluate and stop

After each action, the agent compares the result with the goal. Reflexion takes the idea further: when an attempt fails, the agent writes a reflection on the mistake and keeps it in episodic memory for the next try (Shinn et al., 2023). This self-check happens inside the loop. Measuring the quality of the agent as a whole is covered under evaluation in AI Harness.

If the goal is met, it answers. If not, the runtime checks the limits on steps, time and cost. If there is still room, the loop goes back to planning with the next step. If there isn’t, the agent stops and returns a partial answer with the reason, or hands the case to a human.

Diagram detail: the self-check compares result and goal. 'Goal met?' leads to the output. If not, 'limit reached?' checks steps, time and cost: if yes, it exits with a partial answer; if not, it goes back to planning with the next step.
5. Goal met: answer. Not met: check the limit before trying again.

In the example: if Ana isn’t free at the chosen time, the agent tries another morning. If the limit runs out first, it stops and gives the user the slots it tried, with the reason.

6. Protect and observe

Every flow that crosses the boundary between the agent and the world goes through a guardrail, an automatic check that blocks, adjusts or asks for approval:

CrossingWhat to check
InputPrompt injection, scope, sensitive data
ActionLeast privilege, arguments, human approval
ResultExternal content is data, not instructions
OutputData leaks, format, policy

OWASP lists prompt injection (LLM01) and excessive agency (LLM06: more tools, permissions or autonomy than needed) among the top risks for LLM applications. To counter excessive agency, it recommends least privilege and human approval for high-impact actions.

To find out what happened, every run produces a trace: a timeline with one segment (span) for the agent, for each model call and for each tool. In the figures, the dashed span: … labels mark the parts that produce their own segment. The trace yields latency, errors and tokens. The OpenTelemetry conventions for generative AI, an open telemetry standard still in development, standardize the spans and the token and duration metrics. Cost is calculated from the tokens.

Diagram detail: the observability panel shows one trace per run, with bars for the agent, an inference, a tool and another inference. Footer: tokens become cost; latency; errors.
6. One trace per run: agent, model and tools on the same timeline.

In the example: the trace shows the model calls, the calendar read, the event change and the email waiting for approval.

Start simple

Not every problem needs an agent. When the steps are always the same, a workflow with fixed code is cheaper and more predictable. Anthropic recommends finding the simplest solution possible and only increasing complexity when needed (Anthropic, 2024). Knowing where each part lives helps you decide how much agent a problem calls for.

References

  1. Xi, Z. et al. (2023). The Rise and Potential of Large Language Model Based Agents: A Survey. arXiv:2309.07864
  2. Weng, L. (2023). LLM Powered Autonomous Agents. Link
  3. Wei, J. et al. (2022). Chain-of-Thought Prompting Elicits Reasoning in Large Language Models. NeurIPS 2022. arXiv:2201.11903
  4. Yao, S. et al. (2023). ReAct: Synergizing Reasoning and Acting in Language Models. ICLR 2023. arXiv:2210.03629
  5. LangChain (2023). Plan-and-Execute Agents. Link
  6. Wang, L. et al. (2023). Plan-and-Solve Prompting: Improving Zero-Shot Chain-of-Thought Reasoning by Large Language Models. ACL 2023. arXiv:2305.04091
  7. Sumers, T. R. et al. (2024). Cognitive Architectures for Language Agents. TMLR. arXiv:2309.02427
  8. Shinn, N. et al. (2023). Reflexion: Language Agents with Verbal Reinforcement Learning. NeurIPS 2023. arXiv:2303.11366
  9. Anthropic (2024). Building effective agents. Link
  10. Anthropic (2024). Introducing the Model Context Protocol. Link. Specification: modelcontextprotocol.io
  11. OWASP (2025). LLM01:2025 Prompt Injection and LLM06:2025 Excessive Agency. OWASP Top 10 for LLM Applications. LLM01 · LLM06
  12. OpenTelemetry. Semantic Conventions for Generative AI (in development; accessed November 2026). GitHub