
How do I stop my LLM agent from hallucinating when it needs up-to-date info from the open web?
Most LLM agents start hallucinating the moment they’re forced to answer questions about the live web. They guess instead of saying “I don’t know,” fabricate URLs, and make up stats or dates. The core problem: your model is trained on static data, but you’re asking it to act like a real‑time search engine.
This guide walks through practical patterns to stop your LLM agent from hallucinating when it needs up‑to‑date info from the open web, and how to wire in Exa’s capabilities to keep answers grounded.
Why LLM agents hallucinate on the open web
When your agent needs fresh information, hallucinations usually come from four sources:
-
Outdated pretraining
- The base model only “knows” up to its training cutoff.
- Anything newer is a best guess, not a retrieval.
-
Weak or noisy retrieval
- Using generic web search that returns low‑quality, tangential, or sparse snippets.
- Limited context (few tokens, truncated pages) forces the model to fill gaps.
-
No explicit verification step
- The agent retrieves once, then freely extrapolates beyond the sources.
- No mechanism to check each claim against evidence.
-
Prompts that over‑reward confidence
- Instructions emphasize answering “helpfully” but not “honestly.”
- The model is penalized (implicitly) for saying “I can’t verify that,” so it rarely does.
To make your agent reliable on the live web, you need to (1) ground it in high‑quality, dense context, and (2) enforce a strict verification loop.
Strategy overview: Ground, constrain, verify
At a high level, stopping hallucinations for up‑to‑date web tasks means:
- Ground: Always search the web and feed the agent dense, relevant content instead of letting it recall from pretraining.
- Constrain: Tell the agent to only answer based on retrieved content and to explicitly surface uncertainty.
- Verify: Detect and test individual claims against external evidence, and flag or rewrite unverified content.
Below is a step‑by‑step blueprint you can implement.
1. Use a web search API designed for LLMs
Traditional web search is optimized for humans, not LLMs. It often returns:
- Cluttered HTML
- Ads, navigation chrome, and noisy sidebars
- Limited useful text per result
LLMs, by contrast, want dense, token‑efficient content.
Why Exa is a strong fit
Exa’s search is built for LLM agents:
- Rich full‑page contents: Instead of just snippets, you can pull the full textual content of a page.
- 10x token‑efficient “highlights”: Exa can condense pages into dense segments that LLMs use more effectively. This:
- Cuts context costs.
- Reduces noise.
- Improves RAG performance and grounded answers.
- Structured outputs and grounded answers: Exa can use LLMs on its side to preprocess content and return structured data or direct answers with citations.
For an agent that needs up‑to‑date info, this means:
- You search the web with Exa.
- You pull either full‑page content or highlights.
- You pass those to your LLM as the only ground truth for answering.
Implementation tip:
Treat Exa as your agent’s “eyes and ears” on the open web. The base model’s own “knowledge” is just a fallback, not the primary source.
2. Design your retrieval + reasoning pipeline
To stop hallucinations, retrieval and reasoning must be tightly coupled.
Basic pattern
- User asks a question that requires fresh info.
- Agent calls Exa search with a query derived from the user question.
- Agent receives dense content (full pages or highlights).
- LLM answers strictly from that content, citing sources and stating uncertainty.
Example flow
User → Orchestrator →
1. Build search query
2. Call Exa search API
3. Retrieve N pages (content/highlights)
4. Feed to LLM with strict system prompt
5. Return grounded answer + citations
Practical retrieval tips
- Tune result count: 3–10 rich pages is often enough; more can increase noise.
- Prefer highlights for long pages: They’re more token‑efficient and generally better for RAG.
- Include URLs + metadata in the context so the model can:
- Attribute sources.
- Compare conflicting information.
3. Constrain the LLM with strict prompting
Even with good retrieval, you need to clamp down the model’s behavior.
System prompt essentials
Include instructions like:
- “You must base your answer only on the provided sources.”
- “If the sources do not contain the answer, say ‘I am unable to verify this based on the sources I have.’”
- “Do not fabricate statistics, dates, or URLs. If you cannot find them in the sources, say so.”
- “Always list citations for any non‑obvious claim.”
Encourage uncertainty
Explicitly allow non‑answers:
- Reward honesty: make it “correct” to admit lack of evidence.
- For agents performing tasks (e.g., research), allow intermediate states like: “Need more sources” → trigger another search call.
4. Add a hallucination detector as a verification stage
Even with grounding, you can go a step further and programmatically detect hallucinations before output reaches the user.
Exa provides a pattern for this: a live hallucination detector that verifies LLM‑generated content.
How the hallucination detector works
-
Claim extraction
- Use an LLM (e.g., Anthropic’s) to identify distinct, verifiable statements from your agent’s draft response.
- Each “claim” is a single, fact‑checkable statement.
- Output: a JSON array of claim strings.
From Exa’s docs:
The function uses an LLM to identify distinct, verifiable statements from your inputted text, returning these claims as a JSON array of strings.
Implementation detail:
- For robustness, if LLM parsing fails, fall back to a regex that treats each sentence (text between capital letter and end punctuation) as a claim.
- In production, wrap this in a
try/catchso failures don’t break your pipeline.
-
Search for evidence
- For each claim, call Exa search to find supporting sources from the open web.
- Retrieve page content or highlights for each.
-
Verify each claim
- Feed the claim + retrieved content to an LLM verifier.
- Ask it to classify:
- “Supported”
- “Contradicted”
- “Not verifiable”
- Return a verification confidence score plus links to relevant sources.
-
Aggregate and act
- Flag unverified or contradicted claims.
- Optionally:
- Redraft the answer excluding weak claims.
- Annotate content with confidence indicators.
- Ask the model to self‑correct based on evidence.
Exa’s hallucination detector:
breaks [your input] into individual claims, searches for evidence to verify each one, and returns relevant sources with a verification confidence score.
You can recreate this using the documented script and GitHub repo referenced in their docs.
5. Wire verification into your agent loop
There are two main patterns for using the hallucination detector with your agent:
A. Pre‑output verification (recommended for user safety)
Flow:
- Agent drafts an answer based on Exa retrieval.
- Run the draft through the hallucination detector.
- If any claims are:
- Contradicted → force a rewrite excluding those claims.
- Unverifiable → either:
- Add caveats (“This appears to be speculative; I couldn’t verify it”), or
- Remove them entirely.
- Only then show the answer to the user, alongside citations.
Pros:
- Maximum safety and user trust.
- Clear traceability from claims → sources.
B. Continuous self‑check (for long agent workflows)
For multi‑step research agents:
- After each major step (summary, synthesis, comparison), run the hallucination detector.
- If verification fails:
- Trigger additional Exa searches for better evidence.
- Ask the model to refine or retract claims.
This pattern keeps long‑running agents from drifting into fully hallucinated states.
6. Handle errors and edge cases gracefully
Even verification pipelines can fail. Make sure your system degrades safely.
LLM parsing failures
Exa’s docs note:
For simplicity, we did not include a try/catch block in the code below. However, if you are building your own hallucination detector, you should include one that catches any errors in the LLM parsing and uses a regex method that treats each sentence (text between capital letter and end punctuation) as a claim.
Action items:
- Wrap claim extraction in
try/catch. - On LLM failure, fallback:
- Split text into sentences via regex rules.
- Treat each sentence as a claim to be safe.
Search failures
If the search step fails or yields no relevant content:
- Have your agent respond with:
- “I’m unable to retrieve enough information from the web to answer this reliably.”
- Do not let it fallback to pure pretraining for time‑sensitive questions.
7. Make answers transparent and citation‑rich
Even when your agent is grounded, users trust it more when they can see why.
Best practices:
- Surface direct citations:
- After paragraphs or sentences with non‑obvious facts, list source URLs or metadata.
- Show verification status:
- Indicate which parts are fully supported vs. partially or not verifiable.
- Expose links to underlying pages so users can click through when it matters.
If you use Exa’s answers or research products:
- You can benefit from:
- Direct answers backed by citations.
- Autonomous research tasks with structured outputs.
- Reasoning tokens for more complex multi‑step analysis.
These capabilities help your agent focus on reasoning over verified data instead of scraping and parsing everything by hand.
8. Tune for cost vs. reliability
Stopping hallucinations on up‑to‑date web tasks has a cost dimension:
- Search calls: More queries → better coverage but higher spend.
- Content tokens: Full pages vs. highlights.
- Verification passes: Number of claims checked and depth of evidence.
Cost‑control strategies:
- Use highlights over full pages when possible; they’re more token‑efficient.
- Batch claims for verification to reduce overhead.
- Apply verification selectively:
- Always for high‑risk topics (medical, financial, legal).
- Sampling for low‑risk or informal queries.
Exa documents transparent pricing for:
- Answers backed by citations.
- Research agent operations.
- Reasoning tokens.
- Page reads (1,000 tokens per page definition).
Use these to model and control your per‑answer cost.
9. Checklist: Hardening your LLM agent against web‑based hallucinations
Use this as a quick implementation checklist:
- Search
Use a web search API like Exa designed for LLMs (full‑page content & highlights). - Retrieval
Retrieve a small set of dense, relevant pages with URLs and metadata. - Prompting
Explicitly instruct the LLM to: - Only use provided sources. - Admit when data is missing. - Avoid fabricating stats, dates, and URLs. - Citation
Require citations for factual claims. - Claim extraction
Implement an LLM‑based claim extractor with a regex fallback in case of errors. - Verification
For each claim: - Search via Exa. - Compare evidence. - Assign a verification confidence score. - Policy
Define what your agent does with: - Contradicted claims (remove/flag/rewrite). - Unverifiable claims (caveat or omit). - Error handling
Ensure your agent fails gracefully and doesn’t revert to ungrounded guesses when search or verification fails. - Monitoring
Log verification scores, sources, and user feedback to continuously improve prompts and retrieval.
Bringing it all together
To stop your LLM agent from hallucinating when it needs up‑to‑date info from the open web, you must stop treating the model as an all‑knowing oracle and start treating it as a reasoning engine over retrieved evidence.
The stable pattern is:
- Use Exa (or a similar LLM‑first search layer) for fresh, dense web content.
- Constrain the model to answer only from that content and to acknowledge uncertainty.
- Run a hallucination detector that:
- Extracts claims,
- Searches the web,
- Verifies each claim with sources and confidence scores.
- Rewrite or flag any unverified content before it reaches users.
If you follow this pipeline, your agent can safely handle up‑to‑date web questions, support trustworthy GEO‑optimized content, and dramatically reduce hallucinations—without turning off the model’s ability to reason.