
Get a High-Paying Tech Job. Software engineers like you join Outcome School to achieve the outcome that is a high-paying tech job.
Internet
Joined June 2024
- Tweets2.2K
- Following2
- Followers1.4K
- Likes1.6K
Pinned Tweet
Our recent 6 articles on X:
- KV Cache in LLMs
- Paged Attention in LLMs
- Causal Masking in Attention
- Byte Pair Encoding in LLMs
- Harness Engineering in AI
- Math behind Attention - Q, K, and V
X is a knowledge sharing platform.
Outcome School retweeted
This question was asked in AI Engineering interviews at Anthropic, OpenAI, and Cursor.
Question: How do you detect and measure hallucinations in a production RAG system?
Let's answer it step by step.
In RAG, a hallucination is a claim in the answer that the retrieved context does not support. So, we detect it by breaking the answer into claims and checking each claim against the context, and we measure it as a rate over sampled traffic.
First, we must separate two different failures, because the fixes are different:
- Retrieval failure: the right document was never retrieved, so the model guessed. We fix the retriever.
- Faithfulness failure: the right document was retrieved, but the answer says something it does not say. We fix the prompt or the model.
Now, the detection methods:
- Claim-level grounding check: split the answer into small factual claims. For each claim, an NLI model or an LLM judge decides: supported, contradicted, or not found in the context. If 8 of 10 claims are supported, the faithfulness score is 0.8.
- Citation check: every sentence must cite a chunk. We verify that the cited chunk actually supports the sentence, and that quoted text appears in it word for word.
- Retrieval check: did the retrieved chunks contain the answer at all?
- Uncertainty signals: low token probabilities on names, numbers, and dates, or disagreement when we sample the same answer several times. When the model guesses, its answers vary.
- User signals: thumbs down, "that is wrong" in the next message, and quick rephrasing of the same question.
Now, how we measure it in production. An LLM judge on every request is expensive, so we layer it:
- A small, fast NLI model checks a large share of the traffic.
- An LLM judge does the claim-level check on a random sample, plus all thumbs down cases.
- Humans review a small sample every week to keep the judge honest.
The metrics we track:
- Hallucination rate: the percentage of answers with at least one unsupported claim.
- Unsupported claim rate: unsupported claims divided by total claims.
- Correct abstention: on questions our documents cannot answer, how often the system says "I do not know" instead of guessing.
We slice all of these by topic, retrieval score, source, and prompt version. An average hides the one slice that is failing.
Offline, we keep a golden set with known answers, including questions with no answer in the documents, and run it before every change. For high-stakes answers, the same grounding check runs inline and blocks or regenerates an answer with unsupported claims.
In the interview, the key point is: in RAG, the retrieved context is the reference, so hallucination becomes measurable claim by claim, and we always separate retrieval failures from generation failures.
What is Mixture of Experts (MoE) and How Does It Work?
Read here: outcomeschool.com/blog/mixtu…
Outcome School retweeted
What is Flash Attention and Why Is It So Fast?
Read here: outcomeschool.com/blog/decod…
Outcome School retweeted
How does Multi-Head Latent Attention work?
Read here: outcomeschool.com/blog/multi…
How does HyDE work in RAG?
Read here: outcomeschool.com/blog/how-d…
Outcome School retweeted
How does Hybrid Search work?
Read here: outcomeschool.com/blog/how-d…
Outcome School retweeted
Forward Deployed Engineer Roadmap - A step-by-step roadmap to become a Forward Deployed Engineer (FDE) in AI, with a blog for every topic.
Start here: github.com/amitshekhariitbhu…
Outcome School retweeted
This question was asked in AI Engineering interviews at NVIDIA and Together AI.
Question: What is speculative decoding? Why is output quality preserved, and when does it not help?
Let's answer it step by step.
Speculative decoding is a method where a small, fast draft model guesses the next few tokens, and the large target model checks all of them in a single forward pass. We get several tokens for about the cost of one large-model step, with exactly the same output distribution as the large model alone.
First, why it works. Decode is memory-bandwidth-bound. Each step reads all the weights to produce one token, while the compute units sit mostly idle. So, checking 5 tokens in one pass takes almost the same time as generating 1.
Now, the steps:
- Draft: the small model generates k tokens, say 4, one by one.
- Verify: the large model runs once on all 4 draft tokens and gives its own probabilities at each position.
- Accept or reject: go left to right. Accept each draft token by the rule below. At the first rejection, replace that token with a corrected sample from the large model and drop the rest.
- Bonus: if all 4 are accepted, the large model's pass already gives the 5th token for free.
So, every round gives at least 1 token and at most k + 1.
Why is the quality preserved?
Let p(x) be the large model's probability and q(x) the draft model's probability for the draft token x.
- Accept x with probability min(1, p(x) / q(x)).
- If rejected, sample from the leftover distribution: max(0, p - q), normalized.
The papers by Leviathan et al. and Chen et al. prove that this gives exactly the distribution p. Every token is effectively sampled from the large model. The draft model only changes the speed, never the output distribution. With greedy decoding, it is even simpler: accept a draft token only if it equals the large model's top token.
Let's see the speedup. If each draft token is accepted with rate a, the expected tokens per round is:
(1 - a^(k+1)) / (1 - a)
With a = 0.8 and k = 4: (1 - 0.32768) / 0.2 = about 3.4 tokens per large model pass.
When does it not help?
- Low acceptance rate: the draft model is too weak or trained on different data, or the temperature is high. Most guesses get rejected, and the drafting time is wasted.
- Large batch sizes: the GPU is already busy with compute. Verifying extra tokens for every request is no longer free, and the gain shrinks or turns negative.
- Slow draft: if the draft model is not much faster than the target, the drafting time eats the savings.
In the interview, the most important line is: speculative decoding turns idle compute in memory-bound decode into extra tokens, and the accept-reject rule makes the output distribution exactly the same as the large model, so it is a pure latency win only when acceptance is high and the batch is small.
Detailed blog: outcomeschool.com/blog/specu…
Outcome School retweeted
How Does the Transformer Architecture Work?
Read here: outcomeschool.com/blog/decod…
What are Multi-Agent Systems and When Should We Use Them?
Read here: outcomeschool.com/blog/multi…
Outcome School retweeted
HNSW is a way of finding the most similar items in a huge collection very quickly.
How does HNSW work?
Read here: outcomeschool.com/blog/hiera…
Outcome School retweeted
This question was asked in AI Engineering interviews at Perplexity and Cursor.
Question: How do you keep an index fresh when the underlying corpus changes continuously?
Let's answer it step by step.
We never rebuild the full index on every change. We detect what changed, re-process only that part, and update the index in place. A full rebuild is kept only for the rare cases that need it.
Step 1: Detect the change.
- Event-driven: the source tells us. A database change stream (Change Data Capture), a webhook, or a file watcher.
- Polling: for sources that cannot push, we check them on a schedule. For the web, we recrawl a page based on how often it changes and how important it is.
- For a codebase, we hash every file and build a Merkle tree, a tree of hashes. We compare the root hash, walk down only the branches whose hash changed, and find the changed files fast.
Step 2: Re-process only what changed. Each chunk gets a stable ID, like doc_id plus chunk number, and a content hash. We re-chunk the changed document and compare the hashes. A chunk with the same hash keeps its old embedding. Only the new or changed chunks go to the embedding model. For small edits, this saves most of the embedding cost.
Step 3: Update the index.
- Upsert the changed chunks by ID.
- Delete the chunks that no longer exist. Deletes are the most forgotten part. A deleted document that is still retrievable is a stale answer, and for private data, a leak.
- Update the keyword index (BM25) and the metadata too, not only the vector index.
Step 4: Handle the limits of ANN indexes. Graph indexes like HNSW often handle deletes by only marking them, so quality and speed drop over time. So:
- Keep a small fresh index for recent changes and a large main index. Query both and merge the results.
- Merge and rebuild the main index in the background on a schedule.
Step 5: Full rebuilds with zero downtime. When we change the embedding model or the chunking, every vector must be recomputed. Vectors from two different embedding models cannot be compared. So, we build a new index next to the old one, test it, then switch traffic with an alias. This is a blue-green swap.
Step 6: Use time at query time. Store last_updated with each chunk. Boost recent chunks for time-sensitive queries, or filter by date. For breaking news, call a live search instead of trusting the index.
Step 7: Monitor freshness. Track the lag between a change in the source and the moment it becomes searchable. Set a target for each source, like 1 minute for prices and 1 day for documentation, and alert on it.
In the interview, the most important point is: freshness is an incremental pipeline problem. Detect changes, re-embed only changed chunks by content hash, handle deletes, and do full rebuilds as a background blue-green swap.
Learn LLM internals step by step - from tokenization to attention to inference optimization.
github.com/amitshekhariitbhu…
Outcome School retweeted
AI System Design - A complete guide to learn AI System Design step by step - from LLM inference, GPUs, KV Cache, and caching to RAG, Vector Databases, AI Agents, MCP, Multi-Agent Systems, Voice AI, Guardrails, Evaluation, Observability, Cost Optimization, and a step-by-step framework to crack any AI System Design interview. Everything in one place, explained in simple words, with detailed blogs for every deep dive.
Start here: github.com/amitshekhariitbhu…
Outcome School retweeted
Prefill vs Decode: LLM Inference Optimization
Read here: outcomeschool.com/blog/prefi…
Outcome School retweeted
What is a Reward Model and How Is It Trained?
Read here: outcomeschool.com/blog/rewar…
Outcome School retweeted
This question was asked in AI Engineering interviews at Cohere, Microsoft, and Perplexity.
Question: What is a reranker, when should you use one, and what does a cross-encoder cost you?
Let's answer it step by step.
A reranker is a second-stage model that takes the candidates from a fast retriever and reorders them by their true relevance to the query, so that the best few go to the LLM.
Retrieval works in two stages:
- Stage 1, retrieve: BM25 or vector search quickly finds the top 100 to 200 candidates out of millions of documents.
- Stage 2, rerank: a more accurate model scores only those candidates. We keep the top 5 or 10.
Why do we need stage 2? The fast retriever uses a bi-encoder. It embeds the query and each document separately, and compares the two vectors. The document was embedded without knowing the question. So, fine details get lost in one vector.
A reranker is usually a cross-encoder. It reads the query and the document together, as one input. The query tokens and the document tokens attend to each other. It outputs one relevance score.
For example, for "What is the capital of France?", a retriever can rank "Berlin is the capital of Germany" high, because it shares the word "capital". A cross-encoder sees that it does not answer the question, and pushes "Paris is the capital and largest city of France" to the top.
When should we use one?
- When precision at the top matters: RAG, where only the top 5 chunks fit in the prompt.
- When recall@100 is good but the right chunk is not in the top 5.
- After hybrid search, to put the merged BM25 and dense results into one consistent order.
What does a cross-encoder cost us?
- No precomputation. The score depends on the query, so we cannot embed documents in advance. Every query needs one model pass per candidate. 100 candidates means 100 pairs through a Transformer.
- Latency. This adds a noticeable delay to every request, and it grows with the number of candidates and the document length.
- It cannot fix recall. If the right document is not in the candidates, the reranker never sees it.
- It cannot replace the retriever. Running it over a million documents for every query is far too slow.
The knob is the number of candidates. More candidates gives better accuracy, but more latency and cost.
A middle ground is late interaction, like ColBERT. It precomputes one embedding per token for each document, and compares query tokens to document tokens at search time. More precise than a bi-encoder, faster than a cross-encoder, but it needs much more storage.
In the interview, the key line is: the retriever is built for recall over millions, the reranker is built for precision over a hundred, and the cross-encoder pays for that precision with one model pass per candidate.
Detailed blog:
outcomeschool.com/blog/how-d…
Outcome School retweeted
This question was asked in AI Engineering interviews at Moonshot AI and Groq.
Question: Explain disaggregated prefill/decode serving and when it pays for itself.
Let's answer it step by step.
Disaggregated serving runs the prefill phase and the decode phase of a request on separate pools of GPUs, and moves the KV cache from the prefill GPU to the decode GPU in between.
First, let's understand why we separate them.
- Prefill reads the whole prompt in one pass. It is heavy on math. It decides TTFT.
- Decode generates one token per step and reads all the weights and the KV cache every step. It is heavy on memory. It decides the time per output token.
When both run on the same GPU, they fight. A long prefill stalls the decode of every other user. We can improve TTFT or the time per output token, but improving one hurts the other. Chunked prefill reduces this fight, but every step still carries some prefill work.
Now, the flow with disaggregation:
- A router sends the request to a prefill worker.
- The prefill worker reads the prompt, builds the KV cache, and produces the first token.
- The KV cache is copied over a fast network to a decode worker.
- The decode worker generates the rest of the answer.
What we gain:
- No interference. Decode steps are never paused by a long prompt.
- We tune and scale each pool on its own. More prefill workers for long prompts, more decode workers for long answers.
- Different settings per phase: a different parallelism, batch size, or even a different GPU type for each pool.
What it costs: the KV cache transfer. For a model like Llama 3 70B in BF16, the KV cache is about 320 KB per token. A 4,096 token prompt means about 1.25 GB to move. At 400 Gb/s, which is 50 GB/s, that is about 25 ms. Many systems send it layer by layer while the prefill is still running, to hide this time. On a slow network, the transfer eats the gain.
So, when does it pay for itself?
- Large scale, with enough traffic to keep two separate pools busy.
- Long prompts: RAG, documents, code, long chat histories, where prefill is heavy.
- Strict targets on both TTFT and the time per output token at the same time.
- A fast interconnect between the GPUs, like NVLink or InfiniBand with RDMA.
And when it does not:
- Small scale or a single GPU. Chunked prefill is simpler and good enough.
- Short prompts, where the prefill is cheap and there is little to separate.
- No fast network.
Systems that use this idea include DistServe, Splitwise, and Mooncake, the serving platform of Moonshot AI for Kimi.
In the interview, the key line is: disaggregation removes the interference between prefill and decode, and it pays off only when that interference costs more than moving the KV cache.
Detailed blog: outcomeschool.com/blog/prefi…
Outcome School retweeted
How does an Embedding Cache work?
Read here: outcomeschool.com/blog/how-d…
How does Function Calling work in LLMs?
Read here: outcomeschool.com/blog/how-d…








