Tutorial

How to Ground AI Agents to Prevent Hallucination

Stop AI agent hallucination by grounding with live web search. MCP config for Claude Code and Python agent examples.

AI agents hallucinate outdated APIs, wrong versions, and fabricated data because their training data is months old. Search grounding gives agents live web access to verify facts before responding. Cost: 3-5 searches per task at $0.015-0.025.

Prerequisites

  • Claude Code or Python agent framework
  • Scavio API key
  • Basic understanding of MCP or tool calling

Walkthrough

Step 1: Configure MCP search server

Add Scavio MCP to your .mcp.json for native Claude Code integration.

Step 2: Define search tool for agents

Create a search tool function that the agent can call when uncertain about facts.

Step 3: Add grounding logic

Instruct the agent to search before recommending packages, APIs, or version numbers.

Step 4: Test with known-stale queries

Ask about recent releases to verify the agent searches instead of guessing.

Python Example

Python
import requests, os

H = {'x-api-key': os.environ['SCAVIO_API_KEY'], 'Content-Type': 'application/json'}

def search_ground(query):
    resp = requests.post('https://api.scavio.dev/api/v1/search',
        headers=H, json={'query': query, 'country_code': 'us', 'num_results': 5})
    data = resp.json()
    return [{'title': r['title'], 'snippet': r.get('snippet', '')} for r in data.get('organic_results', [])[:5]]

# Agent uses this before recommending
results = search_ground('next.js latest version 2026')
for r in results:
    print(f"{r['title']}: {r['snippet'][:100]}")

JavaScript Example

JavaScript
async function searchGround(query) {
  const resp = await fetch('https://api.scavio.dev/api/v1/search', {
    method: 'POST',
    headers: {'x-api-key': process.env.SCAVIO_API_KEY, 'Content-Type': 'application/json'},
    body: JSON.stringify({query, country_code: 'us', num_results: 5})
  });
  const data = await resp.json();
  return data.organic_results?.slice(0,5).map(r => ({title: r.title, snippet: r.snippet}));
}
const results = await searchGround('next.js latest version 2026');
results.forEach(r => console.log(r.title));

Expected Output

JSON
Next.js 16.1 Release Notes: Next.js 16.1 introduces...
Next.js Blog: Announcing Next.js 16...
...

Related Tutorials

Frequently Asked Questions

Most developers complete this tutorial in 15 to 30 minutes. You will need a Scavio API key (free tier works) and a working Python or JavaScript environment.

Claude Code or Python agent framework. Scavio API key. Basic understanding of MCP or tool calling. A Scavio API key gives you 250 free credits per month.

Yes. The free tier includes 250 credits per month, which is more than enough to complete this tutorial and prototype a working solution.

Scavio has a native LangChain package (langchain-scavio), an MCP server, and a plain REST API that works with any HTTP client. This tutorial uses the raw REST API, but you can adapt to your framework of choice.

Start Building

Stop AI agent hallucination by grounding with live web search. MCP config for Claude Code and Python agent examples.