Vibe Engines
YouTube

Vibe Engines is free, interactive AI engineering & system design — runs in your browser. Pick a learning path

Handbooks  /  Agent Patterns
Handbook~14 min readIntermediate
Deep Dive

The patterns
behind every agent.

Give a language model a loop and some tools and it stops being a chatbot and starts being an agent — something that pursues a goal over many steps. This is the field guide to the handful of patterns that agents are built from, and the failure modes that break them in production.

01

What makes something an agent

A plain language model does one thing: you give it text, it gives you text back. An agent wraps that model in a loop and hands it tools. Now, instead of answering in one shot, the model can reason about a goal, take an action in the world, look at what came back, and decide what to do next — over and over, until the job is done.

That shift — from "answer" to "act, observe, adjust" — is the whole idea. Everything in this handbook is a pattern for structuring that loop: how the model decides what to do (ReAct, plan-and-execute), how it acts (tool calling), how it remembers (memory), how many models cooperate (multi-agent), and how you stop it going off the rails (failure modes).

→ Mental model

An agent is not a smarter model. It's the same model, given the ability to take actions and see their results — the difference between a brilliant person answering from memory and the same person with a computer and a web browser.

02

ReAct — the core loop

The pattern almost every agent runs is ReAct: Reasoning + Acting. The model alternates between thinking about what to do and doing it, feeding each result back into the next thought. Reasoning decides the next action; the action's result grounds the next round of reasoning.

The Thought–Action–Observation loop
ThoughtReason about the goal and what to do next.
ActionCall a tool — search[…], query_db[…], calculate[…].
ObservationThe tool returns a real result. Read it…
↑ loop back to Thought until ready
AnswerEnough information gathered — produce the final response.
Each observation injects a fact from outside the model's memory, keeping the reasoning honest.

The magic is the synergy: reasoning makes acting smarter — the model plans which tool to call and recovers from dead ends — while acting makes reasoning honest, because every observation is a real fact from the world, so the chain of thought can't quietly drift into fiction. ReAct hallucinates far less than reasoning alone.

→ Worth knowing

The interleaved trace is human-readable, so you can see why an agent did what it did — and correct it mid-run. That legibility is a big reason ReAct became the default. See the ReAct paper breakdown for the full story.

03

Tool & function calling

An agent is only as capable as the tools it can reach. Tool calling (a.k.a. function calling) is how a model asks your code to do something: you describe each tool with a name, a plain-language description, and a typed argument schema; the model responds with a structured call — the tool name and JSON arguments — instead of prose.

The quality of your tool descriptions matters as much as the model. The model decides which tool to use by reasoning over those descriptions, so vague or overlapping ones cause wrong calls. Good tools are narrow, well-named, clearly documented, and validate their inputs.

Part of a toolWhat it doesFailure if it's weak
NameIdentifies the tool in the model's callAmbiguous names → wrong tool picked
DescriptionTells the model when to use itVague desc → tool ignored or misused
Argument schemaTypes and shape of the inputsLoose schema → malformed / hallucinated args
Return valueThe observation fed back to the modelNoisy return → wasted context, confusion

Design tool schemas the way you'd design an API — that's exactly what they are. The interactive Tool-Schema Designer lets you draft and pressure-test one.

04

ReAct vs plan-and-execute

ReAct decides one step at a time, re-reasoning after every observation. That's flexible but token-heavy, and on long tasks the agent can lose the thread. Plan-and-execute takes the opposite tack: first produce a full plan of steps, then execute them, re-planning only when reality diverges.

Two ways to structure the loop

ReAct — decide as you go

  • Re-reason after every observation
  • Adapts well to surprises
  • More tokens, can drift on long tasks
  • Best for open-ended, unpredictable goals

Plan-and-Execute — plan first

  • Draft the whole plan, then run it
  • Cheaper, more structured execution
  • Re-plans only when a step fails
  • Best for well-defined, multi-step tasks

These aren't rivals — mature agents combine them: a planner lays out the high-level steps, and each step runs a small ReAct loop to handle the messy details. Choosing where to draw that line is one of the real design decisions in building an agent.

05

Reflection & self-critique

Agents get noticeably better when they check their own work. Reflection adds a step where the model critiques its output — "is this actually correct? did I miss anything?" — and revises before finishing. It's the difference between a first draft and a proofread one.

Common forms: a critic pass that scores or flags the draft, a verifier that re-checks a result against the tools (re-run the query, re-read the source), and retry-with-feedback, where a failed step is fed back in with the error so the model can fix it. Reflection costs extra calls, so reserve it for steps where being wrong is expensive.

→ Real-world use

Coding agents lean on reflection hard: write code → run it → read the error → fix it. The test suite is the verifier, and the loop repeats until it goes green. Reflection with a real signal beats reflection on vibes alone.

06

Memory — short-term and long-term

Agents have two kinds of memory, and confusing them is a classic bug.

Two memories, two jobs

Short-term — the context window

  • The running transcript of thoughts, actions, observations
  • Everything the model "sees" this turn
  • Finite — fills up on long runs
  • Managed by summarizing or trimming old steps

