Back to all posts

Building AI agents that don't hallucinate: a practical architecture

AI agents are powerful but unreliable. Here's the architecture we use to build agents that know what they don't know.

Building AI agents that don't hallucinate: a practical architecture blog post image

AI agents can reason, plan, and execute multi-step tasks. They can also confidently tell you that 2+2=5 if you're not careful. Building reliable agents requires architectural guardrails, not just prompt engineering.

We've built agents for customer support, data analysis, and workflow automation. Here's the architecture that works.

The agent loop

Every agent follows the same basic loop: observe the environment, reason about the next action, execute the action, observe the result. The magic (and the bugs) are in the details.

Our agent architecture has four layers:

  • Perception layer: parse user input, extract intent, identify entities
  • Reasoning layer: plan the next action, select tools, decompose complex tasks
  • Execution layer: call APIs, query databases, invoke other models
  • Validation layer: check outputs, handle errors, decide whether to retry or escalate

Tool use: the agent's hands

Agents need tools to interact with the world. But giving an LLM access to your database and APIs is terrifying without guardrails.

We use a tool registry pattern:

  • Each tool has a schema (inputs, outputs, side effects)
  • Tools are sandboxed—can't access resources outside their declared permissions
  • Dangerous operations (writes, deletes) require explicit confirmation
  • Tool calls are logged and auditable
The agent should be able to do anything a human user could do, but no more. Scope the tools to the task.

Memory: the agent's context

Agents need memory to maintain context across multiple turns. But dumping the entire conversation history into every prompt is expensive and noisy.

We use a tiered memory system:

  • Working memory: last 5-10 messages (always included)
  • Episodic memory: summarized conversation history (retrieved as needed)
  • Semantic memory: facts and preferences extracted from conversations (stored in vector DB)
  • Procedural memory: learned patterns and successful action sequences (fine-tuned into the model)

The validation layer: catching hallucinations

This is where most agent systems fail. The agent executes an action, gets a result, and assumes it's correct. But LLMs hallucinate tool outputs, misinterpret errors, and confidently proceed with wrong data.

Our validation layer:

  • Schema validation: every tool output must match its declared schema
  • Sanity checks: numeric outputs within expected ranges, dates in valid formats
  • Consistency checks: new information doesn't contradict established facts
  • Confidence scoring: low-confidence outputs trigger a retry or human escalation

When to escalate to humans

The best agents know when they're stuck. We track:

  • Retry count: if the agent tries the same action 3 times without success, escalate
  • Confidence threshold: if the agent's self-assessed confidence drops below 70%, escalate
  • Novel situations: if the agent encounters a scenario not in its training data, escalate
  • High-stakes decisions: any action that affects money, data, or user accounts requires human approval

Escalation isn't failure—it's a feature. The agent hands off with full context, so the human can pick up where it left off.

Testing agents

Traditional unit tests don't work for agents because they're non-deterministic. We use:

  • Scenario-based testing: predefined user journeys with expected outcomes
  • Property-based testing: verify invariants (agent never deletes data without confirmation)
  • Chaos testing: inject failures (API timeouts, malformed responses) and verify graceful handling
  • Regression testing: replay production conversations and verify behavior hasn't degraded

The reality check

Agents are powerful but not magic. They work best for well-defined tasks with clear success criteria. They fail at open-ended creative work or tasks requiring nuanced judgment.

Start small. Build agents for specific workflows, not general-purpose assistants. And always have a human in the loop for high-stakes decisions.

Questions? Message us directly — start a WhatsApp chat ↗