Skip to content

Conversation

@chindris-mihai-alexandru
Copy link

@chindris-mihai-alexandru chindris-mihai-alexandru commented Nov 26, 2025

Summary

This PR implements a multi-provider search system with automatic fallback for DeepResearch. It provides a 4-provider fallback chain optimized for quality and reliability.

Fallback Chain

  1. Exa.ai (primary) - Best quality, semantic/neural search ($10 free credits)
  2. Tavily (2nd) - Purpose-built for RAG/LLMs (1,000 free requests/month)
  3. Serper.dev (3rd) - Google SERP results (2,500 free queries)
  4. DuckDuckGo (final) - Free forever, always available

The system automatically falls back to the next provider when:

  • API key is not configured
  • Rate limit is hit (429)
  • API errors occur (401, 402, 432, 433)
  • Credits/quota exhausted

Why This Approach?

Scenario Old (Single Provider) New (Fallback Chain)
Exa credits run out Search fails Falls back to Tavily
Tavily quota exceeded Search fails Falls back to Serper
Serper rate limited Search fails Falls back to DuckDuckGo
No API keys configured Cannot use DuckDuckGo works
All providers fail Crash Clear error message

Provider Comparison

Feature Exa.ai Tavily Serper.dev DuckDuckGo
Search Type Neural/semantic RAG-optimized Google SERP Keyword
Quality Excellent Excellent Good Acceptable
API Key Required Yes Yes Yes No
Free Tier $10 credits 1,000/month 2,500 queries Unlimited
Best For Semantic search LLM apps Google results Fallback

Changes

  • inference/tool_search.py: Complete rewrite with 4-provider fallback
    • search_exa(), search_tavily(), search_serper(), search_duckduckgo()
    • sanitize_query() for input validation
    • format_results() for consistent output
    • Proper error handling for each provider's specific error codes
    • multi_provider_search() orchestrates the fallback chain
  • .env.example: Updated with all provider API keys

Configuration

# .env - configure one or more (DuckDuckGo needs no key)
EXA_API_KEY=your_exa_key        # Best quality (recommended)
TAVILY_API_KEY=your_tavily_key  # RAG-optimized (recommended)
SERPER_KEY_ID=your_serper_key   # Google results
# DuckDuckGo is always available as final fallback

Error Handling

Provider Error Code Meaning Action
Exa 402 Payment required Fall back to Tavily
Tavily 432 Plan limit exceeded Fall back to Serper
Tavily 433 Pay-as-you-go limit Fall back to Serper
All 429 Rate limited Fall back to next
All 401 Invalid API key Fall back to next

Testing

Tested all four paths:

  • Exa primary works with type: "auto" search
  • Tavily fallback works with Bearer token auth
  • Serper fallback works when Exa/Tavily disabled
  • DuckDuckGo fallback works when all APIs disabled
  • Error messages show which providers failed and why

Exa.ai provides AI-native neural search which offers significant
advantages for research agents:

- Semantic understanding: Finds relevant results based on meaning,
  not just keyword matching
- Query optimization: Built-in autoprompt improves query quality
- Direct content retrieval: Can fetch full page text in a single call
- Better for complex queries: Neural embeddings excel at nuanced
  research questions

This change simplifies the codebase by removing the dual search
provider system and standardizing on Exa.ai.
- Add category parameter to filter results (research paper, news, github, etc.)
- Add AI-generated highlights for better content extraction
- Include author information in search results
- Document all available Exa categories in docstrings
Implements multi-provider search with automatic fallback:
- Exa.ai as primary (best semantic search quality)
- Serper.dev as first fallback (Google results)
- DuckDuckGo as second fallback (free, always available)

Fallback triggers: missing API key, rate limit, or API errors.
Each provider has 3 retry attempts before failing over.
@chindris-mihai-alexandru chindris-mihai-alexandru changed the title Replace Serper.dev with Exa.ai for web search Multi-provider search with Exa → Serper → DuckDuckGo fallback chain Nov 28, 2025
- Reorder providers by quality: Exa -> Tavily -> Serper -> DuckDuckGo
- Tavily is purpose-built for RAG/LLMs with 1,000 free requests/month
- Simplify implementation with cleaner function structure
- Remove unnecessary class-based approach for providers
- Update .env.example with TAVILY_API_KEY documentation
@chindris-mihai-alexandru chindris-mihai-alexandru changed the title Multi-provider search with Exa → Serper → DuckDuckGo fallback chain Multi-provider search with Exa → Tavily → Serper → DuckDuckGo fallback chain Nov 28, 2025
- Use Exa type: auto instead of neural for better results
- Switch Tavily to Bearer token auth (per API docs)
- Add Exa 402 (payment required) error handling
- Add Tavily 432/433 quota error handling
- Switch Serper from http.client to requests for consistency
- Add sanitize_query() for input validation
- Add format_results() for consistent output
- Handle DuckDuckGoSearchException
- Add ConnectionError, JSONDecodeError handling
- Better error messages with provider names
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant