Recommended resources
The freely available resources this lesson will direct you to.
- Lost in the Middle: How Language Models use Long Context - Explained!www.youtube.com
- Greedy? Min-p? Beam Search? How LLMs Actually Pick Words – Decoding Strategies Explainedwww.youtube.com
- Why AI Agents Fail: The Stochastic Convergence Spiral | by Gian Lucamedium.com
- [PDF] Measuring Long Horizon Execution in LLMs - arXivarxiv.org
Introduction
Welcome to the first lesson of your journey into designing agentic AI systems. Our goal throughout this course is to equip you with the mental models and architectural patterns to transition from executing tasks yourself to designing systems where AI agents execute them for you—a crucial skill for delivering robust AI solutions to clients.
This first module lays the groundwork by examining the fundamental component of any agentic system: the Large Language Model (LLM). Today, we'll focus on a single, critical learning outcome: to analyze how next-token prediction directly impacts agent reliability, consistency, and failure modes.
Understanding this is paramount. The probabilistic nature of LLMs is both their greatest strength—enabling creativity and fluency—and their greatest weakness, introducing a fundamental source of unreliability. As a system architect, your primary challenge is to design systems that harness the former while mitigating the latter. Given your background in computer science, we will approach this not as a black box, but from a first-principles perspective, examining the underlying mechanics and their systemic consequences.
The Engine of Ambiguity: Probabilistic Next-Token Prediction
At its core, an LLM is a sophisticated function that, given an input sequence of text (tokens), produces a probability distribution over its entire vocabulary for the very next token. It doesn't "know" the "right" answer; it calculates the likelihood of every possible next word. This autoregressive process, where each predicted token is appended to the input for the next step, is how an LLM generates text.
The process of selecting a single token from this vast probability distribution is handled by a decoding strategy. This is your first and most direct lever for influencing an agent's behavior, balancing the trade-off between deterministic, consistent output and creative, diverse responses.
To get a clear overview of these strategies, please watch the following video. It explains the most common approaches you'll encounter.
Greedy? Min-p? Beam Search? How LLMs Actually Pick Words – Decoding Strategies Explained
This video from AI Coffee Break with Letitia provides a concise and clear explanation of the fundamental decoding strategies that govern LLM text generation. Focus on understanding the conceptual difference between deterministic and sampling-based methods.
Watch from the beginning to 08:00. Pay close attention to the explanations of Greedy Decoding, Top-K, Top-P (Nucleus Sampling), and especially Temperature. We will discuss Temperature in more detail in the next lesson, but it's crucial to understand its role here.
As the video explains, you have a spectrum of choices:
- Greedy Decoding: Always picking the single most probable next token. This is deterministic and fast but often leads to repetitive and uninspired text. It's generally unsuitable for complex agentic tasks that require nuanced responses.
- Sampling-based Decoding (Top-K, Top-P): These methods introduce controlled randomness. Instead of picking the single best token, they sample from a truncated distribution containing the top
Kmost likely tokens or a "nucleus" of tokens that collectively meet a probability massP. This is the standard for most applications, as it provides a balance between coherence and creativity.
The temperature parameter, which we'll explore more deeply later, directly manipulates the probability distribution itself before sampling.

