DEV Community

ScaffDev
ScaffDev

Posted on

Build an AI Agent with Web Search in Python — No API Key Needed

Most AI agents can't search the web without expensive API keys from Google or Bing. I built Scaff Search — a free, open search API that any agent can call with zero configuration.

In this tutorial, I'll show you how to build a simple AI agent in Python that can search the web, process results, and answer questions using real-time data.

The Search API

One endpoint, no auth:

curl "https://search.scaff.dev/api/search?q=python+async+tutorial"
Enter fullscreen mode Exit fullscreen mode

Returns JSON with results[], each containing title, url, snippet. That's it.

Step 1: Search Function

import requests

def web_search(query: str, num_results: int = 5) -> list[dict]:
    """Search the web using Scaff Search API."""
    resp = requests.get(
        "https://search.scaff.dev/api/search",
        params={"q": query},
        timeout=10
    )
    resp.raise_for_status()
    return resp.json()["results"][:num_results]
Enter fullscreen mode Exit fullscreen mode

Step 2: Build the Agent Loop

import json

class SearchAgent:
    def __init__(self, llm_client):
        self.llm = llm_client
        self.tools = {
            "web_search": {
                "description": "Search the web for current information",
                "parameters": {"query": "string"},
                "function": web_search
            }
        }

    def run(self, user_query: str) -> str:
        messages = [
            {"role": "system", "content": f"""You are a helpful assistant with web search.
Available tools: {json.dumps({k: v['description'] for k, v in self.tools.items()})}
To search, respond with: SEARCH: <query>
After getting results, synthesize an answer."""},
            {"role": "user", "content": user_query}
        ]

        for _ in range(5):  # max iterations
            response = self.llm.chat(messages)

            if "SEARCH:" in response:
                query = response.split("SEARCH:")[1].strip()
                results = web_search(query)

                formatted = "\n".join(
                    f"- [{r['title']}]({r['url']}): {r.get('snippet', '')}"
                    for r in results
                )
                messages.append({"role": "assistant", "content": response})
                messages.append({"role": "user", "content": f"Search results:\n{formatted}"})
            else:
                return response

        return response
Enter fullscreen mode Exit fullscreen mode

Step 3: Use It

# With any LLM client (OpenAI, Anthropic, local models)
agent = SearchAgent(your_llm_client)

answer = agent.run("What are the latest Python 3.13 features?")
print(answer)
Enter fullscreen mode Exit fullscreen mode

The agent will:

  1. Decide it needs to search for current info
  2. Call web_search("Python 3.13 new features 2026")
  3. Get real results from Scaff Search
  4. Synthesize a grounded answer

Async Version

For production, use async:

import httpx

async def async_web_search(query: str) -> list[dict]:
    async with httpx.AsyncClient() as client:
        resp = await client.get(
            "https://search.scaff.dev/api/search",
            params={"q": query},
            timeout=10
        )
        return resp.json()["results"]
Enter fullscreen mode Exit fullscreen mode

JavaScript / Node.js

Same API works from any language:

async function webSearch(query) {
  const url = new URL("https://search.scaff.dev/api/search");
  url.searchParams.set("q", query);
  const res = await fetch(url);
  const data = await res.json();
  return data.results;
}

// Use in your agent
const results = await webSearch("latest AI frameworks 2026");
console.log(results.map(r => `${r.title}: ${r.url}`));
Enter fullscreen mode Exit fullscreen mode

Why This Works for AI Agents

  • No API key — no signup, no billing, no rate limit headaches
  • JSON by default — structured data agents can parse directly
  • Fast — responses in 200-400ms
  • Multi-engine — aggregates results from multiple search engines via SearXNG
  • OpenAPI spec — auto-discoverable at search.scaff.dev/openapi.json
  • MCP compatible — works with the Model Context Protocol

Full API Docs

If you're building AI agents, RAG pipelines, or any tool that needs web search — give it a try. It's completely free and running in production.


Previously: I Built a Free Search API for AI Agents

Top comments (0)