Back to use cases

    Use case / AI SEARCH / Python 3 / Published

    Build an AI Fact Checker That Finds Sources for Any Claim in Python

    Build an AI fact checker in Python. Find web sources for a claim and read a summary that keeps cited sentences, with optional X search off by default.

    Card for Build an AI Fact Checker That Finds Sources for Any Claim in Python

    This AI fact checker is a small web app for anyone who wants to see the sources behind a claim before repeating it. Paste one claim and get related web sources, each with a link, and a short summary that keeps only sentences tied to a returned source. Summary sentences that judge the claim are hidden. X search is optional and off by default. It helps you find sources; you still read them and decide.

    The problem

    A claim shows up in a chat, a post or an AI answer, and checking it means opening a search engine, reading several pages and looking for who said what. A summary without links is hard to trust, and a list of links without context takes time to read. Most people want the few sources that matter, with the sentence that backs each point.

    What the app does

    • Takes one claim, 8 to 200 characters, with at least two words that carry meaning.
    • Returns deduplicated web sources shown neutrally, with a rank, title link, domain and snippet.
    • Hides summary sentences with judgment or verdict phrasing, then keeps remaining sentences only if every link they cite is a returned source. Verdict sentences and uncited or mis-cited sentences are counted separately. This checks citation destinations, not whether a source establishes a sentence's accuracy.
    • Shows the number of sources returned. With fewer than two displayed sources, it also says "Few sources returned. Read them before you share anything."
    • When X search is enabled, adds posts containing at least two distinct meaningful claim terms, or all terms if the claim has fewer than two. Posts with account mentions or text matching the built-in profanity filter are dropped; authors are shown as "a user".
    • Can also search Wikipedia, and serves the same result as JSON from GET /api/check.

    How it works

    1. Check the input. Blank, too short or junk claims get a 400 before any paid call.
    2. Find web sources. POST /desearch/ai/search with tools: ["web"], result_type: "LINKS_WITH_FINAL_SUMMARY" and count: 10 returns sources plus a summary. The request asks for a neutral summary with citations and without a verdict.
    3. Optionally search X. X search is off by default. When enabled, GET /twitter uses the claim's first 4 key terms, sort: "Top" and count: 10, running in parallel with the web search.
    4. Clean and filter. Sources are deduplicated and shown neutrally. Summary sentences that judge the claim are hidden; remaining sentences are kept only when every link they cite is a returned source. Optional X posts pass the claim-term, profanity and account-mention filters.
    5. Serve. The page and the JSON endpoint share the same code, with a 6-hour cache header so repeat claims are answered from a CDN.

    The code

    The core call, copied from the example's README. The key is read from the DESEARCH_API_KEY env var:

    BASH
    curl -s https://api.desearch.ai/desearch/ai/search \
      -H "Authorization: $DESEARCH_API_KEY" -H "Content-Type: application/json" \
      -d '{"prompt": "Octopuses have three hearts and blue blood", "tools": ["web"], "streaming": false, "result_type": "LINKS_WITH_FINAL_SUMMARY", "count": 10}'

    The response holds search (each source's title, link, snippet and highlights) and completion, a summary that cites sources as [n](url).

    The folder layout:

    • factcheck/core.py: input checks, request bodies, source and text cleanup, verdict sentence filtering, citation checks, X post filters, rate limiter, spend cap, cache and report rendering. No network.
    • factcheck/client.py: the plain-HTTP Desearch client.
    • factcheck/service.py: the /api/check logic shared by the local server and the hosted function.
    • api/check.py, public/index.html, vercel.json: the hosted version.
    • app.py: local server and CLI. test_core.py: offline tests.

    Endpoints

    • POST /desearch/ai/search
    • GET /twitter

    Example output

    Example output for the claim "The Great Wall of China is visible from space with the naked eye":

    Claim: The Great Wall of China is visible from space with the naked eye
    10 sources returned.
      - While some structures like cities and large dams are visible from low Earth orbit, the Wall's size and coloration make it indistinguishable to the naked eye from space.
      (1 uncited summary sentence(s) hidden)
    2 summary sentence(s) that judged the claim were hidden.
     1. Can you see the Great Wall of China from space? | BBC Sky at Night Magazine
        www.skyatnightmagazine.com
        https://www.skyatnightmagazine.com/space-science/can-you-see-great-wall-china-from-space
        One of the biggest is the idea that the Great Wall of China is the only human-made structure that can be seen from space. Is this true?

    The made-up claim in samples/made-up-claim.json returned general science pages. The page says "10 sources returned." A source count does not establish source quality or the claim's accuracy.

    Saved sample run.

    Run it yourself

    Clone desearch-py-examples and open ai-fact-checker. Then run the README's local commands:

    BASH
    export DESEARCH_API_KEY_FILE=/path/to/key         # or DESEARCH_API_KEY, or --key-file
    python3 app.py                                    # web app at http://127.0.0.1:8000

    For a one-off check from the command line:

    BASH
    python3 app.py --cli "Humans only use 10 percent of their brain" --wiki --json out.json --html out.html
    • --wiki (or wiki=1 on /api/check) also searches Wikipedia, with one more call.
    • X search is off by default. Set DEMO_X_ENABLED=1 or DEMO_X_ENABLED=true on the server (case-insensitive). The CLI enables X independently with --x; an API query parameter cannot enable it.
    • --json and --html save the full result as data or as a page.
    • --log-dir runs/today saves each API call with the key removed.

    What it costs

    With X off by default, a check costs $0.004 for one web search, or $0.008 with the additional Wikipedia search. Enabling X adds $0.0015, for $0.0055 per check or $0.0095 with Wikipedia. Checks served from the cache cost nothing. See pricing.

    A normal check takes about 10 to 15 seconds. Three live runs measured ai/search at 9.4 to 10.1 seconds. Allow up to 40 seconds with X enabled. Wikipedia adds a parallel search call, so timing can vary.

    Try it

    Paste a claim and see related web sources and a cited summary. Read the sources to assess the claim. X search is optional and off by default.

    Ideas to extend it

    • Limit sources to sites you trust with include_domains, for example a science magazine and an encyclopedia.
    • Add a browser button that sends the selected text on any page to /api/check.
    • Check every factual sentence in a draft article and collect linked sources for review.
    • Give an agent /api/check as a tool so it attaches sources before it answers.

    Use with AI agents

    Point an MCP client at the hosted Desearch server. Replace the placeholder with a key from the console. Do not commit the key.

    JSON
    {
      "mcpServers": {
        "desearch": {
          "url": "https://mcp.desearch.ai/mcp",
          "headers": {
            "Authorization": "Bearer <key>"
          }
        }
      }
    }

    Prompt you can paste for this example:

    Use Desearch to gather related sources for the claim I give you. Show source links and snippets neutrally. Hide summary sentences that judge the claim, and keep remaining sentences only when every link they cite is a returned source. X search is off by default; include posts only when enabled. Do not decide whether the claim is true. Do not invent sources.

    Build your own example.

    Give your agents access to web and social search. Get an API key to start building, or talk to us about your integration.

    Read docs