Skip to content
MikeDaDev's Blog

Agentic AI architecture: the boring parts that decide whether your agent survives production

Tung Bui Thanh (MikeDaDev)15 mins read

Circuit board with brain
On this page

"An agent is just an LLM calling tools in a loop."

That sentence is completely accurate, and about as useful as saying a database is just files on a disk. It explains the demo you can build in an afternoon. It does not explain why that demo, once real users arrive, starts looping, leaks a customer's address into a log, or spends eleven dollars answering a question a SQL query could have answered for free.

The model is the most impressive part of an agentic system and, in my experience, the least likely reason it fails. What fails is everything around it. This post is a map of that "everything": the architecture I look for when I review an agent design, and the questions I ask before it goes anywhere near production.

The process side, how you evaluate, release and operate these systems, is covered in the companion post on the Agentic Development Life Cycle.

# first question: do you need an agent at all?

The most valuable architectural decision is often the one that removes the agent. Anthropic's widely cited write-up on building effective agents draws a line I use constantly:

  • A workflow orchestrates LLM calls and tools through code paths you define.
  • An agent lets the model decide the next step, which tool to use, and when it is done.

Workflows are predictable, cheap and easy to test. Agents are flexible, expensive and harder to reason about. You pay for flexibility with latency, tokens and uncertainty, so buy only as much of it as the problem needs.

Each step to the right adds capability, cost and failure modes. My rule of thumb: move one step further only when you can point to real tasks that the current step cannot handle. "It might need to" is not a reason. A good share of the "agents" I review turn out to be a three-step workflow in costume, and they become faster, cheaper and more reliable the moment we admit it.

The common patterns are worth knowing by name, because they cover more ground than people expect:

PatternShapeGood for
Prompt chainingStep A's output feeds step B, with checks in betweenGenerate, then validate, then format
RoutingA classifier sends input to a specialised handlerSupport triage; a cheap model for easy cases
ParallelisationSeveral calls run at once; results are merged or voted onIndependent sub-questions, guardrail checks
Orchestrator-workersA planner splits work dynamically and delegates itTasks whose sub-steps you cannot know in advance
Evaluator-optimiserA generator drafts, a critic scores, they loop to a limitWriting, translation, code that must pass tests

Only the last two start to feel "agentic". The first three are ordinary software with a model inside.

# the anatomy of an agent

When you genuinely need an agent, stop thinking of it as "the model" and start thinking of it as a small runtime with the model in the middle. This is the reference picture I draw on whiteboards.

Eight components, each with a clear job:

  1. Model. The reasoning engine. Swappable, and you should design as if it will be swapped, because it will.
  2. Control loop. Calls the model, executes its tool requests and, critically, decides when to stop.
  3. Context builder. Assembles what the model sees on each turn: instructions, relevant memory, retrieved documents, recent tool results.
  4. Tools. The agent's hands: everything it can do to the outside world.
  5. Policy and approvals. Who may do what, and which actions need a human.
  6. Guardrails. Checks on the way in and on the way out.
  7. State and memory. What survives between steps, sessions and users.
  8. Observability. Traces, token and cost accounting, and the hooks your evals plug into.

The rest of this post goes through the components where I see the most expensive mistakes.

# the control loop: always know how it ends

The naive loop sends context to the model, runs any tool it asks for, appends the result and repeats. It works beautifully until the model gets confused, retries the same failing call, and keeps going until something external kills it. A production loop looks more like this:

def run(task, budget):
    state = load_checkpoint(task.id) or new_state(task)
    while True:
        if state.steps >= budget.max_steps or state.tokens >= budget.max_tokens:
            return handoff_to_human(state, reason="budget exhausted")
        if clock() - state.started_at > budget.timeout_s:
            return handoff_to_human(state, reason="timeout")
 
        reply = model.generate(build_context(state))           # one model turn
        state.tokens += reply.usage.total
        if reply.is_final:
            return finish(state, output_guardrails(reply.text))
 
        for call in reply.tool_calls:
            if repeated(state, call, times=3):                 # same tool, same args
                return handoff_to_human(state, reason="stuck in a loop")
            if policy.requires_approval(call):
                return pause_for_approval(state, call)         # resumes later from checkpoint
            result = tools.execute(call, idempotency_key=f"{task.id}:{state.steps}:{call.id}")
            state.append(call, summarise_if_large(result))
 
        state.steps += 1
        save_checkpoint(state)

A few details in there are worth calling out:

  • Every exit is explicit. Budget, timeout, loop detection and approval each have a named path. "Give up and hand over to a human" is a feature, not a failure.
  • State is checkpointed after each step. A crash, a deploy or a three-hour wait for approval resumes the task instead of restarting it. For anything long-running, use a durable execution engine (Temporal, a workflow service, or a database-backed state machine) rather than an in-memory loop.
  • Tool calls carry an idempotency key. When the loop resumes after a crash, a retried create_ticket must not create a second ticket.
  • Large tool results are summarised before they enter the context, which leads to the next topic.

