Building LLM Agents That Don't Hallucinate in Production
Jawad
AI Engineer
12 June 2025
9 min readEvery AI agent system we've deployed has gone through the same arc: impressive demo, confident launch, and then the edge cases start arriving. The LLM calls the wrong tool. It loops. It generates plausible-looking output that's factually wrong. The problem isn't the LLM — it's the architecture around it. Reliable agents require as much engineering discipline as any other production system.
Tool Design: The Biggest Lever on Reliability
Most agent failures trace back to tool design problems, not model problems. LLMs call tools in ways the developer didn't anticipate — with unexpected argument combinations, out-of-range values, or in the wrong sequence. The fix is defensive tool design, not model tuning.
- Validate every argument at the tool boundary: Treat tool calls like untrusted API inputs. Validate types, ranges, and allowed values before executing. Return structured errors that the agent can reason about.
- Make tools idempotent: Agents retry on failure. Non-idempotent tools (charge payment, send email, delete record) called twice will cause real problems. Use idempotency keys or check-then-act patterns.
- Narrow tool scope: A tool that does 'everything related to users' is an invitation for unintended side effects. Split into read tools (getUserById, listUsers) and write tools (updateUserEmail, deactivateUser) with explicit separation.
- Return structured results, not prose: Return { success: true, userId: '123', changes: ['email'] } not 'I successfully updated the user's email address to...' Structured results let you validate outcomes programmatically.
Guardrails That Actually Work
⚠️ Confidence scores are not guardrails
Many teams add 'only proceed if confidence > 0.8' checks. LLMs don't have reliable confidence calibration — they hallucinate confidently. Real guardrails are structural: human approval gates for irreversible actions, maximum iteration counts, explicit scope definitions.
- Human-in-the-loop for destructive actions: Any action that's hard to reverse — send email to 1000 users, delete records, process payments — requires human approval before execution.
- Hard iteration limits: Agents can loop indefinitely if given the wrong goal or tools. Hard stop at N iterations and surface the state to a human.
- Structured output validation: Use Zod or JSON Schema to validate every tool call output before passing it to the next step. Parse, don't trust.
- Audit logs for every action: Maintain an immutable audit log of every tool call, its arguments, and its result. When something goes wrong, you need to reconstruct the full agent trajectory.
6
Production agent systems deployed
94%
Task completion rate (post-guardrails)
72%
Task completion rate (pre-guardrails)
0
Irreversible incidents after human gates
Tags
You might also like
Work with us
Ready to build your product?
We help product teams across the UK, Netherlands, Australia, and North America ship faster without compromising quality. Let's talk about your project.
Talk to our team →
