# Annoy

> Approximate nearest neighbor search with memory-mapped, shareable index files

- Category: Retrieval & Knowledge (RAG) / Vector Databases
- Language: Python
- Repository: https://github.com/spotify/annoy
- Canonical page: https://ai-tldr.dev/tools/annoy/

## In short

Annoy (Approximate Nearest Neighbors Oh Yeah) is a C++ library with Python bindings, built at Spotify, that finds points in a vector space close to a given query point. It sits in the Vector Databases category of the Retrieval & Knowledge (RAG) tools and is open-source, written in Python. To get started: install from PyPI.

## Overview

Annoy (Approximate Nearest Neighbors Oh Yeah) is a C++ library with Python bindings, built at Spotify, that finds points in a vector space close to a given query point. It builds a forest of trees over your vectors and answers approximate nearest-neighbor queries quickly.

What sets it apart is the index format: Annoy writes a read-only file that is memory-mapped (mmapped) into memory, so many processes can share the same data with a small footprint. Building the index is separated from loading it, so you build once and then pass the file around to load instantly in production, batch jobs, or other services.

It fits the embedded / in-process vector search category: there is no server to run. You add your vectors, build the index, save it to disk, and load it directly inside your application. Spotify uses it for music recommendations over millions of high-dimensional track and user vectors where memory usage matters.

## What it does

- Supports angular (cosine), Euclidean, Manhattan, Hamming, and dot (inner) product distance metrics
- Indexes are static files that are mmapped into memory, so multiple processes can share one index
- Index creation is decoupled from lookup: build once, then load the file quickly anywhere
- Small memory footprint, designed for large high-dimensional datasets
- Can build the index directly on disk so datasets that do not fit in RAM can still be indexed
- Native Python bindings plus a header-only C++ library you include with annoylib.h

## Getting started

Install Annoy from PyPI, then build an index, save it to disk, and load it to run nearest-neighbor queries.

1. Install from PyPI

   Pull the latest Python package down from PyPI. For C++, clone the repo and include the header instead.

```bash
pip install --user annoy
```

2. Build and save an index

   Create an AnnoyIndex with a vector length and metric, add your items, build a forest of trees, and save the result to a file.

```python
from annoy import AnnoyIndex
import random

f = 40  # Length of item vector that will be indexed

t = AnnoyIndex(f, 'angular')
for i in range(1000):
    v = [random.gauss(0, 1) for z in range(f)]
    t.add_item(i, v)

t.build(10) # 10 trees
t.save('test.ann')
```

3. Load and query

   Loading just mmaps the saved file, so it is fast. Then ask for the nearest neighbors of an item.

```python
u = AnnoyIndex(f, 'angular')
u.load('test.ann') # super fast, will just mmap the file
print(u.get_nns_by_item(0, 1000)) # find the 1000 nearest neighbors
```

## When to use it

- Serving item or user recommendations from precomputed embedding vectors, as Spotify does for music
- Sharing one read-only index across many processes or workers to keep memory usage low
- Distributing a prebuilt index file to production servers, Hadoop, or batch jobs that load it on startup
- Indexing a dataset too large for RAM by building the Annoy index directly on disk
