# Annoy

Annoy is an open-source C++ library with Python bindings for approximate nearest neighbor search, developed by Erik Bernhardsson at Spotify to power music recommendations.

Annoy (Approximate Nearest Neighbors Oh Yeah) is an open-source C++ library with Python bindings for approximate nearest neighbor search. It was created by Erik Bernhardsson while working at Spotify to power music recommendations, where it finds similar tracks or artists based on embedding vectors. Annoy is designed for large-scale, read-only datasets and is known for its simplicity, speed, and memory efficiency, making it a popular choice for [machine-learning](https://www.wikiprompt.org/wiki/machine-learning) applications that require fast similarity search.

The library builds a forest of random projection trees, where each tree partitions the data space using hyperplanes. At query time, Annoy traverses multiple trees to collect candidate points and then scores them to return the approximate nearest neighbors. This approach trades a small amount of accuracy for significant gains in speed and scalability, particularly for high-dimensional vectors. Annoy supports several distance metrics, including Euclidean distance, Manhattan distance, cosine similarity, and dot product, and it can be used from C++, Python, and other languages via bindings.

## History and Development

Annoy was first released in 2013 by Erik Bernhardsson, who was then an engineer at Spotify. The project originated from the need to handle millions of audio tracks and provide real-time recommendations. Bernhardsson open-sourced the library in 2014, and it quickly gained traction in the [artificial-intelligence](https://www.wikiprompt.org/wiki/artificial-intelligence) community. The name "Annoy" is a playful acronym for "Approximate Nearest Neighbors Oh Yeah." The library has been maintained by Bernhardsson and other contributors, with its latest stable release being 1.17.3 in 2023. It is hosted on GitHub and is available under the Apache 2.0 license.

## Technical Approach

Annoy's core algorithm is based on random projection trees. During the build phase, the library creates multiple trees by recursively splitting the data at the median along a randomly chosen hyperplane. Each split is determined by two randomly selected points from the current subset, and the hyperplane is the perpendicular bisector of the line segment connecting them. This process continues until each leaf contains at most a specified number of points (default 10). The resulting forest of trees is stored on disk, allowing for memory-mapped loading, which enables multiple processes to share the same index without duplicating memory.

At query time, Annoy traverses each tree from the root to a leaf, collecting the points in the leaf as candidates. It then computes the exact distances from the query point to all candidates and returns the top-k nearest neighbors. The number of trees to search is a parameter that controls the trade-off between speed and accuracy: more trees yield better recall but slower queries. Annoy also supports a "search_k" parameter that limits the number of nodes visited, providing finer control over performance.

## Usage and Integration

Annoy is widely used in production systems, particularly in recommendation engines and information retrieval. At Spotify, it was used to power the "Discover Weekly" playlist feature, which recommends new music based on user listening history. The library is also employed in various [deep-learning](https://www.wikiprompt.org/wiki/deep-learning) pipelines for tasks such as image retrieval, document similarity, and [neural-network](https://www.wikiprompt.org/wiki/neural-network) embedding search. Its simplicity and lack of external dependencies make it easy to integrate into existing projects. Annoy provides a straightforward API: you build an index by adding items and then call `build(n_trees)`, and for queries you use `get_nns_by_vector` or `get_nns_by_item`. The library also supports incremental addition of items, though the index must be rebuilt to incorporate new data.

## Comparison with Other Libraries

Annoy is one of several approximate nearest neighbor libraries, each with different strengths. Compared to libraries like FAISS (from Facebook AI Research) and HNSW (Hierarchical Navigable Small World graphs), Annoy is often simpler to use and requires no training phase. However, it may have lower recall for a given speed compared to HNSW, which uses a graph-based approach. FAISS offers GPU acceleration and more advanced indexing structures, but it is heavier and more complex. Annoy's memory-mapped files make it particularly suitable for large datasets that exceed RAM, as it can load the index on demand. This feature is less common in other libraries, making Annoy a preferred choice for read-only, large-scale deployments.

## Impact and Legacy

Annoy has had a significant impact on the field of similarity search and has been cited in numerous research papers. It has inspired other projects and has been used as a baseline in benchmarking studies. The library's design influenced later developments in [generative-ai](https://www.wikiprompt.org/wiki/generative-ai) and [large-language-model](https://www.wikiprompt.org/wiki/large-language-model) applications, where efficient retrieval of relevant vectors is crucial for tasks like semantic search and memory augmentation. Annoy remains a relevant tool in the [artificial-intelligence](https://www.wikiprompt.org/wiki/artificial-intelligence) ecosystem, and its codebase is a valuable resource for learning about approximate nearest neighbor algorithms.

## See Also

- [machine-learning](https://www.wikiprompt.org/wiki/machine-learning)
- [deep-learning](https://www.wikiprompt.org/wiki/deep-learning)
- [neural-network](https://www.wikiprompt.org/wiki/neural-network)
- [generative-ai](https://www.wikiprompt.org/wiki/generative-ai)
- [large-language-model](https://www.wikiprompt.org/wiki/large-language-model)

---
Source: https://www.wikiprompt.org/wiki/annoy
License: CC BY-SA 4.0 (https://creativecommons.org/licenses/by-sa/4.0/)
Last updated: 2026-09-12T22:22:13.013955+00:00
