The agent loop has a memoization problem. Nobody built the table.
An agent loop is naive recursive Fibonacci, and nobody built the memoization table. When a research agent spawns sub-agents, they independently recompute the same subproblems -- retrieving the same papers, calling the same tools with semantically equivalent queries -- paying full inference and API cost every time, because no current framework detects that these are the same subproblem. The fix is the one Bellman wrote down in 1957: memoize. But the DP table for agents can't key on exact input, because 'carbon pricing research' and 'carbon tax policy papers' are the same subproblem with different text. The key has to be a semantic fingerprint, and the cache value isn't just the result -- it's the KV states that encode processing that result, so a second branch that hits the same subproblem skips the tool calls and most of the prefill. Three recent papers set up the frame -- the Y-combinator paper gives the lambda-calculus framing, the RLM paper gives the REPL execution substrate, RAO gives the training objective -- and none of them built the cache. The semantic fingerprinting layer, the hash function that decides when two subproblems are the same, is the piece that doesn't exist yet. Everything else is standard CS applied to a new domain. The hash function is the research problem.
July 26, 2026Let me build it.
I've been thinking about this since a conversation I had with a friend who kept insisting that the right mental model for an agent loop is a state machine. He's not wrong. But state machines don't capture the thing that's most interesting about multi-step agentic execution: that subproblems recur, and when they recur, every current agent framework recomputes them from scratch.
There's a name for the class of algorithm that has this problem. It's called naive recursion. And the fix has been known since Bellman wrote about it in 1957.
The Y-Combinator for LLMs paper and why it's the right framing.
Roy et al. published "The Y-Combinator for LLMs: Solving Long-Context Rot with λ-Calculus" (arXiv:2603.20105, March 2026). The core argument: recursive language models are the right answer to context rot -- the degradation of LLM performance as context grows -- because they externalize the prompt and solve subproblems recursively. But the existing ones let the model write free-form control code in a REPL, which is hard to verify or predict. Their λ-RLM replaces that with a typed functional runtime built from a small library of pre-verified combinators, with neural inference only at bounded leaf subproblems. That structure is what buys them formal guarantees: termination, closed-form cost bounds, controlled accuracy as recursion deepens.
The Y-combinator is a fixed-point combinator. It allows a function to call itself recursively without being bound to a name. Y(f) = f(Y(f)). If f is a function that takes itself as an argument and uses that to recurse, Y(f) is the recursive function. The Y-combinator is how lambda calculus -- a language with no recursion primitive -- implements recursion.
Applied to agents: an agent that invokes a sub-agent with a modified context, where the sub-agent can in turn invoke sub-agents, is implementing Y-combinator-style recursion. The agent doesn't need to know its own name. It receives itself as a capability and calls it on a reduced subproblem. The recursion terminates when the subproblem is small enough to solve directly or when a base case is reached.
This is the right framing. Let me show you the problem it exposes.
The memoization problem in agent recursion.
In classic DP, the Fibonacci sequence computed naively:
fib(n) = fib(n-1) + fib(n-2)
Computed naively, fib(50) makes about 41 billion function calls (40,730,022,147). With memoization:
cache = {}
def fib(n):
if n < 2: return n
if n in cache: return cache[n]
result = fib(n-1) + fib(n-2)
cache[n] = result
return result
fib(50) makes exactly 99 calls. Same result. About 4×10^8 times fewer calls. The memoization table is the data structure that transforms an exponential algorithm into a linear one by recognizing that the same subproblem appears in multiple branches of the recursion tree and caching the result the first time.
Agent loops have overlapping subproblems. Here is a specific example.
A research agent is asked to write a report on climate policy. It spawns sub-agents: one to research economic impacts, one to research environmental outcomes, one to research political feasibility. Each sub-agent independently searches for papers by the same authors, retrieves the same IPCC reports, calls the same web search tool with semantically equivalent queries. The three sub-agents independently compute the same subproblems -- "retrieve information about carbon pricing" -- and return similar results.
No current agent framework detects that these are the same subproblem. Each sub-agent executes the tool calls independently, paying full inference and API cost three times. The memoization table is empty because nobody built it.
What the memoization table for agents looks like.
In DP, the table is keyed by the exact input to the function. For agents, exact input matching is the wrong key because two tool calls with semantically equivalent arguments are the same subproblem even if the arguments aren't textually identical. "papers about carbon taxation" and "research on carbon tax policy" are the same subproblem.
The memoization key is a semantic fingerprint: an embedding of the tool name, the semantic intent of the arguments, and the relevant context that the call depends on. Two calls with embeddings within cosine distance δ are the same subproblem and should return the cached result.
The cache entry is: the result of the tool call, and the KV cache states for the processing of that result. When a cache hit is found for a sub-agent invocation, you don't just return the text result -- you return the KV states that encode the processing of that result, so the downstream agent can skip most of the prefill over the tool result.
This is cross-branch KV sharing, and it isn't free. KV states depend on everything earlier in the context, so KV computed in one branch is exact only for another branch with the same prefix. Dropping it in after a different prefix is an approximation. That's the problem CacheBlend (arXiv:2405.16444) works on: reuse most of the cached KV and selectively recompute the tokens whose attention changes. The second branch skips the tool calls entirely and pays a fraction of the prefill, not zero.
The eviction policy is DP-guided: KV states for subproblems that are on the critical path (subproblems that must be computed before the final answer can be produced) stay hot. KV states for subproblems that are in completed branches or that are leaves of the recursion tree can be tiered down. This is exactly the graph-aware KV eviction I wrote about earlier, now derived from DP first principles rather than intuition about workflow graphs.
Recursion detection and the base case.
Naive Y-combinator recursion without a base case diverges. An agent that spawns a sub-agent with the same task will spawn another sub-agent with the same task indefinitely. This is the "retry loop" failure mode I described at the start of the competitive programming post -- the agent in a cycle it can't escape.
DP handles this by detecting cycles in the dependency graph. If subproblem A depends on subproblem B which depends on subproblem A, you have a cycle. The standard DP treatment: cycles indicate that the subproblem is ill-defined (true recursion without termination) or can be resolved by a fixed-point iteration.
For agents, cycle detection in the subproblem graph is the structural analog of loop detection. The memoization table tracks which subproblems are "in progress" (currently being computed by some branch) in addition to which are complete. When a new sub-agent invocation's fingerprint matches an "in progress" entry, you've detected a cycle. Options:
Fail fast: terminate the new invocation with a sentinel value indicating a cycle was detected. Force the parent to handle the cycle explicitly.
Fixed-point iteration: allow the cycle to execute once more, using the current partial result as the approximation. If the new result matches the previous result within tolerance, you've reached a fixed point. If not, update the cache and iterate. This is the DP solution to cyclic dependencies in constraint satisfaction -- iterate until convergence.
The Y-combinator doesn't solve termination, and that's the point: Y(f) = f(Y(f)) recurses forever unless f has a base case. In untyped lambda calculus it's the textbook way to write a term that never terminates. Termination comes from well-founded recursion: each recursive call operates on a strictly smaller input. That's why λ-RLM gets its termination guarantee from the typed combinator library and bounded leaves, not from Y. For agents, the analogous requirement is that each recursive sub-agent invocation operates on a strictly simpler or smaller subproblem. The memoization layer enforces this: if the fingerprint matches a completed entry, return cached. If it matches an in-progress entry, cycle detected, handle accordingly.
Recursive Agent Optimization (RAO) and the training signal.
Recursive Agent Optimization (arXiv:2605.06639, May 2026) trains recursive agents with reinforcement learning over rollout trees -- trees of sub-agent invocations rather than flat token sequences. It samples several rollout trees per root task, scores each trajectory against a leave-one-out baseline from the other trees, and trains every agent instance in the tree jointly, so the policy learns when to delegate, how to phrase sub-tasks, and how to combine the results.
The DP connection to RAO: the rollout tree IS the DP recursion tree. Each node is a subproblem. Each edge is a recursive call. The value function being optimized is the expected reward of the full rollout tree, which decomposes recursively over the tree structure. RAO is solving the same optimization problem that DP solves -- find the policy that maximizes expected value by decomposing the problem into subproblems and optimizing each subproblem's contribution -- but using RL rather than exact dynamic programming because the state space is too large for exact DP.
The credit-assignment instinct rhymes with DP: in DP, the value of a subproblem is computed relative to the values of its children; in RAO, the paper argues for node-local signals where sub-tasks can be verified -- a sub-agent graded on whether its own task and its delegated sub-tasks were solved, rather than only on the root's reward. Same instinct, different algorithm class.
What RAO doesn't have yet: the memoization layer. Two branches of the rollout tree that encounter the same subproblem are still computed independently. Adding the memoization table I described would reduce the number of distinct subproblem evaluations needed during training, which reduces the training cost (fewer total sub-agent invocations for the same number of training examples). The trade-off is variance: a cached answer is one sample reused, not a new one, so you'd want to cache at inference time and be more careful about it during training.
The Recursive Language Model (RLM) as the substrate.
The RLM (arXiv:2512.24601, Zhang, Kraska, Khattab, December 2025) proposes a specific mechanism: instead of summarizing context (which loses information), the model treats a long prompt as part of an external environment. The context lives as a variable in a Python REPL, and the model writes code that examines it, decomposes it, and calls itself recursively on snippets. Those sub-calls can make sub-calls of their own. The recursion is explicit in the scaffolding, as code the model writes.
This is the execution engine for the memoization scheme I described. Every sub-call is a function call in code, which gives you a natural checkpoint for the memoization lookup: before a sub-call runs, compute the fingerprint of its task and check the cache. If hit, return the cached result and inject the cached KV states where the prefix allows. If miss, run the sub-call and store the result.
The sub-call boundary is also where the cache value gets captured. Its inputs and outputs are already explicit there: the task going in, the answer coming back, and the KV states the caller builds while reading that answer. That's exactly what a memoization layer needs to store and replay to the next branch that asks the same question.
The thing this would build, made concrete.
You're running a research agent on a corpus of 10,000 papers. The task: "find all papers that contradict the main claims of this survey." The agent spawns 50 sub-agents to analyze different sections of the corpus. Many of those sub-agents will independently retrieve the same foundation papers, process the same common references, form conclusions about the same baseline claims.
With the memoization table: the first sub-agent to process "paper X contradicts claim Y" stores the result and its KV states. Every subsequent sub-agent that arrives at the same question gets the cached answer in milliseconds instead of re-running retrieval and inference, and reuses KV states where the prefix allows. The 50 sub-agents effectively share a pool of solved subproblems. The total work done is proportional to the number of distinct subproblems, not the number of (subproblem, agent) pairs.
Without it: each of the 50 sub-agents independently retrieves and processes paper X. 50 inference calls, 50 sets of KV cache, 50x the cost.
The memoization table is the difference between O(unique subproblems) and O(agents × subproblems) in inference cost for the class of tasks where sub-agents encounter overlapping subproblems. Research, code analysis, competitive intelligence, any task with natural structure that different analysis branches independently need to ground truth against.
the agent loop is naive recursive fibonacci.
the memoization table is the optimization that transforms it.
the y-combinator paper (march 2026) gave us the lambda calculus framing.
the rlm paper (december 2025) gave us the execution substrate.
the rao paper (may 2026) gave us the training objective.
nobody built the cache.
the semantic fingerprinting layer -- what makes "carbon pricing research" and "carbon tax policy papers" the same subproblem -- is the piece that doesn't exist yet. it's the hash function in the DP cache. everything else is standard computer science applied to a new domain. the hash function is the research problem.
P.S. The fixed-point iteration approach to cycles has a beautiful connection to the RL training literature. Value iteration in RL is itself a fixed-point algorithm: iterate the Bellman equation until the value function converges to a fixed point. When a cycle appears in an agent's subproblem graph, resolving it via fixed-point iteration is exactly what RL value iteration does on cyclic MDPs. The agent's cycle is a small MDP with a back-edge, and the fixed-point resolution gives you the correct value for the subproblem that forms the cycle. This is not a coincidence -- it's the same mathematical structure (contraction mappings, fixed-point theorems) appearing at two different levels of the agent stack. The RL training objective and the runtime cycle resolution are solving the same class of problem. That connection is worth a standalone paper.
i write these when i have something worth saying. no schedule. no algorithm. if you want to know when the next one goes up -- leave your email.
no spam. no sequence. just the note, when it exists.