# tools: design them like a public API

Tools are where an agent touches reality, and they are an interface whose caller cannot ask you what you meant. Anthropic's guidance on writing tools for agents matches what I have seen in practice: tool design moves quality more than prompt tweaking does.

Compare a tool that mirrors an internal API with one designed for its caller:

{
  "name": "search_orders",
  "description": "Find a customer's recent orders. Use this before any refund or delivery question. Do NOT use it to look up products; use search_catalog for that. Returns at most 10 orders, newest first.",
  "input_schema": {
    "type": "object",
    "properties": {
      "customer_id": { "type": "string", "description": "UUID from the ticket header, not the email address." },
      "status": { "type": "string", "enum": ["any", "delivered", "in_transit", "returned"] },
      "since_days": { "type": "integer", "minimum": 1, "maximum": 365, "default": 90 }
    },
    "required": ["customer_id"]
  }
}

What I look for in a tool review:

  • Few, purposeful tools. Twenty overlapping tools confuse a model the way they would confuse a new engineer. Expose search_orders, not the raw REST surface of three services.
  • Descriptions written for the model. Say what the tool is for, when not to use it, and what each parameter means, with units and formats.
  • Small, high-signal responses. Return the five fields that matter, paginate, and when you truncate, say so in the response.
  • Errors that teach. "customer_id must be a UUID; you passed an email address. Look for the 'Customer' field in the ticket header." lets the agent recover. 400 Bad Request does not.
  • Safe writes. Idempotency keys, dry-run modes and, where possible, a two-step "prepare, then commit" design so that approval sits between the steps.

The Model Context Protocol (MCP), which Anthropic donated to the Linux Foundation's Agentic AI Foundation in late 2025, has become the standard way to package tools so that any compliant agent can use them. It solves distribution and discovery. It solves neither design nor security: a badly designed tool behind MCP is still badly designed, and a third-party MCP server is a dependency that deserves the same scrutiny, pinning and review as any package you install.

# context and memory: less, but better

Large context windows tempted many of us into a lazy architecture: put everything in the prompt and let the model sort it out. It does not scale. Long, noisy contexts cost more, run slower and, past a certain point, reduce accuracy, because the instruction that matters is buried under thirty kilobytes of tool output.

The discipline is now usually called context engineering: on every turn, give the model the smallest set of high-signal tokens it needs. In practice I split memory into layers:

LayerLifetimeExampleMain risk
Working contextOne model callCurrent instructions, last few tool resultsBloat; key facts lost in the noise
Session stateOne task or conversationPlan, decisions so far, scratch notesDrift as the task grows longer
Long-term memoryAcross sessionsUser preferences, validated facts, past outcomesPoisoning; stale or wrong "facts"
KnowledgeOwned by the businessDocs, policies, product data via retrievalRetrieval misses; access control

Three rules save a great deal of pain:

  • Compact, do not accumulate. Summarise or drop old tool output instead of carrying it forward verbatim. Keep the plan and the decisions; discard the raw logs.
  • Order the prompt for the cache. Providers cache the longest identical prefix of a request, and cached input is billed at a fraction of the normal price. Put the stable parts first (system instructions, tool definitions, policy documents) and the volatile parts last. In an agent that resends its whole context on every step, a cache-friendly layout routinely cuts input cost by more than half; a timestamp at the top of the system prompt quietly destroys that saving.
  • Treat writes to long-term memory as privileged. If an agent can store anything it reads as a durable "fact", one malicious document can influence every future session. OWASP lists memory and context poisoning among its top agentic risks for exactly this reason. Require provenance on every memory entry, and let only validated sources write to it.

# multi-agent: a powerful tool with a real price tag

Multi-agent systems are the most exciting part of the field, and the part most often used too early.

The case for them is real. An orchestrator that splits a broad research question into independent threads, runs workers in parallel with their own clean contexts, and merges the findings can outperform a single agent on wide, parallelisable problems. Anthropic reported exactly that for its research feature, together with the bill: agents used about four times the tokens of a chat interaction, and the multi-agent system about fifteen times.

The case against them is also real. Cognition's well-known argument is that agents working in parallel make decisions on partial context, and those decisions conflict. Two workers each make a sensible choice; together the choices are incompatible, and the orchestrator has no idea.

My reading of both: the deciding factor is how independent the sub-tasks are.

  • Breadth-first, read-heavy work (research, search, analysis of separate documents) parallelises well. Use orchestrator-workers, and give each worker a precise brief: objective, output format, tools allowed, and when to stop.
  • Tightly coupled, write-heavy work (one codebase, one document, one plan) usually goes better with a single agent and a well-managed context, or with workers in strict sequence and an explicit contract between them.
  • Quality-critical output benefits from an evaluator-optimiser pair, with a hard cap on iterations so that the critic cannot keep the loop alive for ever.

