Tokens and the context window

Models read tokens, not words, and can only look at a limited window of them at once. Watch old tokens fall out.

Time O(n)Space O(w)Code C#
Watch it animatedStep through it, with the running line highlighted. Free with an account.

The idea

A model does not see letters or words. Text is first cut into tokens: common words, pieces of longer words, punctuation. Everything is measured in tokens: the size of your prompt, the length of the answer, and the context window, which is the most tokens the model can consider in one request. The demo uses a tiny window so you can watch it fill and the oldest tokens drop out.

Code

public sealed class ContextWindow
{
    private readonly Queue<string> _tokens = new();
    private readonly int _capacity;

    public ContextWindow(int capacity) => _capacity = capacity;
    public IEnumerable<string> Visible => _tokens;

    public void Add(string token)
    {
        _tokens.Enqueue(token);
        if (_tokens.Count > _capacity)
            _tokens.Dequeue();
    }
}

public static string WhatTheModelSees(ITokenizer tokenizer, string text, int capacity)
{
    var tokens = tokenizer.Split(text);
    var window = new ContextWindow(capacity);
    foreach (var t in tokens) window.Add(t);
    return string.Join(" ", window.Visible);
}

Tips

In the .NET library

This demo window drops the oldest token. Real apps decide what to keep on purpose: they trim or summarize old conversation turns and always keep the system prompt, instead of letting important text fall out by accident.