Skip to the page

Follow one request through an AI agent

An AI agent, the working part of agentic AI, is a model that asks for tools in a loop, with code around it doing the work. Follow a refund request from the moment a customer sends it to the answer, then a web page with a hidden instruction and a job too big for one context window, and see what the agent reads and does at every step, with the words for each and where it runs on the Claude API.

request 1Sent
message
Order 88231 never arrived. Can I have my money back?
system prompt
2,140 tokens: role, refund rules, tone
tools
5 cards, 880 tokens
cache_read_input_tokens
3,020 (tools and system prompt)
input_tokens
19 (the new message)

6 steps later

answered

Sorry, Parcelway lost your parcel. I have refunded £42.50 to your card.

From request to answer

The customer, the order, the page, the code base and their values are made up for the example. The steps, the fields, the words and the Claude API features are real.

Order 88231 left the warehouse on 15 October and never arrived. The customer writes to the shop's support agent, which can look up orders, track parcels, read the refund policy and issue refunds.

request 1To the model
message
Order 88231 never arrived. Can I have my money back?
system prompt
2,140 tokens: role, refund rules, tone
tools
5 cards, 880 tokens
cache_read_input_tokens
3,020 (tools and system prompt) (new)
input_tokens
19 (the new message) (new)

Step 1 · Sent

The request goes out as tokens, most of them from the cache

The customer types one line. The model receives far more: the shop's system promptSystem PromptInstructions the builder of an AI app gives the model before you type anything. It sets the role, the tone and the rules, and it outranks what users say. with its rules, a card for each of the agent's 5 tools, and then the message. All of it is counted in tokensTokenThe small piece of text a language model reads and writes, often part of a word. Prices, limits and the context window are all counted in tokens.. The tools and the system prompt are the same for every customer, so the app marks the end of the system prompt for prompt cachingKV CacheWork a model saves from the text it has already read, so it is not redone for every new token. Prompt caching keeps that work between requests and charges less for it.. The next request that starts the same way reads that part from the cache, for a tenth of the normal input price or less, and the model only has to process the new message.

What goes wrong

The cache only matches a start that is identical, byte for byte. Put the time or the customer's name near the top of the system prompt and every request starts differently, so the app pays to write the cache each time and never reads it. A cached entry also lasts 5 minutes by default, refreshed each time it is used.

On the Claude APIPrompt cachingToken counting

The request, with the end of the system prompt marked for the cache
{
  "model": "claude-sonnet-5-5",
  "max_tokens": 4096,
  "tools": [ look_up_order, track_parcel, search_policy, issue_refund, memory ],
  "system": [{
    "type": "text",
    "text": "You are the support agent for the shop. Refund rules: ...",
    "cache_control": {"type": "ephemeral"}
  }],
  "messages": [{"role": "user", "content": "Order 88231 never arrived. ..."}]
}

Step 1 of 7

Which to use

What agents change about building with AI

  1. It was: A prompt went in, text came out, and a person decided what to do with it.

    Now: The model's reply is a request to act, and code carries it out.

    Whatever an agent's tools can reach, it can change. What it may do is set in the tools' code, not in the prompt.

  2. It was: Writing a good prompt was the craft.

    Now: Choosing what is in the context, lap by lap, is the craft.

    Every tool result stays and is re-sent on each lap, and accuracy falls as the context grows. What to fetch, clear, note or summarise decides the answer.

  3. It was: Text from the web was something a person read.

    Now: It is input that can carry orders.

    A hidden line in a page, an email or a file can steer an agent. Least privilege and a person's approval limit what a planted line can do.

  4. It was: A question cost one call to the model.

    Now: It costs one call for every lap the agent takes.

    Each lap re-sends everything so far. Caching the unchanged start and capping laps and spend keep the bill tied to the work.

The words for it

Each one explained in a sentence, and the main ones in full. Every step above uses some of them.

Every concept, A to Z