Long-term — an external store

  • Facts written out to a database (often a vector DB)
  • Survives across sessions
  • Retrieved on demand, like RAG
  • Lets the agent "remember" you next time

Short-term memory is the context window — powerful but bounded, so as a run grows you must summarize earlier steps to keep the important bits without blowing the budget. Long-term memory is an external store the agent writes to and retrieves from, so knowledge persists beyond one conversation. Retrieving from it is exactly RAG, pointed inward at the agent's own notes.

07

Routing & multi-agent orchestration

One agent with fifty tools becomes confused and slow. A cleaner pattern is to route: a lightweight classifier or small agent decides which specialised agent or tool should handle a request, then hands off. Push this further and you get multi-agent orchestration — several agents, each good at one thing, cooperating.

Planner / worker orchestration
PlannerDecomposes the goal into sub-tasks and assigns them.
WorkersSpecialised agents (researcher, coder, writer) each execute a sub-task.
HandoffResults flow back; the planner integrates or re-assigns.
ResultPlanner assembles the final answer once sub-tasks complete.
Coordination — routing, shared state, and clean handoffs — is the hard part, not the agents themselves.

Multi-agent shines when sub-tasks are genuinely different (research vs. writing vs. code) or need isolation (one agent's mistake shouldn't corrupt another's context). But every extra agent adds coordination cost and new failure surfaces, so reach for it only when a single agent is genuinely struggling. The full architecture — message flow, shared state, termination — is covered in Multi-Agent Orchestration.

08

Failure modes & guardrails

Most of the engineering in a real agent is not the happy path — it's keeping the loop from destroying itself.

The four ways agents break — and how to guard each

Infinite loops

The agent repeats the same action forever, or oscillates between two.

Guard: hard step/time limits, loop detection, budget caps.

Hallucinated tool calls

Invents a tool that doesn't exist, or fills arguments with plausible nonsense.

Guard: strict schema validation; reject and re-prompt on bad calls.

Error cascades

One failed or misread observation derails every step after it.

Guard: catch tool errors, feed them back, allow retries with backoff.

Silent wrong answers

The agent finishes confidently with a wrong result and no error.

Guard: verifier/reflection step; human-in-the-loop for high stakes.

The single most important guardrail is the step limit — a hard cap on how many loops an agent may run before it must stop or ask for help. After that: validate every tool call against its schema, wrap tools in error handling so a failure becomes an observation rather than a crash, and put a human in the loop for any irreversible or high-stakes action (spending money, deleting data, sending messages).

→ Key insight

You cannot fix these by prompting alone — they're engineering problems. And you can't fix what you can't see: log the full Thought–Action–Observation trace of every run. Evaluating that trajectory is its own discipline, covered in the Agent Evals handbook. And once the loop is guarded, the next level is running it unattended — triggers, stopping conditions, and verification around whole runs — which is loop engineering.

The two most important guards are just a few lines. A step limit caps the loop; retry with exponential backoff survives a flaky tool without hammering it:

while steps < limit: …  ·  retry delays = 1, 2, 4, 8, 16s, then surface the error

A stuck agent halts at the cap instead of spinning forever; a transient failure recovers on attempt 3 or gives up cleanly. The runnable version below is both guards.

RUN IT YOURSELF

The two guardrails every agent loop needs

Most agent failures aren't the model — they're the missing guardrails, and the two load-bearing ones are a handful of lines. A step limit means a stuck agent halts at the cap instead of spinning forever; a task that needs three steps finishes, one that never converges is stopped at ten. Retry with exponential backoff (1, 2, 4, 8, 16s) survives a flaky tool: it recovers on attempt three or gives up cleanly and surfaces the error, instead of hammering a dead service or cascading. Change the limits or the failure point.

CPython · WebAssembly
By the numbers

Worked example: one guarded ReAct run, step by step

Task: “Has order 1182 shipped? If not, email the customer.” The agent has three tools: query_db, track_shipment and send_email. Say the system prompt plus tool schemas cost 1,500 tokens, and each Thought, Action and Observation adds 500 to the transcript. The step limit is 10.

StepThought → ActionObservationInput tokens this call
1Find the order → query_db(1182)status: packed, tracking ref Z91,500
2Check the carrier → track_shipment("Z9")Timeout. The wrapper waits 1 s and retries, times out again, waits 2 s, and succeeds on attempt 3: “not collected”2,000
3Not shipped, so tell the customer → send_email(...)Held for human approval: sending is irreversible2,500
4Approved and sent → final answer“Not shipped yet; customer emailed.”3,000
Total4 of 10 steps used3 s of backoff, spent outside the model9,000
Stuck runModel re-calls track_shipment every stepStep limit stops it at 1037,500

Three guards did the work. The retry lived in the tool wrapper, so the flaky carrier cost 3 seconds and no extra model steps or tokens. The approval gate sat on the only irreversible action. The step limit turned the stuck run into a clean stop at 37,500 tokens instead of an open-ended bill. Notice the token column too: every step resends the whole transcript, so the cost of each call rises as the run goes on.

Trade-offs

Cost and trade-offs of each pattern

Here Time is model calls, Space is the context each call carries, and the last column is what you get for it. Token figures use the example’s 1,500 + 500 per step.

CaseTimeSpaceWhy
ReAct, n stepsn + 1 calls, one after anotherGrows every step: 10 steps total 37,500 input tokens, 20 steps total 125,000Adapts after every observation. Doubling the steps more than triples the tokens, because each call resends the transcript.
Plan-and-executeOne planning call, then one per stepEach step sees the plan and its own inputs, not the full historyCheaper and steadier on long, well-defined tasks. Pays for a re-plan whenever reality diverges.
Reflection+1 critic or verifier call per checked stepThe draft plus the critiqueCatches silent wrong answers. Worth it on expensive-to-be-wrong steps, wasted on trivial ones.
Multi-agentCalls multiply across agents, plus handoffsEach agent keeps a smaller, isolated contextIsolation and specialisation, paid for in coordination and new failure surfaces.
Step limitNoneCaps the worst case: 10 steps can never pass 37,500 tokens hereThe only guard that bounds cost. Set too low, it cuts off legitimate long tasks.
Retry with backoffWall-clock waits: 1 + 2 = 3 s to recover on attempt 3No model tokens if done in the wrapperSurvives a flaky tool without hammering it. Retry only idempotent calls, or a retried email is sent twice.

The pattern choice is mostly a cost choice. ReAct buys flexibility with tokens that grow faster than the step count. Plan-and-execute buys cheapness with rigidity. The guards are cheap, and the two that matter most cost no model calls at all.

In the wild

Where this shows up in production and interviews

  • Tool-use APIs

    OpenAI’s function calling and Anthropic’s tool use both take tool definitions as a name, a description and a JSON Schema for the arguments, then return a structured call for your code to run. That is the tool table above, as an API contract.

  • Coding agents

    Claude Code, Cursor’s agent mode and similar tools run a ReAct loop over a repository: read files, edit, run the tests, read the failure, edit again. The test suite is the verifier from the reflection section, and destructive shell commands sit behind an approval prompt.

  • Support and operations agents

    Agents that touch money or customers usually let reads run freely and put refunds, cancellations and outbound messages behind a human or a policy check, exactly like step 3 of the worked example. Every run is logged as a trace so a bad outcome can be replayed.

  • The interview version

    For “design an agent that does X”, draw the loop, list the tools with their schemas, and say where you would put the step limit, the retries and the approval gate. Then explain how you would evaluate the trajectory, not just the final answer. Saying “I would add guardrails” without naming them scores far lower.

Self-check

Check yourself

Your agent averages 6 steps, but a few runs hit 40 and cost a fortune. Which single change bounds the bill, and what does it cost you?

A step limit. With the example’s numbers a 40-step run sends 450,000 input tokens against 37,500 for a cap of 10. The price is that a genuinely long task is stopped too. Halt with a clear message, or hand off to a human, rather than failing silently.

A task has eight well-known steps that rarely change, run thousands of times a day. ReAct or plan-and-execute?

Plan-and-execute. The steps are predictable, so re-reasoning after each one buys little and resends a growing transcript every time. Keep a small ReAct loop inside any step that is messy, and re-plan only when a step fails.

A charge_card tool times out. Should the wrapper retry it with backoff?

Only if the call is idempotent. A timeout does not mean the charge failed, so a blind retry can charge twice. Pass an idempotency key so the payment service ignores the repeat, or check the charge status before retrying.

The model keeps calling search_orders when it should call search_products. Is the fix a better prompt?

Look at the tool descriptions first. The model picks tools by reasoning over their names and descriptions, so two vague, overlapping ones produce exactly this. Rename and describe each with when to use it, and reject calls whose arguments fail the schema.

Frequently asked

Quick answers

What is an AI agent?

A language model wrapped in a loop with tools, so it can reason about a goal, take an action, observe the result, and repeat until done — not just answer once.

What is the ReAct pattern?

Interleaving reasoning with actions: Thought → Action → Observation, looping until the agent can answer. Reasoning guides actions; observations keep reasoning grounded.

ReAct vs plan-and-execute?

ReAct decides one step at a time (flexible, token-heavy); plan-and-execute plans all steps up front then runs them (structured, cheaper). Many agents combine both.

How does agent memory work?

Short-term memory is the finite context window (the running transcript); long-term memory is an external store — often a vector DB — the agent writes to and retrieves from across sessions.

What are the main agent failure modes?

Infinite loops, hallucinated tool calls, error cascades, and silent wrong answers — guarded by step limits, schema validation, error handling, and human-in-the-loop.

The Agent Patterns Handbook · Vibe Engines · 2026
Finished this one? 0 / 209 Handbooks done
NextAgents vs Workflows Next on the “Building agents” ladder · rung 2 of 10

Explore the topic

See this alongside everything else on the same subject — handbooks, system designs, challenges and tools, in one place.

More Handbooks