# What is TF-IDF?

> TF-IDF weights a word by how often it appears in a document and how rare it is across the collection. BM25 is a refinement of it.

Canonical: https://promptlyfound.com/glossary/tf-idf
Updated: 2026-12-01

## Short answer

TF-IDF is **a word weight that multiplies how often a word appears in a document by how rare the word is across the collection**. It stands for term frequency, inverse document frequency. A word that is frequent in one document and rare elsewhere gets a high weight, and a word found in almost every document gets almost none. BM25, a common keyword ranking function, is a refinement of TF-IDF.

## Key findings

- Introduction to Information Retrieval defines the tf-idf weight of a term in a document as its term frequency multiplied by its inverse document frequency. The inverse document frequency is the log of the number of documents divided by the number that contain the term. [1]
- The weight is highest when a term occurs many times within a small number of documents, and lowest when the term occurs in virtually all documents. [1]
- In 2020, the Dense Passage Retrieval paper called sparse models such as TF-IDF and BM25 the de facto method for passage retrieval in open-domain question answering. [4]

TF-IDF scores how much a word says about a document. It is one of the oldest ideas in keyword search, and the BM25 ranking that AI retrieval systems still run is a refinement of it.

## How is TF-IDF calculated?

TF-IDF multiplies two numbers for each word in each document. The [tf-idf chapter of Introduction to Information Retrieval](https://nlp.stanford.edu/IR-book/html/htmledition/tf-idf-weighting-1.html), a Cambridge University Press textbook, defines them as follows:[1]

- **Term frequency (tf)** is the number of times the word occurs in the document.
- **Inverse document frequency (idf)** is log(N / df), where N is the number of documents in the collection and df is the number of documents that contain the word.

The textbook's [section on inverse document frequency](https://nlp.stanford.edu/IR-book/html/htmledition/inverse-document-frequency-1.html) explains the second number. The idf of a rare term is high, and the idf of a frequent term is likely to be low. The base of the logarithm does not change the ranking.[2]

Take a collection of 1,000,000 documents and base-10 logarithms. A word that appears in 10 documents has an idf of log(100,000), which is 5. If it occurs 3 times in a page, its tf-idf weight there is 15. A word that appears in all 1,000,000 documents has an idf of log(1), which is 0, so its weight is 0 in every document.

## What does TF-IDF do in search ranking?

TF-IDF lets rare, specific words decide a match. Microsoft's page on [full text search in Azure AI Search](https://learn.microsoft.com/en-us/azure/search/search-lucene-query-architecture) gives an example. In an index of all Wikipedia articles, for the query "the president", documents that match on "president" are more relevant than documents that match on "the".[3]

The textbook also treats each document as a vector, with one tf-idf weight for each word in the dictionary and 0 for the words it does not contain.[1] Search systems compare such vectors with [cosine similarity](https://promptlyfound.com/glossary/cosine-similarity). The document frequency comes from the dictionary of the [inverted index](https://promptlyfound.com/glossary/inverted-index).

## What is the difference between TF-IDF and BM25?

BM25 is a refinement of TF-IDF. [Anthropic's post on Contextual Retrieval](https://www.anthropic.com/news/contextual-retrieval) says TF-IDF measures how important a word is to a document in a collection. BM25 also considers document length and applies a saturation function to term frequency, which helps prevent common words from dominating the results.[5] [BM25](https://promptlyfound.com/glossary/bm25) explains the formula.

## Is TF-IDF still used in AI search?

TF-IDF and its refinement BM25 are still in use. The [Dense Passage Retrieval paper](https://arxiv.org/abs/2004.04906) called sparse vector models such as TF-IDF and BM25 the de facto method for passage retrieval in open-domain question answering in 2020.[4] Anthropic's description of a standard RAG pipeline creates TF-IDF encodings and embeddings for each chunk, then uses BM25 for exact matches and embeddings for meaning.[5]

[What is semantic search?](https://promptlyfound.com/ai-search/semantic-search) compares keyword weighting of this kind with search by embeddings.

## Sources

1. Tf-idf weighting. Introduction to Information Retrieval, Cambridge University Press. https://nlp.stanford.edu/IR-book/html/htmledition/tf-idf-weighting-1.html
2. Inverse document frequency. Introduction to Information Retrieval, Cambridge University Press. https://nlp.stanford.edu/IR-book/html/htmledition/inverse-document-frequency-1.html
3. Full text search in Azure AI Search. Microsoft Learn. https://learn.microsoft.com/en-us/azure/search/search-lucene-query-architecture
4. Dense Passage Retrieval for Open-Domain Question Answering. arXiv, 2020-04-10. https://arxiv.org/abs/2004.04906
5. Introducing Contextual Retrieval. Anthropic, 2024-09-19. https://www.anthropic.com/news/contextual-retrieval

---

This is a plain-text copy of https://promptlyfound.com/glossary/tf-idf from Promptly Found. The web page is the canonical version and includes the charts.
