# TF-IDF

> Canonical: https://skillalmanac.com/glossary/tf-idf | Last verified: 2026-11-25 | Index status: index-eligible

TF-IDF (term frequency, inverse document frequency) is a way to weight words in a document. A word scores high when it appears often in that document but in few other documents of the collection.

Also called: tf idf, tfidf, term frequency inverse document frequency, tf-idf weighting, tf-idf score.

## In plain language

TF-IDF is a scoring trick for text. Imagine searching a pile of recipes for one that is about saffron. The word the appears everywhere, so it tells you nothing, while saffron appears in only a few recipes, so a recipe that mentions it often is probably about it. TF-IDF multiplies how often a word appears in one document by how rare the word is across all documents. It does not understand meaning: it treats words as separate tokens and cannot tell that two different words mean the same thing.

## Technical definition

TF-IDF is the product of term frequency and inverse document frequency. Wikipedia describes a high tf-idf weight as reached by a high term frequency in the given document combined with a low document frequency across the collection. Scikit-learn defines tf-idf(t,d) = tf(t,d) times idf(t), with a smoothed idf by default and L2 normalisation of each row.

## How it shows up when you use agents

TF-IDF is classic keyword scoring from [natural language processing](/glossary/natural-language-processing). You meet it when an agent searches a folder of documents or a codebase and ranks files by the words they contain. See [BM25](/glossary/bm25) for a related ranking function.

See [Embedding](/glossary/embedding) for the vector alternative; the [semantic search](/glossary/semantic-search) entry explains meaning-based search. In [retrieval-augmented generation](/glossary/retrieval-augmented-generation), the retrieval step can use keyword scoring, embeddings or both, and the [what is RAG](/learn/what-is-rag) guide walks through the flow.

What breaks when TF-IDF is misunderstood: a search for one phrase misses a document that uses different words for the same idea, because TF-IDF matches words. A next step in a Claude Code or Codex session is to ask what ranking method a search tool uses before trusting its results, and to test it with a query you know the answer to. In scikit-learn the method is available as a vectorizer, so a script an agent writes for you may use it without saying so.

## Common confusions

- **TF-IDF versus embeddings.** TF-IDF scores words by counts and rarity. Embeddings place text in a space where similar meanings sit close together.
- **TF-IDF versus BM25.** BM25 is a separate ranking function; see [BM25](/glossary/bm25).
- **Term frequency versus TF-IDF.** Term frequency alone favours common words. The idf part lowers the weight of words found in many documents.
- **Myth: TF-IDF understands what a document is about.** It counts words. It has no model of meaning.

## Worked examples in this catalogue

### [keyword-density-count](https://skillalmanac.com/skills/skill-almanac/keyword-density-count)

Status: illustrative example from the item's own documentation, not executed for this page.

What you see: The skill counts how often each keyword or phrase appears in plain page text and reports case-insensitive, whole-word counts and density figures.

Why it exercises the concept: Counting how often a word appears in one text is the term-frequency half of the idea. The skill has no inverse-document-frequency step, so it shows only half of TF-IDF.

### [duplicate-passage-finder](https://skillalmanac.com/skills/skill-almanac/duplicate-passage-finder)

Status: illustrative example from the item's own documentation, not executed for this page.

What you see: The skill finds exact and near-duplicate paragraphs across a folder of text and Markdown files and reports each group with its file and line number.

Why it exercises the concept: Comparing passages across a set of files is a task where word-level comparison matters. The skill's description does not say it uses TF-IDF, so treat it as an adjacent task.

### [What Is RAG? Retrieval-Augmented Generation Explained](https://skillalmanac.com/learn/what-is-rag)

Status: illustrative example from the item's own documentation, not executed for this page.

What you see: A guide to retrieval-augmented generation, in which an agent looks up passages from your documents and gives them to the model before it answers.

Why it exercises the concept: The guide describes the retrieval step, where an agent looks up passages from your documents before the model answers. TF-IDF is background here, not something the guide names.

## Related terms

- [BM25](https://skillalmanac.com/glossary/bm25.md)
- [Embedding](https://skillalmanac.com/glossary/embedding.md)
- [Semantic search](https://skillalmanac.com/glossary/semantic-search.md)
- [Natural language processing (NLP)](https://skillalmanac.com/glossary/natural-language-processing.md)
- [Vector search](https://skillalmanac.com/glossary/vector-search.md)
- [Retrieval-augmented generation (RAG)](https://skillalmanac.com/glossary/retrieval-augmented-generation.md)

## Sources

- [Feature extraction, Tf-idf term weighting](https://scikit-learn.org/stable/modules/feature_extraction.html), scikit-learn (accessed 2026-11-24)
- [tf–idf](https://en.wikipedia.org/wiki/Tf%E2%80%93idf), Wikipedia (accessed 2026-11-24)

Definitions are written by Skill Almanac from the sources above and checked against them on 2026-11-25. [Glossary index](https://skillalmanac.com/glossary.md).