AgentTool UseContext EngineeringModel Context ProtocolHuman in the LoopPrompt InjectionSystem PromptTokenKV CacheFunction CallingObservationRAGAgent MemoryGuardrailContext WindowHaystackLarge Action ModelMemory ToolPlanner‑Executor PatternCompactionFunction SchemaLangChainLLamaIndexSubagent

On the Claude API

Every step above uses a part of Anthropic's Claude API or Claude Agent SDK, each named in Anthropic's own documentation.

Tool use
Tools as a name, a description and an input schema. The model asks for one, your code or Anthropic's servers run it, and the result comes back.
Planned · Called · Looped · Fetched · Marked
Strict tool use
With strict set to true, the model's inputs to a tool match its JSON schema.
Planned
Thinking
The model works through a problem in thinking blocks before it answers and between tool calls, billed as output tokens.
Planned
Prompt caching
Reuses the unchanged start of a request. Lasts 5 minutes by default, or 1 hour at a higher write price.
Sent · Answered · Lap 30
Token counting
Counts a request's input tokens, tools and images included, before it is sent.
Sent · Lap 1
MCP connector
Connects remote MCP servers from a Messages API request, with no MCP client of your own. Tool calls only, in beta.
Called
Web fetch tool
Fetches a web page on Anthropic's servers, only from addresses already in the conversation, with limits on domains and uses.
Fetched
Memory tool
Files under /memories that the model reads and writes and your code stores, kept between conversations.
Recalled · Noted
Context editing
Clears the oldest tool results or thinking blocks once a request passes a threshold. In beta.
Cleared
Compaction
Replaces the older part of a long conversation with a summary Claude writes on the server. In beta.
Compacted
Claude Agent SDK
The agent loop as a library, with built-in tools, permission rules and approvals, subagents, and limits on laps and spend.
Looped · Checked · Limited · Delegated

Questions

What is an AI agent?
A model that uses tools in a loop: it asks for a tool, reads the result and decides the next step, until the task is done. The model only writes the requests. Code around it, often called the harness, runs the tools and passes the results back.
How does an AI agent work, step by step?
The app sends the model a system prompt, the tool definitions and the user's message. The model replies with a request to use a tool. The app runs it and sends the result back with the whole conversation. This repeats while the model keeps asking for tools, and ends when it replies in words.
What is agentic AI?
AI that carries out a task in steps instead of giving one reply: a model plans, uses tools, reads the results and decides what to do next. Anthropic calls these agentic systems, and counts both agents, where the model directs its own steps, and workflows, where code sets them.
What is the difference between an AI agent and a workflow?
In a workflow, code decides the steps and calls the model at fixed points. In an agent, the model decides which tools to use and in what order, and code only carries out its requests. Anthropic calls both agentic systems and draws the line there.
What is context engineering?
Choosing what goes into a model's context at each step of a task: what to load at the start, what to fetch only when needed, and what to clear, write to notes or summarise as the context grows. It matters because accuracy falls as the context fills.
What is prompt caching, and why does it matter for agents?
Prompt caching reuses the unchanged start of a request, such as the system prompt and tool definitions, so the provider does not process it again. An agent re-sends everything on every lap, so reading that part from the cache, at a tenth of the input price or less on Claude, cuts the cost of each lap.
How do you protect an AI agent from prompt injection?
No single step stops it. Give outside content to the model only as tool results, say in the system prompt that it is data, screen it with a small model, give the agent only the tools its task needs, and have a person approve actions that send, pay or delete.
What is MCP?
The Model Context Protocol, an open standard for connecting AI applications to tools and data. An MCP server offers tools, resources and prompts, and any application that speaks MCP, such as Claude, ChatGPT or Visual Studio Code, can use them without its own connector for each one.
Does the model run the tools itself?
No. The model writes a structured request naming a tool and its inputs. Your code runs your own tools, and the provider runs its server tools, such as web search, then the result goes back to the model.

Read next