Decoder.

A transformer component that generates text tokens sequentially using causal attention, where each token can only attend to previous tokens in the sequence.

A decoder is the core component of modern large language models that generates text one token at a time in an autoregressive manner. Unlike encoder-decoder architectures used in early transformer models, contemporary LLMs like GPT, Claude, and Llama use decoder-only architectures that have proven highly effective for text generation tasks. The decoder's fundamental role is to predict the next token in a sequence based on all previously generated tokens, making it the engine behind conversational AI and text completion.

The decoder operates using causal (or masked) self-attention, which ensures that when predicting a token at position i, the model can only access information from positions 1 through i-1, never from future positions. This constraint prevents the model from 'cheating' by looking ahead during training and ensures that generation follows a left-to-right sequential pattern. Each decoder layer contains self-attention mechanisms, feed-forward networks, and residual connections, with the stack typically ranging from dozens to hundreds of layers in large models.

The shift to decoder-only architectures has simplified model design while achieving superior performance across diverse language tasks. This approach eliminates the need for separate encoder components, reducing architectural complexity and training overhead. However, the autoregressive nature of decoders means they generate text sequentially rather than in parallel, which can create latency bottlenecks during inference. Understanding decoder mechanics is crucial for optimizing model performance, as factors like context length, attention patterns, and layer depth directly impact both generation quality and computational efficiency.