Author's note: Claude was used to scaffold and review this post
Intro: looking for main()
For a long time, I've struggled to form a mental model around AI. In many previous technologies I've encountered, I have a "where is the main()?" quest. Nothing is magic (or everything is, but that's another discussion), so I should be able to understand how this stuff works well enough for a technical deep dive.
AI's "main()" has eluded me up to this week, I think. But now, I think I get it.
Memento should be required viewing
Hear me out - Christopher Nolan's early film Memento was my epiphany.
In the film, protagonist Leonard Shelby has amnesia from an injury and is trying to solve his wife's murder. He starts each scene in the nonlinear narrative with a baseline of knowledge, but temporary and permanent memory are achieved by photos, notes and tattoos.
It's a great film - one that I watched many years ago and has stuck with me, due to the inventive storyline.
And reflecting on it helped me "find main()" with AI.
- Leonard's training data is everything before his injury.
- Each few minute scene is his context window.
- His photos, notes and tattoos are system prompts, memory files, CLAUDE.md / AGENTS.md / etc.
No spoilers, because you really should watch it, but I will say that false information supplied with prompts and memory files can affect the system adversely. Garbage In / Garbage Out (especially if the garbage is deliberate misinformation by people with bad intent).
This is merely a mental model to better understand how to use the tool. In reality, an AI system rereads the whole conversation from the top for every word it writes. Leonard keeps a few minutes in his head; the AI keeps nothing between words.
Found it: the main loop
Claude was nice enough to write this out as pseudocode for me
tokens = system_prompt + memory_files + conversation;
while (!stop) {
logits = forward(weights, tokens); // pure function, weights frozen
next = sample(logits);
tokens.push_back(next);
}
For any non-programmers, read it like this:
- The system starts at the trained state
- Add in a system prompt, and memory (Leonard's tattoos), and the conversations between you and the AI system so far.
- To generate a response, the AI system keeps generating words (tokens - not exactly, but good enough for our discussion) until it says "yeah, enough. Your turn, user".
How this helps
- What's important gets written in "tattoos" - permanent memory files.
- Try to be judicious about use of space. Leonard only had so much skin and didn't want to waste time with excessive re-reading for context.
- Other inputs (web sites visited during a session) get added to the pile of words as well. And those words are reprocessed for output words. Be wary of bad actors.