The basic recurrence
An RNN processes a sequence one token at a time, maintaining a hidden state h that summarizes everything seen so far. At each step: h_t = f(W_h h_{t-1} + W_x x_t + b). The same weights are applied at every step (parameter sharing across time, like CNNs share across space).
Vanilla RNNs are simple and elegant but have terrible vanishing gradients on long sequences. The standard fixes are LSTM (Long Short-Term Memory, 1997) and GRU (Gated Recurrent Unit, 2014), both of which add gating mechanisms that let gradients flow further back through time.
LSTM cells, in pictures and in code
An LSTM cell maintains TWO state vectors: a hidden state h (output) and a cell state c (long-term memory). Three gates (forget, input, output) control what flows where. The forget gate is the key insight — it lets the cell selectively keep or drop information across hundreds of timesteps without vanishing.
When RNNs are still the right answer in 2026
Streaming inference (one token at a time, fixed memory). State-space models (Mamba, S5) which are RNN-like and competitive with transformers at very long context. Some music/audio generation pipelines. Most other sequence tasks have moved to transformers.