Grounded Search & Citation Prediction
Fast, cost-effective citation prediction before expensive multi-LLM execution.
Overview
Grounded Search enables AIQ to predict whether a query will generate brand citations before running expensive execution across all 4 LLM providers.
The Problem
Running queries across Claude, GPT-4, Gemini, and Perplexity costs $0.05-0.15 per query. Without knowing if a query will generate citations, you waste budget on low-quality queries.
The Solution
Grounded Search uses Gemini's native Google Search tool to predict citations for $0.0001-0.001 – a 98-99% cost reduction compared to full execution.
Key Benefits
| Before | After | Improvement |
|---|---|---|
| $0.05-0.15 per validation | $0.0001-0.001 | 98-99% cheaper |
| 15-30s (full execution) | 1-3s | 90% faster |
| 60-70% accuracy (text parsing) | 90-95% | +30% more accurate |
How It Works
Three-Tier Provider Strategy
AIQ automatically selects the optimal provider for citation prediction:
Tier 1: Gemini (BEST)
├─ Native Google Search grounding
├─ 95% confidence
└─ $0.000158 per prediction
Tier 2: Perplexity (GOOD)
├─ Native web search + citations
├─ 90% confidence
└─ $0.0006 per prediction
Tier 3: Claude/GPT-4 (FALLBACK)
├─ LLM-simulated citations
├─ 65% confidence
└─ $0.005 per prediction
The system automatically chooses the cheapest provider with native grounding support.
Usage
Basic Prediction
import { CitationPredictionService } from '@/lib/services/citation-prediction-service';
const service = new CitationPredictionService();
const result = await service.predictQueryCitations({
query: 'What are the key features of Salesforce CRM?',
organizationDomain: 'salesforce.com',
organizationId: 'org-123',
});
console.log(result.willCiteBrand); // true
console.log(result.confidence); // 95
console.log(result.cost); // 0.000158
console.log(result.recommendation);
// "HIGH QUALITY: Query likely to generate brand citations..."
Batch Prediction
Test multiple queries at once:
const batchResult = await service.predictBatchCitations({
queries: [
'What is Salesforce?',
'How does Salesforce compare to HubSpot?',
'Salesforce pricing plans',
],
organizationDomain: 'salesforce.com',
organizationId: 'org-123',
});
console.log(batchResult.aggregate.expectedCitationRate); // 85%
console.log(batchResult.aggregate.totalCost); // 0.0003
Provider Capabilities
Gemini (Recommended) ⭐
Method: Native Google Search grounding Confidence: 95% (highest) Cost: $0.075/1M tokens (cheapest)
Gemini uses real Google Search during inference and returns structured groundingMetadata with actual citations.
When to use: Always (if available)
Perplexity (Good Alternative)
Method: Native web search + citations API Confidence: 90% Cost: $1-3/1M tokens (13-40x more expensive)
Perplexity performs real web search and returns native citations[] array.
When to use: Gemini unavailable or testing Perplexity-specific behavior
Claude/GPT-4 (Fallback)
Method: LLM-simulated citation prediction Confidence: 65% (lower - simulation not real search) Cost: $2.50-3/1M tokens (33-40x more expensive)
Uses LLM to simulate which sources it would cite when answering a query.
When to use: No providers with native grounding available
Understanding Results
Prediction Confidence
| Confidence | Meaning | Action |
|---|---|---|
| 90-95% | High confidence (native grounding) | Trust prediction |
| 70-89% | Medium-high (good simulation) | Mostly reliable |
| 60-69% | Medium (simulation) | Consider actual execution |
| < 60% | Low confidence | Run actual execution |
Recommendations
The service provides automatic recommendations:
- "HIGH QUALITY" → Query likely to cite brand, recommended for execution
- "MEDIUM QUALITY" → May cite brand, consider refining
- "LOW QUALITY" → Unlikely to cite brand, recommend regeneration
- "UNCERTAIN" → Low confidence, test with actual execution
Cost Analysis
Per-Query Comparison
| Provider | Prediction Cost | Full Execution Cost | Savings |
|---|---|---|---|
| Gemini | $0.000158 | $0.15 | 99.9% |
| Perplexity | $0.0006 | $0.15 | 99.6% |
| GPT-4 | $0.00525 | $0.15 | 96.5% |
| Claude | $0.0078 | $0.15 | 94.8% |
Monthly Savings Example
Scenario: 1000 queries/month, 20% rejected by prediction
| Metric | Without Grounded Search | With Grounded Search | Savings |
|---|---|---|---|
| Prediction cost | $0 | $0.158 | - |
| Execution cost | $150 (1000 × $0.15) | $120 (800 × $0.15) | $30 |
| Total | $150 | $120.158 | $29.84/month |
Annual savings per organization: ~$358/year 100 organizations: ~$35,800/year
Best Practices
1. Pre-Validate Before Execution
Always predict citations before running expensive multi-provider execution:
// Step 1: Predict
const prediction = await service.predictQueryCitations({
query: userQuery,
organizationDomain: 'example.com',
organizationId: orgId,
});
// Step 2: Only execute if prediction is positive
if (prediction.willCiteBrand && prediction.confidence >= 70) {
// Run expensive multi-provider execution
await executeQuery(userQuery);
} else {
// Save budget - don't execute low-quality query
console.log('Query unlikely to cite brand - skipping execution');
}
2. Batch Validation
For query generation, validate entire batch before execution:
const generated = await generateQueries({ industry: 'SaaS', count: 20 });
const batchPrediction = await service.predictBatchCitations({
queries: generated.queries.map(q => q.text),
organizationDomain: 'example.com',
organizationId: orgId,
});
// Only execute queries with positive predictions
const highQualityQueries = batchPrediction.results
.filter(r => r.willCiteBrand && r.confidence >= 70)
.map(r => r.query);
console.log(`Executing ${highQualityQueries.length} of ${generated.queries.length} queries`);
3. Track Prediction Accuracy
Compare predictions to actual execution results:
const prediction = await service.predictQueryCitations(params);
const execution = await executeQuery(params.query);
const actualCited = execution.citations.some(c =>
c.domain.includes(params.organizationDomain)
);
// Log accuracy for analytics
console.log('Prediction correct:', prediction.willCiteBrand === actualCited);
Troubleshooting
"No providers available"
Cause: Organization has no enabled LLM providers
Solution: Configure enabledProviders in organization settings or set environment variables
"No API key found for provider"
Cause: Missing API key for selected provider Solution:
- Set environment variable (
GEMINI_API_KEY, etc.) - OR configure organization-specific encrypted key
Predictions don't match actual execution
Possible causes:
- Time delay - Web search results change over time
- Provider differences - Different providers index differently
- Simulation error - Claude/GPT-4 simulation less accurate
Solution: Use Gemini grounded search for 95% accuracy
"Prediction confidence is low"
Cause: Using simulated grounding (Claude/GPT-4) Solution: Enable Gemini or Perplexity for higher confidence
Advanced Usage
Multi-Provider Comparison
Test prediction accuracy across providers:
const comparison = await service.compareProviderPredictions({
query: 'Best CRM for small business',
organizationDomain: 'salesforce.com',
organizationId: 'org-123',
providers: ['gemini', 'perplexity'],
});
for (const [providerName, result] of comparison) {
console.log(`${providerName}: ${result.willCiteBrand} (${result.confidence}% confidence)`);
}
Custom Provider Selection
Force specific provider:
const result = await service.predictQueryCitations({
query: 'What is TypeScript?',
organizationDomain: 'typescriptlang.org',
organizationId: 'org-123',
providers: ['perplexity'], // Force Perplexity
});
Related Features
- Query Generation Modes - Quality validation during generation
- Multi-Perspective Testing - Segment predictions by user intent
- Citation Analytics - Track actual execution results
Technical Details
For developers and technical users:
- Implementation:
lib/services/citation-prediction-service.ts - Provider Clients:
lib/api/grounded-search/ - Type Definitions:
types/grounded-search.ts - Documentation:
docs/features/GROUNDED_SEARCH.md