Adam Bandel


Search Gateway

Dec 2025
Type: api
Code: 19k lines
Files: 107
Active: Nov 2025 to Dec 2025
Stack:
PythonFastAPISQLiteRedisPlaywrightPrometheus
Tags:
developer-toolsaidataautomation

Overview

Search Gateway is a REST API that puts 27+ search and content extraction providers behind one interface. Applications query Brave, Tavily, DuckDuckGo, arXiv, GitHub, Reddit, YouTube, Wikipedia and the rest with the same request and response schemas, and when a provider fails, the request falls back along a chain to the next one.

The gateway holds the API keys and handles rate limits, cost tracking and caching, so the applications that call it only deal with their own search logic. It is meant for AI agents, developer tools and other applications that need web search and page content with the costs kept under control. My News Aggregator gets its Reddit posts and article text through it.

Screenshots

API Documentation

Metrics Dashboard

Problem

Applications that search the web, fetch articles, query academic papers or extract content from URLs end up integrating several providers, and each one (Brave, Tavily, DuckDuckGo and so on) has its own API, rate limits, pricing and capabilities. Maintaining 10+ of those integrations is a lot of upkeep. Each has its own authentication and schema and needs its own throttling, there is no common fallback when one fails or rate-limits, costs are hard to track across pay-per-use services, and every integration repeats the same caching logic.

Search Gateway handles all of that in one place, with routing by operation, automatic retries, metrics and tracing.

Approach

Stack

Challenges

Outcomes

All 27+ providers answer through the same schemas, fallbacks route around failing providers automatically, and costs are tracked per client, so each client can have its own spending budget.

Providers change their schemas, rate limits and behavior without notice, so the adapters have to be written defensively. The circuit breakers are what let the gateway keep working when a provider goes down.

Implementation Notes

Provider Adapter Pattern

Each provider extends BaseAdapter with standardized retry logic:

class BraveAdapter(BaseAdapter):
    name = "brave"
    base_url = "https://api.search.brave.com/res/v1"
    
    def capabilities(self) -> Dict[str, Any]:
        return {
            "ops": ["search:web", "search:news", "ai:grounding"],
            "filters_supported": ["include_domains", "freshness_days"],
            "options_supported": ["max_results", "safesearch"],
        }
    
    async def search(self, req: SearchRequestModel) -> List[SearchResult]:
        response = await self._request_with_retry(
            "GET", f"{self.base_url}/web/search",
            params={"q": req.query, "count": req.options.max_results}
        )
        return self._transform_results(response.json())

Operation-Based Routing

The selector routes by operation category, not just provider name:

selection = selector.select(
    operation="search:academic",  # Routes to arXiv, Semantic Scholar, OpenAlex
    client_id=x_client_id,
    fallback=True,
)
# Returns prioritized list: ["arxiv", "semantic_scholar", "openalex"]

Rate Limiting with Token Bucket

Per-client, per-provider rate limiting with burst allowance:

class TokenBucket:
    def allow(self) -> bool:
        now = time.monotonic()
        self.tokens = min(self.capacity, self.tokens + elapsed * self.rate)
        if self.tokens >= 1:
            self.tokens -= 1
            return True
        return False

Provider Catalog Configuration

All provider metadata lives in provider-catalog.yaml:

providers:
  brave:
    ops: ["search:web", "search:news", "ai:grounding"]
    limits: { rps: 1, monthly_cap: 2000 }
    pricing_usd:
      "search:web": 0.003
    plans:
      free:
        limits: { rps: 1, monthly_cap: 2000 }
      pro_ai:
        limits: { rps: 50 }
        pricing_usd: { "search:web": 0.009 }

Related Posts

No posts yet.