Skip to main content
← CoursesAI Engineering with PythonModule 3 · RAG (Retrieval-Augmented Generation)Rerankingwrite37 / 101
💬 Discuss🧪 Playground+100 XP
Task
📝 **Question:** **Write the function** \`retrieval_cost(strategy, total_docs)\` that returns \`(cost_usd, latency_ms)\` for two strategies, given these unit costs: - **BM25/vector scan** — \`$0.00001\` per doc scored, \`1ms\` per doc - **Rerank (cross-encoder)** — \`$0.001\` per (query, doc) pair, \`50ms\` per pair Two strategies: - \`"rerank-only"\` — rerank EVERY doc in the corpus - \`"two-stage"\` — BM25 scans all docs, then rerank the top **50** candidates Round cost to 4 decimals. Then print the ledger at 100, 10k, and 1M docs: \`\`\` 100 docs | rerank-only $ 0.1000 5000ms | two-stage $ 0.0510 2600ms 10000 docs | rerank-only $ 10.0000 500000ms | two-stage $ 0.1500 12500ms 1000000 docs | rerank-only $1000.0000 50000000ms | two-stage $10.0500 1002500ms \`\`\` At 1M docs, rerank-only would cost **$1000** and take **14 hours** per query. Two-stage gets you to ~$10 and 17 minutes — and the quality lift is comparable because BM25 already pre-filtered the obvious junk. That gap is why every production search engine uses the funnel. 📋 Pick the right answer. 💡 **Hint:** Re-read the theory above if unsure.

Keep going

✏️ Write your code here
🐍
Loading Python...
First visit only — ~5-10s. Stays cached afterward.
📊 Result
Press Run to see result...
📣 Help someone learn PythonShare this lesson with a friend — the first 15 are free, no signup.Tweet

💬 Discussion

Be the first to ask a question or share a tip.
Sign in to join the discussion. Reading is free.
Loading discussion…