Thread Transfer
Designing context-aware AI agents that don't hallucinate
Hallucinations kill trust. Here's how leading teams build agents that stay grounded with structured context and verification loops.
Jorgo Bardho
Founder, Thread Transfer
Hallucinations kill trust. An AI agent can execute 99 tasks flawlessly, but one confidently wrong answer destroys credibility. The fix isn't better prompting—it's context-aware architecture that grounds agents in facts and verifies every claim before acting.
Why agents hallucinate
LLMs are prediction engines, not knowledge databases. They generate plausible text based on patterns, not facts. When an agent lacks grounding, it fills gaps with confident guesses. Common triggers:
- Missing context. If the agent doesn't have the right information, it makes something up rather than admitting uncertainty.
- Ambiguous prompts. Vague instructions leave room for interpretation, and the model picks the most "likely" interpretation—even if wrong.
- Outdated training data. Models don't know events after their training cutoff. Ask about recent news and they'll fabricate details.
- No verification loop. If the agent isn't required to check its work, errors propagate silently.
Architectural patterns for grounding
The best defense is structured context paired with verification guardrails. Here's how leading teams build agents that stay honest:
1. Explicit context injection
Don't rely on the model's training data. Inject the facts it needs directly into every request. Use:
- RAG (Retrieval-Augmented Generation): Fetch relevant documents from a vector database and pass them as context. The agent answers based on retrieved facts, not guesses.
- Structured bundles: Pre-distilled summaries of prior conversations, tickets, or threads. Thread-Transfer bundles are designed for this—they give agents compact, verified context without noise.
- Real-time API calls: Pull live data (CRM records, inventory levels, user profiles) so the agent always works with current information.
2. Verification loops
Never trust the agent's first output. Build verification checkpoints:
- Retrieval verification: After the agent generates a response, re-query the knowledge base to confirm the cited facts exist.
- Citation requirements: Force the agent to cite sources for every claim. If it can't, flag the output as unverified.
- Dual-model validation: Run critical outputs through a second, smaller model trained to detect hallucinations (e.g., fact-checking classifiers).
- Human-in-the-loop: For high-stakes actions (financial transactions, policy changes), require human approval before execution.
3. Constrained output schemas
Free-form text invites hallucination. Constrain the agent's output to structured formats:
- JSON schemas: Define exactly what fields the agent can populate. If it tries to add invented fields, the schema rejects it.
- Enum values: For categorical data (status, priority, region), limit the agent to predefined options.
- Template filling: Give the agent a template with placeholders. It fills blanks with extracted facts but can't invent new structure.
4. Confidence scoring and uncertainty handling
Teach the agent to say "I don't know." Implement:
- Logprobs analysis: Models like GPT-4 expose token-level confidence. Flag low-confidence outputs for review.
- Explicit uncertainty prompts: Instruct the agent to respond with
"UNKNOWN"or"INSUFFICIENT_DATA"when it lacks grounding. - Fallback escalation: If confidence is low, escalate to a human or a more powerful model.
5. Guardrails and safety filters
Layer multiple safety checks:
- Input sanitization: Strip or escape user input that could manipulate the agent (prompt injection attacks).
- Output filters: Block responses containing blacklisted patterns (PII, offensive language, competitor mentions).
- Semantic guardrails: Use a lightweight classifier to detect off-topic or hallucinated outputs before they reach the user.
Real-world example: Customer support agent
A SaaS company built a support agent that resolved 75% of tickets autonomously. Their anti-hallucination stack:
- RAG pipeline: Every query triggers a semantic search against the knowledge base. Top 5 chunks are injected into the prompt.
- Citation requirement: Agent must link every answer to a specific doc URL. If no match, output is
"I couldn't find that in our docs. Let me escalate." - Human handoff: If the agent flags uncertainty or the user explicitly requests it, the conversation + context bundle transfers to a human agent via Thread-Transfer.
- Post-interaction audit: Random sample of agent responses are reviewed weekly. Hallucinations trigger prompt updates.
Takeaways
- Grounding beats prompting. You can't prompt your way out of missing context. Inject facts via RAG, APIs, or bundles.
- Verification is mandatory. Treat every agent output as "guilty until proven innocent." Check, cite, validate.
- Structure reduces risk. Constrain outputs to schemas and enums. Free text = free hallucination.
- Admit uncertainty. An agent that says "I don't know" is more trustworthy than one that guesses confidently.
Hallucinations aren't a model problem—they're an architecture problem. Build context-aware agents with verification loops and structured guardrails, and you'll ship systems people actually trust.
Learn more: How it works · Why bundles beat raw thread history