Skip to main content

Web Search Technical Reference

Complete technical reference for crocbot’s web search subsystem. Designed to be exhaustive for maintainers and extensible for adding new providers.

Table of Contents

  1. Architecture Overview
  2. Supported Providers
  3. Configuration Schema
  4. Provider: Brave Search
  5. Provider: Perplexity Sonar
  6. Model Selection Logic
  7. API Key Resolution
  8. Caching Layer
  9. Tool Parameters
  10. Response Formats
  11. Error Handling
  12. Adding a New Provider

Architecture Overview

Source Files


Supported Providers


Configuration Schema

Full TypeScript definition from src/config/types.tools.ts:

JSON Config Example (Complete)


Endpoint

Authentication

Request Parameters

Freshness Parameter

Validation regex: /^(\d{4}-\d{2}-\d{2})to(\d{4}-\d{2}-\d{2})$/

Response Type

API Key Sources

Resolution order:
  1. tools.web.search.apiKey (config)
  2. BRAVE_API_KEY (environment variable)

Notes

  • Use the “Data for Search” plan, NOT “Data for AI”
  • Free tier available at https://brave.com/search/api/
  • freshness parameter is Brave-exclusive (returns error for Perplexity)

Provider: Perplexity Sonar

Endpoints

Full endpoint: {baseUrl}/chat/completions

Authentication

Request Body

Response Type

Available Models

API Key Sources

Resolution order (with source tracking):
  1. tools.web.search.perplexity.apiKey (config) → source: "config"
  2. PERPLEXITY_API_KEY (env) → source: "perplexity_env"
  3. OPENROUTER_API_KEY (env) → source: "openrouter_env"

Model Selection Logic

Current Behavior

The model is statically configured. There is no automatic selection based on query complexity.

What This Means

The agent does NOT automatically escalate to sonar-reasoning-pro for complex queries.

Manual Override Options

Users who want deep research must either:
  1. Configure globally: Set tools.web.search.perplexity.model to "perplexity/sonar-reasoning-pro"
  2. Create multiple agents: Configure different agents with different models
  3. Future enhancement: Implement query analysis to auto-select model (see Adding a New Provider)

API Key Resolution

Brave

Perplexity (with Base URL inference)

Base URL Resolution Matrix


Caching Layer

Constants

Cache Key Format

Keys are normalized: normalizeCacheKey(key).trim().toLowerCase()

Cache Entry Structure

Eviction Policy

  • TTL-based expiration: Entries older than cacheTtlMinutes are removed on read
  • LRU eviction: When cache reaches 100 entries, oldest entry is removed before insert

Cache Response Flag

Cached responses include cached: true in the result payload.

Tool Parameters

Schema Definition

Parameter Defaults


Response Formats

Brave Response

Perplexity Response

Cached Response

Any response can include "cached": true if served from cache.

Error Handling

Missing API Key

Invalid Freshness

Unsupported Freshness (Perplexity)

API Errors


Adding a New Provider

Step 1: Update Provider List

In src/agents/tools/web-search.ts:

Step 2: Add Config Types

In src/config/types.tools.ts:

Step 3: Add API Key Resolution

Step 4: Add Provider Handler

Step 5: Update Dispatcher

In runWebSearch():

Step 6: Update Cache Key

Step 7: Add Missing Key Error

Step 8: Update Description

Step 9: Write Tests

In src/agents/tools/web-search.test.ts:

Step 10: Update Documentation

  1. Add docs/tools/newprovider.md
  2. Update docs/tools/web.md with new provider option
  3. Update this technical reference

Quick Reference

Environment Variables

Defaults Summary

Config Paths


TODO / Future Enhancements

  • Dynamic model selection: Analyze query complexity to auto-select Perplexity model
  • Hybrid search: Combine Brave structured results with Perplexity synthesis
  • Google Custom Search: Add as alternative traditional search provider
  • Bing Search: Add as alternative traditional search provider
  • SearXNG: Add self-hosted meta-search option
  • Query rewriting: Improve search quality with query expansion

Last updated: 2026-02-01