Coders In Hoods
  • Courses
  • Roles
  • Blog
  • Setup

Coders In Hoods AI Academy

Every course is free. Sign in to start, and your progress, quiz scores and bookmarks follow you.

AI Academy

  • Courses
  • Paths
  • Setup guides
  • How we teach

Role roadmaps

  • AI Engineer
  • Forward Deployed Engineer
  • Machine Learning Engineer
  • All roles

Coders In Hoods

  • The agency
  • Coaching for teams
  • Blog
  • LinkedIn
  • X (Twitter)

Contact

codersinhoods@gmail.com

© Coders In Hoods · since 2019

Course overview

LLMs for Engineers

Free · sign in to start and track progress

1 · Inside the transformer

  • 1Tokens and embeddings
  • 2Attention, step by step
  • 3The transformer block

2 · How models are trained

  • 4Pretraining and scaling laws
  • 5Post-training an assistant

3 · Running models in production

  • 6Sampling and hallucination
  • 7Context windows, KV cache and caching

    Exercise answers

    • Answer: Watch prompt caching hit and miss
    • Answer: Implement a KV cache and measure the speed-up
  • 8Reasoning models and extended thinking
  • 9Choosing and comparing models
CapstoneChoose a model for document extractionReference implementation

Module 3 · Running models in production · Lesson 7

Context windows, KV cache and caching

What happens to your prompt at inference time, why long context costs memory and money, how the KV cache and prompt caching work, and why a million-token window is not a million tokens of attention.

75 min 2 videos 3 exercises 6-question quiz Answers
Setup for the exercises:Install Claude CodePython with uvRun Hugging Face models on CPU

By the end of this lesson you can

  • Distinguish prefill from decode and relate each to time-to-first-token and output speed
  • Compute the KV-cache size of a model from its config, and explain how grouped-query attention shrinks it
  • Explain prompt caching as reuse of a prefix's KV cache, and compute the savings for a realistic workload
  • Structure a prompt so the cache hits, and diagnose the common silent invalidators
  • Summarise the evidence that model quality degrades with long or cluttered contexts

On this page

  • Two phases of inference
  • The KV cache
  • Worked example
  • Prompt caching: reusing the KV cache across requests
  • How it works on the Claude API
  • Worked example: what caching saves
  • Why caches silently miss
  • How well do models use long context?
  • Summary
  • Sources & further reading
  • Quiz

In this lesson

  1. 01Two phases of inference
  2. 02The KV cache
  3. 03Prompt caching: reusing the KV cache across requests
  4. 04How well do models use long context?
  5. 05Summary

Sources & further reading

  • Efficient Memory Management for Large Language Model Serving with PagedAttentionPaper · Kwon et al. · UC Berkeley · 2023The vLLM paper. Sections 2–4 explain why KV-cache memory is the serving bottleneck and how paging it in blocks raises throughput.
  • GQA: Training Generalized Multi-Query Transformer Models from Multi-Head CheckpointsPaper · Ainslie et al. · Google Research · 2023Grouped-query attention, the reason most current open models have far fewer key/value heads than query heads.
  • Lost in the Middle: How Language Models Use Long ContextsPaper · Liu et al. · Stanford · 2023The U-shaped curve: models use information at the start and end of a long context better than in the middle.
  • RULER: What's the Real Context Size of Your Long-Context Language Models?Paper · Hsieh et al. · NVIDIA · 2024Shows that passing needle-in-a-haystack tests overstates usable context; harder long-context tasks degrade well before the advertised limit.
  • SGLang: Efficient Execution of Structured Language Model ProgramsPaper · Zheng et al. · 2023RadixAttention, which shares cached prefixes across requests automatically. The open-source counterpart of hosted prompt caching.
  • Prompt caching (Claude API docs)Docs · AnthropicThe authoritative source for the mechanics in this lesson (prefix order, breakpoints, minimum lengths, TTLs, the invalidation table and usage fields).
  • Context Rot: How Increasing Input Tokens Impacts LLM PerformanceArticle · Hong, Troynikov, Huber · Chroma · 2025A careful study across 18 models showing performance becomes less reliable as input grows, even on simple tasks, especially with distractors.
Free · sign in to start

Sign in to keep reading.

Free, no card. We use your account to save progress, quiz scores, bookmarks and occasionally send updates from our team.

  • 5 more sections
  • 3 hands-on exercises
  • A 6-question quiz
  • Progress, quiz scores and bookmarks