Your choice of decoding strategy and its parameters is a foundational design decision that directly impacts the consistency of your agent's output. For a client solution that must perform a task reliably (e.g., extracting specific data from a report), you would favor settings that reduce randomness (lower temperature, lower P in Top-P). For a creative task (e.g., generating marketing copy), you would increase it.
From Probabilistic Generation to Systemic Failures
The autoregressive and probabilistic nature of next-token prediction is the root of several critical failure modes that you must design your systems to handle.
Failure Mode 1: The Self-Conditioning Effect
Because each new token is generated based on the sequence of all preceding tokens (including those the model just generated), errors tend to compound. A single, slightly off-kilter token can send the model down a "bad path," making subsequent errors more likely. The model effectively conditions itself on its own mistakes.
This is a subtle but powerful effect. It means that an agent's reliability isn't constant; it degrades over the course of a long task. A recent paper from arXiv provides an excellent formalization of this phenomenon.
[PDF] Measuring Long Horizon Execution in LLMs - arXiv
This paper, 'The Illusion of Diminishing Returns,' introduces and validates the 'self-conditioning effect.' It demonstrates that even with perfect knowledge and a correct plan, an LLM's per-step accuracy degrades as it makes more mistakes.
Please read Section 2, 'The Self-Conditioning Effect' (page 6), and review Figure 5. This section experimentally disentangles degradation due to context length from degradation due to the model conditioning on its own errors. The findings here are critical for understanding why agent performance can decay unexpectedly in long-running workflows.
The key takeaway is that larger models are not immune to this effect. Simply scaling the model doesn't solve the problem of compounding errors, which has significant implications for designing reliable, long-horizon agentic workflows.
Failure Mode 2: The "Lost in the Middle" Problem
Another failure mode stems directly from the Transformer architecture that powers most modern LLMs. These models don't weigh all parts of their input context equally. Information presented at the very beginning or very end of the context has a much stronger influence on the output than information buried in the middle.
This "U-shaped" performance curve is a major source of unreliability, especially for agents that need to synthesize information from long documents or complex histories.
Lost in the Middle: How Language Models use Long Context - Explained!
This video from Weaviate explains the 'Lost in theMiddle' paper, which first systematically documented this U-shaped performance curve.
Please watch the segments from 00:00-01:14 and 02:10-04:03. These sections introduce the core concept and show the experimental results that reveal the U-shaped performance curve across different models. You don't need to worry about the RAG-specific details for now; focus on the core phenomenon of positional bias.
This architectural limitation means that the structure of the information you provide to an agent is as important as the information itself. We will revisit this concept in depth when we discuss context management and Retrieval-Augmented Generation (RAG).
A Framework for Taming Stochasticity
Given these inherent failure modes, how can we build reliable systems for clients? The answer lies in moving beyond the model itself and designing an agentic system. An agent is not just an LLM; it is an LLM embedded in a feedback loop with deterministic tools.
A powerful mental model for this is the Stochastic Convergence Spiral. It frames agentic work not as a single generation, but as an iterative process that progressively reduces uncertainty.
Why AI Agents Fail: The Stochastic Convergence Spiral
This article by Gian Luca Bailo provides a formal framework for thinking about agent reliability, drawing an analogy to dynamical systems and control theory. It models the agentic process as a contractive map that forces a stochastic model toward a deterministic, verifiable outcome.
Read Section 1 ('Introduction: The Entropy Problem') and Section 2 ('Theoretical Framework: Convergence as Contraction'). Focus on understanding the concepts of the agent as a 'Maxwell's Demon,' the iterative map F = G(V(s)), and the contraction factor λ. This provides a mathematical language for describing agent reliability.
The core idea is this:
- The Generator (LLM) produces a high-entropy, probabilistic output (e.g., a piece of code).
- A Verifier (a deterministic tool like a compiler, a unit test, or an API schema validator) checks the output against ground truth.
- The Verifier produces deterministic feedback (e.g., "SyntaxError on line 42").
- This feedback is passed back to the Generator, which produces a refined output.
With each iteration, the agent's output "spirals" closer to a correct, low-entropy fixed point. Your job as a designer is to create this loop and ensure the feedback is informative enough to make the process converge—that is, to ensure the contraction factor is less than 1.
Conclusion
In this lesson, we've dissected the fundamental mechanism of LLMs and traced its consequences for system design.
Key Takeaways:
- LLMs are Stochastic Engines: Their core function is next-token prediction, which is inherently probabilistic. Your first level of control is the decoding strategy (e.g., Top-P, temperature), which manages the trade-off between consistency and creativity.
- Stochasticity Breeds Failure Modes: The probabilistic, autoregressive nature of LLMs leads directly to critical failure modes like the self-conditioning effect (errors compounding over time) and the "lost in the middle" problem (uneven attention over the context).
- System Design Tames Stochasticity: The key to building reliable agents is not to eliminate the LLM's stochasticity but to constrain it. The Stochastic Convergence Spiral provides a powerful framework for this, where an LLM is placed in a feedback loop with deterministic tools that guide its output toward a correct state.

Preview of the Next Lesson:
We've established that next-token prediction is the source of both power and problems. In the next lesson, we will focus on the practical application of this knowledge. We will learn how to strategically manipulate context windows, temperature, and token limits to achieve desired agent behaviors, giving you a concrete toolkit for your first design decisions.