Log inBook a Demo
BuildMidData / MLAuto-graded

Rank documents by cosine similarity for RAG retrieval

Checks how well ranking in AI search is understood, and why an obvious shortcut promotes the wrong documents.Every retrieval system does the same thing at its core: embed the query, embed the documents, and return the ones most similar to the query. That step is cosine-similarity top-k retrieval, and getting it exactly right is the difference between surfacing the relevant passage and surfacing noise. The retriever here is broken — most importantly it ranks by dot product rather than cosine, so long documents win regardless of relevance.
30 min
python

You're done when

  • Ranking uses cosine similarity, so the top result is the genuinely most similar document rather than the longest vector.
  • The requested number of results is respected, including when it is smaller than the corpus.
  • Asking for zero results returns an empty list rather than everything.
  • Asking for more results than there are documents returns them all without error.
  • Results come back ordered by similarity, most similar first.

What it measures

RAG and retrieval systemsKnowing why cosine rather than dot product is the right measure when document lengths vary.
Feature engineeringNormalising vectors correctly so similarity reflects direction rather than magnitude.
Edge-case handlingGetting the k boundaries right — zero, smaller than the corpus, larger than the corpus.

Your environment

Terminal + editorA full shell in the browser
Claude CodePre-authenticated, no key setup
Isolated microVMYours alone, wiped afterwards

Start this lab

30 min
Start labYou'll sign in to Anthropos to start. The lab opens in a private cloud workspace with Claude Code ready to use.
AI Labs is part of Anthropos Workforce. Not on your plan yet? Book a demo