For agents that talk across team or company boundaries, the Agent2Agent (A2A) protocol, started by Google and now also hosted by the Linux Foundation, standardises how one agent discovers another and hands it a task. A useful mental model: MCP connects an agent to tools; A2A connects an agent to other agents. Neither gives you identity, authorisation or audit trails for free. Those remain your job.

# security: assume the model will be tricked

The most important security fact about agents is that the model cannot reliably tell instructions from data. Any text it reads, whether a web page, an email, a PDF or a tool result, may contain instructions it will try to follow. Better prompts and classifiers reduce the rate; they do not remove the class of bug.

Simon Willison's "lethal trifecta" is the clearest way I know to explain the danger. An agent becomes exploitable for data theft when it combines:

  1. access to private data,
  2. exposure to untrusted content, and
  3. the ability to communicate externally.

Remove any one leg and the attack becomes much harder. Most useful systems cannot remove all three, so the architecture has to contain the damage rather than hope to detect it. The patterns that hold up best share one idea: untrusted text must never be able to choose which actions run.

  • Plan, then execute. The agent commits to a plan (which tools, in what order) before it reads untrusted content. Tool results can fill in data, but cannot add new steps.
  • Dual LLM and capability tracking. A privileged model plans and calls tools but never sees raw untrusted text; a quarantined model reads that text but has no tools, and its output is passed around as an opaque variable. Google DeepMind's CaMeL design takes this further by tracking where each value came from and enforcing policies, such as "data derived from an email may not be sent to an address that did not come from the user", in ordinary code rather than in the prompt.
  • Least privilege per agent and per task. Each agent has its own identity and short-lived, narrowly scoped credentials. Never let an agent borrow a human's full session.
  • Human approval on irreversible actions, with the approval screen showing what will actually happen (the exact recipient, amount and diff), not the agent's own summary.
  • Sandboxed execution and egress control. Code runs in a sandbox; outbound network access goes through an allowlist. Markdown image rendering is an exfiltration channel too, so restrict which domains images may load from.

The OWASP Top 10 for Agentic Applications, published in December 2025, turns this into a concrete checklist: goal hijacking, tool misuse, identity and privilege abuse, supply-chain risk, memory poisoning, insecure inter-agent communication and more. I would make it required reading for anyone signing off an agent design.

# reliability and observability

Agents fail in ways ordinary monitoring cannot see. The service returns 200, latency is fine, and the answer is confidently wrong. The observability bar is therefore higher:

  • Trace every run end to end: each model call, each tool call with inputs and outputs, tokens, cost and the final result. The OpenTelemetry generative AI semantic conventions are a reasonable common format.
  • Track cost per successful task next to quality. A cost spike is often the first visible symptom of a stuck or confused agent.
  • Score samples of production traces with the same graders you use offline, so that regressions surface as alerts rather than complaints.
  • Pin model snapshots and upgrade them deliberately, with an eval run, like any other dependency.
  • Plan for provider failure. Rate limits and outages happen; decide in advance whether you queue, fall back to another model that has passed the same evals, or degrade to suggest-only.
  • Keep a kill switch: a flag that drops the agent to suggest-only mode without a deploy.

# a decision guide

When a team brings me a new agent proposal, these are the questions I ask, roughly in order.

QuestionIf yes, lean towards
Can I write the steps down in advance?A workflow, not an agent
Is a wrong action expensive or irreversible?Prepare-then-commit tools with human approval
Are the sub-tasks independent and read-heavy?Orchestrator-workers in parallel
Do sub-tasks write to the same artefact?A single agent, or workers in strict sequence
Does it read untrusted content and hold private data?Plan-then-execute or dual LLM; block external egress
Does it need to remember across sessions?Explicit memory writes with provenance and expiry
Will another team's agent call it?An A2A-style contract, its own identity, an audit log
Will tasks run for minutes or wait on humans?Durable execution with checkpoints and idempotent tools

# closing thought

The architecture of a good agentic system looks surprisingly conservative. The loop has budgets and named exits. The tools are few and dull. Memory is curated rather than hoarded. Untrusted text cannot choose actions. Dangerous actions wait for a person. Every step leaves a trace. The model provides the intelligence; the architecture decides where that intelligence is allowed to act.

Start with the simplest design that solves real tasks, measure it honestly, and add autonomy only when the data says you have earned it. That advice is not new. It simply matters more now that our software can surprise us.

For deeper dives on specific parts, see the earlier posts on the ReAct loop and multi-agent in production, stateful multi-agent with LangGraph and agentic RAG.

References:


Just personal notes, shared in the hope they are useful. If you found this helpful, feel free to pass it on to your friends and colleagues!

Happy coding ๐Ÿ˜Ž ๐Ÿ‘๐Ÿป ๐Ÿš€ ๐Ÿ”ฅ.