> ## Documentation Index
> Fetch the complete documentation index at: https://aegean.ai/llms.txt
> Use this file to discover all available pages before exploring further.

# MDP Language Models

> Text generation as an MDP, and why dynamic programming cannot solve it.

A token-level MDP of a language model, models $S_t$ as the prompt plus every token generated so far, and the action $a_t$ is the generation of the next token. The transition appends the token to the context, $S_{t+1} = s_t \oplus a_t$. In this application dynamics are deterministic and known. The policy is the model itself, $\pi_\theta(a_t \mid s_t)$. Each token may depend on everything before it, and the process is still Markov, because the state holds the whole history.

The reward is known as well, in the sense that a reward model can be a separate network we can query or even a more concrete verifier or even a unit test that verifies a coding agent output and this can easily provide a pass or fail reward signal. In fact multistep MDP is  not even needed when the reward scores the whole response. In this case we treat the full response as a single action, given the prompt.

As it turns out despite the fact that we have knowledge of MDP dynamics in autoregressive language models, there is a major computational issue in the application of DP algorithms.Dynamic programming keeps a value for every state and updates all of them in every sweep. The states of a language model are all token sequences up to the length limit. With a vocabulary of about $10^5$ tokens there are about $10^{5T}$ sequences of length $T$, so not even one sweep is possible.

Instead we have to use Reinforcement Learning algorithms to optimize the agent responses and Language models are therefore trained with sampling methods, such as policy gradient on sampled responses, which visit only the states the policy actually reaches.

> [!NOTE]
>
> Partial obvervation of the state however is more of a norm than the exception. In  dialogue or tool use, the environment also adds observations to the context such as the user's reply or the tool use outputs. So the assumption that the agent generates the whole next state is not true anymore. When the history of a multi-turn or agentic task no longer fits in the LLM agent's context window, the problem is partially observable. Long term memory and summarization then play the role of the agent's own estimate of the state.
>
> The part of the reward that is unknown is the true objective, human preference. The reward model is a learned estimate of it, and a policy that maximizes the reward model can exploit the model's error (*reward hacking*). It limits how far we can optimize against the reward model.
>
> Some planning does happen at decoding time, because the model is known. Beam search, best-of-$N$ sampling with a verifier and Monte Carlo tree search all use the model to look ahead from the current state.
> They search a tiny part of the tree and never sweep the state space.

***

Found an error on this page? [Report an issue](https://github.com/aegean-ai/eaia-feedback/issues/new?template=page-issue.yml\&page=/aiml-common/lectures/mdp/mdp-language-models).


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.