# Stop AI Agent Hallucinations: 5 Techniques + Production Patterns - Elizabeth Fuentes, AWS
<!--
ref: @content/youtube/aie/2026-07-11-stop-ai-agent-hallucinations-5-techniques-production-pattern
tags: [AI, agent, hallucinations, engineering, techniques]
triggers: [agent returns incorrect information, user queries lead to misleading responses, AI produces irrelevant answers, agent fails to validate output]
trigger_prompt: When the AI agent consistently provides inaccurate or nonsensical answers, indicating a need for improved reliability and validation mechanisms.
version: 3
updated_at: 2026-10-18T05:25:40.367Z
-->

Elizabeth Fuentes Leon (AWS) fixes agent hallucinations with five code changes, not prompt changes — semantic tool selection to cut the 3,000-token schema tax, GraphRAG/Cypher instead of vector search for counts and aggregates, a three-agent executor/validator/critic Swarm, Python hooks for hard rules, and agentcontrol for soft steering.

**[Watch on YouTube](https://www.youtube.com/watch?v=vJukHCIv7Ck)**

# Stop AI Agent Hallucinations: 5 Techniques + Production Patterns - Elizabeth Fuentes, AWS

*Synthesized from Elizabeth Fuentes Leon on AI Engineer, 2026-07-11.*

Elizabeth Fuentes Leon is a developer advocate at AWS focused on agentic applications, and she demos all five techniques live against a 29-tool travel agent built on Strands Agents, the open-source agent framework AWS maintains.

## Core Thesis
Fuentes Leon's argument is that agent hallucinations are mostly an engineering problem, not a prompting problem — every one of her five fixes is a code change, not a prompt change. Prompts are read by the model as suggestions it may or may not honor; only code executes logic. So instead of writing better instructions, you filter what enters the context window, compute answers instead of sampling them, add a second agent to check the first, and enforce hard rules in Python inside the agent loop.

## Key Insights

- **Tool schemas are a silent token tax.** In Strands, a `@tool`-decorated function's name, description, docstring, and typed parameters get compiled into a schema that ships in the context window on *every* call. Each schema runs roughly 170–200 tokens depending on parameter count; her 29-tool travel agent burns about 3,000 tokens per call just describing tools — before the user's message and before the response.

- **Semantic tool selection cuts that to a fraction.** Fuentes Leon builds a vector database of tool descriptions (embeddings via `sentence-transformers`, which runs locally and free; FAISS as the local store), routes each incoming query through a semantic search, and injects only the top-K=3 most relevant tools. Tool-description token usage drops from thousands to under 300, and accuracy *improves* — with all 29 generic dummy tools visible, the model sometimes picks the wrong one; filtering means near-duplicate tools only appear when the query genuinely matches.

- **Conversations reintroduce the bloat, so you swap tools per turn.** Once memory/chat history is in play, tokens climb every turn. Because Strands exposes the agent loop, you can reach into `agent.tool_registry` inside each invocation, clear the tools, and add the freshly-selected ones — a few lines of code to keep the tool payload minimal across a multi-turn conversation.

- **RAG can't count.** Vector search always returns *something*, even when nothing is relevant, and the agent only ever sees the top-N chunks of the corpus. Ask "what's the average guest rating across all hotels in Paris?" or "how many hotels have a pool?" and the model does arithmetic on the three chunks it happened to retrieve — then presents the estimate as fact. Fuentes Leon's demo shows RAG averaging over two retrieved hotels while the full dataset holds more.

- **GraphRAG computes instead of sampling.** She loads the same documents into Neo4j as a knowledge graph, puts Cypher syntax guidance into the model's context, and gives it one tool: write a Cypher query, run it, return the result. The graph aggregates across the *entire* dataset and returns a verified number — plus far fewer output tokens, because the query does the math rather than the LLM reasoning through it. The unfair-advantage detail: Neo4j's `SimpleKGPipeline` in its knowledge-graph library will use an LLM to build the graph from a folder of raw `.txt` files, so you don't hand-model the schema.

- **The Antarctica test.** Asked about hotels in Antarctica (there are none), the RAG agent produces a long hedging answer full of wasted tokens. The GraphRAG agent's Cypher query returns zero rows, so it says plainly that there are no hotels listed in Antarctica. Structured retrieval gives the agent a defensible "I found nothing."

- **A single agent validates its own output in the same loop — so it rationalizes failure.** A tool call errors, and the agent generates a confident success message anyway; the user never sees the error. Fuentes Leon's fix uses Strands' built-in `Swarm` class to chain three agents — an **executor** (does the work), a **validator** (reviews what the executor did, outputs a strict verdict), and a **critic** (approves or rejects) — with the executor as entry point, handoffs managed automatically, and a handoff cap (she sets 6). Booking a hotel that doesn't exist: the single agent returns "success"; the swarm's executor hits the error, the validator flags the hallucination, the critic rejects, and the user sees a clear failure.

- **Rules in the prompt are suggestions; rules in code are constraints.** Write "maximum 10 guests per reservation" in the system prompt and the tool description, and the agent still calls the tool with 15. The model processes prompt text probabilistically, not as logic it must execute. Neuro-symbolic guardians move the rule into Python via Strands **hooks** — `HookProvider`, `HookRegistry`, and `BeforeToolCallEvent`, which fires right before any tool executes and lets you inspect parameters and cancel the call.

- **Her guardian rule set is concrete.** Validate dates (check-in must precede check-out); max 10 guests per booking; payment must be verified before confirmation; no cancellation within 48 hours of check-in. Same model, same tools, same prompt in both agents — the *only* difference is the line registering the hook, and that line is what flips the comparison table from wrong to correct.

- **Hooks are all-or-nothing; sometimes you want to steer, not stop.** A room fits four but a party of six could take two rooms; a flight is full but the next one has seats. Blocking leaves the user retrying. Fuentes Leon uses the open-source **agentcontrol** library (an agent-control plugin that captures agent events and ships them to a local control server, plus a steering handler that carries the server's decision back to the model). She registers rules like *steer_max_guests* ("reduce the guest count when it exceeds 10") and *deny_no_payment*. Asked to book for 50 guests, the hook agent blocks; the steered agent splits the reservation across two rooms on its own and completes the task.

- **The operational difference is deployment, not just behavior.** Changing a hook rule means changing code and redeploying the whole agent. agentcontrol rules live on a server behind an API — you update them and the agent picks them up on the next call, no redeploy.

- **The production path is managed versions of the same primitives.** Amazon Bedrock AgentCore supplies the runtime (framework-agnostic, not just Strands), **AgentCore Gateway** (register tools once; it builds the index and does semantic tool routing for you, routing calls to Lambda functions as tools — the same principle, no infrastructure to manage), short- and long-term memory, and CloudWatch observability. AgentCore's policies service enforces rules in code before tools run at the structural level — the managed twin of her hooks. Steering rules live in DynamoDB, so edits go live on the next call. For the graph, Neo4j AuraDB is a hosted option with a free tier.

## Actionable Takeaways

1. **Measure your tool-schema tax first.** Count how many tools your agent exposes and multiply by ~170–200 tokens per schema. At 29 tools, that's ~3,000 tokens shipped on every single call, used or not — Strands lets you read the in/out token counts directly from the agent.

2. **Build a tool vector database and inject top-K=3.** Embed each tool's description with `sentence-transformers` (local, free), store in FAISS, run every incoming query through the semantic search *before* invoking the agent, and pass only the returned tools. Target: tool-description tokens under 300 per call.

3. **Swap tools per turn once you add memory.** Inside the Strands agent loop, clear `agent.tool_registry` and register the newly-selected tools on each invocation — otherwise conversation history plus accumulating tools drags you back toward the full 29.

4. **Route aggregation/count/multi-hop questions to a graph, not a vector store.** Build the knowledge graph with Neo4j's `SimpleKGPipeline` (it uses an LLM to construct the graph straight from your raw text files), give the model one tool that writes and runs a Cypher query, and put Cypher-syntax guidance in the context. Let the query do the arithmetic instead of the LLM.

5. **Wrap high-stakes actions in a three-agent Strands `Swarm`:** executor → validator ("review what the executor did, output exactly one verdict") → critic (approve/reject). Set the executor as entry point and cap handoffs (she uses 6). This is what catches the fabricated-success confirmation.

6. **Put hard rules in Python hooks, never in the prompt.** Implement a `HookProvider`, register on `BeforeToolCallEvent`, inspect the tool parameters, and cancel the call when a rule fails. Use this for genuinely non-negotiable constraints: date ordering, guest ceilings, payment-before-confirmation, cancellation windows.

7. **Use agentcontrol for soft rules where you'd rather steer than stop.** Register steering rules (e.g., reduce guest count over 10) and deny rules (e.g., block confirmation without payment) on the local control server via API; the agent self-corrects — splitting a 50-guest booking across rooms — and finishes the task without a hard stop. Rule changes need no redeploy.

8. **Rule of thumb she leaves you with:** hooks for hard constraints, agentcontrol for soft rules.

9. **Take it to production with Bedrock AgentCore.** Runtime for the agent (any framework), Gateway for automatic tool routing, memory, CloudWatch observability, DynamoDB-backed steering rules, and Neo4j AuraDB (free tier) if you want the graph hosted. Deploy via the repo's notebook or via CDK — both options are provided. Work the demos in order, one through five.

## Notable Quotes

> "Each one is a code change, not a prompt change." — Elizabeth Fuentes Leon

> "Prompts probably are suggestions, not constraints. The model process them as a text. Not as a logic it has to execute." — Elizabeth Fuentes Leon

> "A rule in the prompt the model read it as a suggestion. A rule in the code the model can not escape it." — Elizabeth Fuentes Leon

> "Vector search always returns something even when nothing is truly relevant." — Elizabeth Fuentes Leon

> "The agent acts and validate its own output in the same loop. There's no separation, no second opinion." — Elizabeth Fuentes Leon

> "Same model, same tools, same prompt. And the different outcome because the rules are in Python, not in the prompt." — Elizabeth Fuentes Leon

> "Use hooks for hard constraints, agent control for soft rules." — Elizabeth Fuentes Leon

---

*Related: `@content/youtube/sirio-berati/i-built-a-6-node-ai-workflow-here-s-what-s-actually-happening` · `@content/youtube/tiago-forte/where-to-focus-your-career-as-ai-changes-everything` · `@content/youtube/hormozi/why-ai-won-t-make-you-rich-in-2026` · `@protocol/onboarding` · `@content/youtube/yc-root-access/2026-04-09-this-startup-is-automating-america-s-biggest-hospitals`*

---

*You're reading a protocol on [protocol.supply](https://protocol.supply) — hosted, searchable knowledge for AI agents. Think Agent Skills, as an API: bootstrap once (`curl -sL https://api.protocol.supply/bootstrap`), search (`/search?q=…`), fetch (`/@repo/slug`).*

*Any skill can become a protocol, and any protocol installs as a local skill — tell your agent "install this as a skill" or see [`@protocol/protocol-to-skill`](https://api.protocol.supply/@protocol/protocol-to-skill).*