Reduce Exa AI search API costs by implementing caching, choosing the right search type, and limiting result count per query. Exa charges per search with costs varying by plan tier.
import { LRUCache } from 'lru-cache';
const searchCache = new LRUCache<string, any>({ max: 5000, ttl: 3600_000 }); // 1hr TTL # 5000: 5 seconds in ms
async function cachedSearch(query: string, options: any) {
const cacheKey = `${query}:${options.type}:${options.numResults}`;
const cached = searchCache.get(cacheKey);
if (cached) return cached;
const results = await exa.search(query, options);
searchCache.set(cacheKey, results);
return results;
}
// Typical cache hit rate for RAG: 40-60%, directly reducing search costs by half
// Don't fetch more results than you'll actually use
const SEARCH_CONFIGS: Record<string, { numResults: number; type: string }> = {
'rag-context': { numResults: 3, type: 'neural' }, // RAG only needs top 3
'research-deep': { numResults: 10, type: 'neural' }, // Research needs more
'fact-check': { numResults: 5, type: 'keyword' }, // Exact match is cheaper
'autocomplete': { numResults: 3, type: 'keyword' }, // Quick suggestions
};
set -euo pipefail
# Neural search: best for semantic/conceptual queries (more expensive)
curl -X POST https://api.exa.ai/search \
-H "x-api-key: $EXA_API_KEY" \
-d '{"query": "best practices for microservices", "type": "neural", "numResults": 5}'
# Keyword search: best for specific terms/names (cheaper, faster)
curl -X POST https://api.exa.ai/search \
-H "x-api-key: $EXA_API_KEY" \
-d '{"query": "RFC 9110 HTTP semantics", "type": "keyword", "numResults": 3}' # 9110 = configured value
// If processing 1000 documents, many will need similar context searches # 1000: 1 second in ms
function deduplicateSearches(queries: string[]): string[] {
const seen = new Set<string>();
return queries.filter(q => {
const normalized = q.toLowerCase().trim();
if (seen.has(normalized)) return false;
seen.add(normalized);
return true;
});
}
// Typical dedup rate: 20-40% for batch processing pipelines
set -euo pipefail
# Check remaining budget and project monthly cost
curl -s https://api.exa.ai/v1/usage \
-H "x-api-key: $EXA_API_KEY" | \
jq '{
searches_today: .searches_today,
monthly_total: .searches_this_month,
monthly_limit: .monthly_limit,
utilization_pct: (.searches_this_month / .monthly_limit * 100),
days_remaining: (30 - (.searches_this_month / .monthly_limit * 30))
}'
| Issue | Cause | Solution |
|---|---|---|
| Monthly limit hit early | Uncached pipeline queries | Add query caching, expect 40%+ savings |
| High cost per useful result | numResults too high |
Reduce to 3-5 for most use cases |
| Cache hit rate low | Highly variable queries | Normalize queries before caching |
| Budget spike from batch job | No search deduplication | Deduplicate queries before batch execution |
Basic usage: Apply exa cost tuning to a standard project setup with default configuration options.
Advanced scenario: Customize exa cost tuning for production environments with multiple constraints and team-specific requirements.