Back to Discover
Spark

How AI Writes Code

AI code generation is next-token prediction: the model repeatedly samples the most probable next token from a learned distribution over its training data, producing code token-by-token without any internal plan or verification step.

Before you enter

A complete interactive classroom, not just a preview.

Start when you are ready to enter this Stage's 4 scenes and explore, respond, and learn as you go.

4
Scenes
8 min
Estimated
Content language: en-US
Start this Stage
Sign-in may be required to play
What happens inside
  1. 01The Magic Illusionslide
    Slot 1Hook

    Show a screen where typing a coding prompt produces a full, working function in seconds. Ask: how did the AI know to write exactly that code?

    • AI produces code from a plain-English prompt
    • It feels like understanding
    • But the model was never explicitly taught programming rules
    Phenomenon

    An AI assistant generates a complete, working function seconds after receiving a natural-language request.

    Question

    If the model was never taught coding rules like a student, where does the code come from?

  2. 02The Tempting Misconceptionslide
    Slot 2Tension

    Surface the intuitive belief: the AI 'reads' the request, 'understands' the problem, then 'writes' the solution — like a human programmer.

    • Intuition: AI reasons like a developer
    • Implies planning, checking, debugging internally
    • But the model has no execution loop and no compiler
    Prediction

    Learners expect AI code generation to involve a planning step where the model figures out the algorithm before writing.

    Tempting intuition

    The AI understands the problem and writes code the way a human programmer would — thinking first, then typing.

  3. 03One Token at a Timeslide
    Slot 3Reveal

    Reveal that the model only does one thing: predict the next token. Show a diagram of a partial code output and a probability distribution over what comes next, with the chosen token highlighted.

    • The model outputs one token (a piece of code) at a time
    • Each token is chosen from a probability distribution learned during training
    • The chosen token is fed back in, and the process repeats
    Evidence

    Every code-generating model produces output autoregressively: it sees a sequence and picks the most probable next token, appends it, and repeats.

    Conclusion

    The 'smart' code is just thousands of tiny next-token guesses stitched together.

    Mechanism
    1. 1The prompt and all tokens generated so far are fed into the model, which outputs a probability for every token in its vocabulary.
    2. 2One token is sampled from that distribution, appended to the sequence, and the loop restarts — no planning step, no verification, just probability sampling repeated thousands of times.
  4. 04Why It Looks Like Magicslide
    Slot 4Takeaway

    Transfer the insight: when you ask the AI to write a sorting function, it isn't 'figuring out' sorting — it's predicting, from billions of seen code examples, the token sequence that sorts typically looks like. That statistical fluency is why simple requests work and why edge cases sometimes fail.

    • Code comes from patterns seen in training data
    • Statistical fluency mimics competence
    • Unusual requests drift because the pattern is thinner
    Transfer

    Ask the AI to write code in a brand-new language or for a brand-new library it likely never saw.

    Expected inference

    Learners should expect the output to degrade, drift, or invent plausible-looking but broken APIs — because next-token prediction has no ground truth to fall back on outside its training distribution.

Discussion

Discussion threads for a Stage aren't available yet.

Where this leads
Explore more

More in Technology & Computing

See all