Best Practices

Optimize your queries for speed, accuracy, and cost-effectiveness.

Quick Wins

1. Choose the Right Mode

Don't always use mix mode:

// ❌ BAD - Slow and expensive for simple queries
await query({ query: 'Alice', mode: 'mix' });

Do match mode to query type:

// ✅ GOOD - Fast for entity lookups
await query({ query: 'Alice', mode: 'local' });

// ✅ GOOD - Fast for keywords
await query({ query: 'API refactor', mode: 'naive' });

// ✅ GOOD - Use mix for complex questions
await query({
  query: 'What did Alice say about the API refactor last week?',
  mode: 'mix',
});

2. Adjust top_k Based on Needs

Don't always use the maximum:

Do start small and increase if needed:

3. Cache Frequently Asked Questions

4. Use Progressive Enhancement

Start fast, upgrade if needed:

Query Mode Selection

Decision Tree

Mode Comparison Table

Mode
Speed
Accuracy
Cost
Best For

naive

⚡⚡⚡

💰

Keywords, simple searches

local

⚡⚡

⭐⭐

💰

"Who/what is X?", entity lookup

global

⚡⚡

⭐⭐

💰

"How does X relate to Y?"

hybrid

⭐⭐⭐

💰💰

General queries, balanced needs

mix

⭐⭐⭐⭐

💰💰💰

Complex questions, accuracy is critical

Real-World Examples

✅ Good Mode Choices

❌ Poor Mode Choices

Parameter Tuning

top_k (Candidates Retrieved)

Purpose: How many candidates to retrieve before reranking

Guidelines:

Example:

chunk_top_k (Final Results)

Purpose: How many results to return to user

Guidelines:

Example:

score_threshold (Quality Filter)

Purpose: Minimum relevance score (0.0-1.0)

Guidelines:

Example:

Performance Optimization

1. Parallel Queries

When querying multiple sources:

2. Request Batching

For multiple queries:

3. Debounce User Input

For search-as-you-type:

4. Prefetch Common Queries

5. Use Streaming for Long Results

Cost Optimization

1. Minimize Reranking

2. Use Appropriate Embedding Models

3. Batch Indexing

Error Handling

1. Graceful Degradation

2. Timeout Handling

3. Retry Logic

Query Optimization Patterns

Pattern 1: Query Expansion

Expand vague queries for better results:

Pattern 2: Query Rewriting

Rewrite natural language to search-friendly format:

Pattern 3: Multi-Step Queries

Break complex queries into steps:

Common Pitfalls

❌ Don't: Always Use Maximum Settings

✅ Do: Match Settings to Needs

❌ Don't: Ignore Error Responses

✅ Do: Handle Errors Gracefully

❌ Don't: Query Without Validation

✅ Do: Validate and Sanitize

Monitoring & Analytics

Track Query Performance

A/B Testing Query Modes

Summary: Quick Reference

Speed Priority

Accuracy Priority

Cost Priority

Next Steps

Last updated

Was this helpful?