Skip to main content

Overview

The LLM reranker allows you to use any supported language model as a reranker. This approach uses prompts to instruct the LLM to score and rank memories based on their relevance to the query. While slower than specialized rerankers, it offers maximum flexibility and can be fine-tuned with custom prompts.

Configuration

Basic Setup

Configuration Parameters

Advanced Configuration

TypeScript (self-hosted)

The TypeScript OSS SDK (mem0ai/oss) ships the LLM reranker under the provider name llm_reranker. It does not reuse the Memory’s main llm instance; it builds its own LLM from the reranker’s own config, defaulting to openai / gpt-4o-mini. Set provider/model/apiKey directly on config, or nest a fully separate config.llm: { provider, config } (its provider/config take priority over the top-level fields, which only backfill values missing from the nested config).
To rerank with a different LLM provider than the Memory’s main llm, nest it under config.llm:

Supported LLM Providers

OpenAI

Anthropic

Ollama (Local)

For providers like Ollama that need extra fields (e.g., ollama_base_url), use the optional nested llm key to pass provider-specific configuration. The nested llm config overrides top-level provider/model/api_key when provided.

Azure OpenAI

Custom Prompts

Default Prompt Behavior

The default prompt asks the LLM to score relevance on a 0.0-1.0 scale:

Custom Prompt Examples

Domain-Specific Scoring

Contextual Relevance

Conversational Context

Custom prompts must include {query} and {document} placeholders. The LLM response should contain a numerical score which is automatically extracted.

Usage Examples

Basic Usage

Batch Processing with Error Handling

Performance Considerations

Speed vs Quality Trade-offs

Optimization Strategies

Advanced Use Cases

Multi-Step Reasoning

Error Handling and Fallbacks

Best Practices

  1. Use Specific Prompts: Tailor prompts to your domain and use case
  2. Set Temperature to 0: Ensure consistent scoring across runs
  3. Limit Top-K: Don’t rerank too many candidates to control costs
  4. Implement Fallbacks: Always have a backup plan for API failures
  5. Monitor Costs: Track API usage, especially with expensive models
  6. Cache Results: Consider caching reranking results for repeated queries
  7. Test Prompts: Experiment with different prompts to find what works best

Troubleshooting

Common Issues

Inconsistent Scores
  • Set temperature to 0.0
  • Use more specific prompts
  • Consider using multiple calls and averaging
API Rate Limits
  • Implement exponential backoff
  • Use cheaper models for high-volume scenarios
  • Add retry logic with delays
Poor Ranking Quality
  • Refine your custom prompt
  • Try different LLM models
  • Add examples to your prompt

Next Steps

Custom Prompts Guide

Learn to craft effective reranking prompts

Performance Optimization

Optimize LLM reranker performance