Agents and Long-Horizon Autonomy
local graph
Chapter 39 — Agents and Long-Horizon Autonomy
Section titled “Chapter 39 — Agents and Long-Horizon Autonomy”A model that answers a question is useful; a model that pursues a goal over hundreds of steps — reading files, running code, observing results, and adjusting course — is something else entirely. The transition from tool use (Chapter 31) to agency is the difference between an advisor and a worker, and making it reliable is among the most watched frontiers in the field. The flagship open models of 2026, GLM-5.2 chief among them, are explicitly built and benchmarked for long-horizon agentic engineering. This chapter examines what an agent is, why long horizons are hard, and where the research is heading.
39.1 The Agent Loop
Section titled “39.1 The Agent Loop”An agent is, at its core, a loop. The model is given a goal and a set of tools; it proposes an action (often a tool call), a harness executes it, the result is fed back, and the loop repeats until the goal is met or a budget is exhausted. The whole apparatus rests on the tool-calling mechanism of Chapter 31 — the model emits a structured call, the surrounding code runs the real function, the result re-enters the context — wrapped in iteration:
def run_agent(model, goal, tools, max_steps=50): context = [system_prompt(goal, tools)] for step in range(max_steps): action = model.generate(context) # think, then act if action.is_final_answer: return action.answer result = execute_tool(action.tool, action.args) # run it for real context.append(action) context.append(result) # observe, then loop return "budget exhausted"Python — the agent loop in its simplest form
This loop is deceptively simple. Over a handful of steps, current models perform impressively. The difficulty — and the entire research problem — is what happens over many steps.
39.2 Why Long Horizons Are Hard
Section titled “39.2 Why Long Horizons Are Hard”Errors compound. If a model takes a correct action 95% of the time, a ten-step task succeeds about 60% of the time, but a hundred-step task succeeds less than 1% of the time — the rithmetic of independent failures is unforgiving. Real long-horizon failure is subtler still: an agent loses track of its goal across a long context, repeats work it already did, fails to notice that an earlier step went wrong, or spirals into an unproductive loop. Three weaknesses dominate. Error recovery: an agent that cannot recognize and undo a mistake accumulates them. Memory: a goal and the relevant state must survive across a context that may span thousands of tool calls. And planning: knowing which sub-problem to attack next, and when to abandon a failing approach, requires judgment that pattern completion alone does not supply. Z.ai described exactly this when they noted that earlier models “exhaust their repertoire early” — applying familiar techniques for quick gains, then plateauing — and built GLM-5.1 and GLM-5.2 specifically to stay productive over hundreds of rounds and thousands of tool calls.
39.3 What Makes Agents Better
Section titled “39.3 What Makes Agents Better”Several lines of work attack these weaknesses. Reinforcement learning for agency optimizes the model for completing tasks rather than producing plausible single turns — the reward is task success over the whole trajectory, which directly trains recovery and persistence. This is where the asynchronous RL infrastructure of Chapter 19 (and Z.ai’s open slime system) becomes essential, because long agentic rollouts are slow and must be generated at scale. Better memory — summarizing old context, retrieving relevant past steps, maintaining an explicit scratchpad of goals and findings — keeps the agent oriented across long horizons. Interleaved and preserved thinking, where the model reasons before every action and stays consistent across turns, improves both planning and instruction-following. And richer environments and benchmarks — Terminal-Bench, SWE-bench, repository-generation and long-running operational tasks like the year-long simulated business of Vending Bench — measure genuine multi-step competence rather than one-shot accuracy, giving the field a target to optimize against.
| Failure mode | Symptom | Direction of work |
|---|---|---|
| Error compounding | small mistakes accumulate | RL on task success |
| Lost goal | drifts off task over time | memory, summarization |
| No recovery | cannot undo a wrong step | self-correction training |
| Premature plateau | repeats familiar tactics | long-horizon RL |
Table 39.1 — Failure modes of long-horizon agents and their remedies
39.4 Multi-Agent Systems
Section titled “39.4 Multi-Agent Systems”A further direction composes several agents rather than scaling one. A planner delegates sub-tasks to specialist workers; a critic reviews another agent’s output; agents with different tools or perspectives collaborate or debate. Multi-agent setups can decompose problems that overwhelm a single context and can catch errors through review, but they add coordination overhead and new failure modes of their own — agents that mislead one another, or collectively reinforce a wrong conclusion. Whether the future of autonomy is one increasingly capable agent or an organized system of many is, like much in this chapter, genuinely open.
From advisor to worker The agent loop is a few lines of code; the research is everything that makes it reliable over a long horizon. Today’s models are strong over a handful of steps and brittle over hundreds, undone by compounding error, lost goals, and an inability to recover. The work that matters — long-horizon RL, durable memory, better planning, and benchmarks that measure real multi-step competence — aims to turn models from things you consult into things you delegate to. It is among the most consequential frontiers precisely because reliable autonomy is what separates a useful assistant from an economic actor.
39.5 Worked Example: The Agent Loop with Tool Calls
Section titled “39.5 Worked Example: The Agent Loop with Tool Calls”Underneath the word “agent” is a plain loop: observe the state, decide on an action, execute it (often a tool call), fold the result back into context, and repeat until the task is done. Everything difficult about long-horizon autonomy lives in what this loop accumulates over many iterations:
context = [system_prompt, task]for step in range(max_steps): action = model.act(context, tools=TOOLS) # text or a tool call if action.is_final: return action.answer result = dispatch(action.tool, action.args) # run the chosen tool context = context + [action, result] # grow the trajectoryPython — agent/loop.py: observe, act, dispatch, repeat
Two properties of this loop explain why long-horizon agency is hard. First, errors compound: a wrong action at step 5 corrupts the context that every later step reasons over, so a small early mistake can cascade into total failure a hundred steps on — the precise weakness that the long-horizon RL of Section 39.3 targets. Second, the context grows without bound; after thousands of tool calls the trajectory can overflow even a million-token window, forcing summarization or retrieval that themselves lose information. The agent loop is trivial to write and brutal to make reliable, which is exactly why it is a frontier rather than a solved problem.
The loop is easy; the horizon is hard Observe–act–observe is a few lines. Keeping a model coherent across thousands of iterations of it — as early errors compound and context balloons — is the open problem that separates a demo from a dependable agent.
39.6 Memory and Context Management for Agents
Section titled “39.6 Memory and Context Management for Agents”The unbounded context growth of Section 39.5 is the practical wall that long-horizon agents hit first, and managing it is an open design space rather than a solved problem. As a trajectory lengthens past the model’s window, the agent must decide what to keep verbatim, what to compress, and what to set aside — and every choice risks discarding something that later turns out to matter.
Three families of strategy are in common use, each with a characteristic loss. Summarization folds older turns into a running synopsis, freeing space but blurring detail that a later step might need exactly. Retrieval stores the full history externally and pulls back only the fragments relevant to the current step, which preserves detail but depends on the retrieval query being good enough to find it. And an explicit scratchpad or external memory lets the agent deliberately write down what it judges important,
trading reliance on the model’s own judgment about what to record. None is lossless, and the failure modes — a summary that dropped the key fact, a retrieval that missed it, a scratchpad the model forgot to update — are exactly the kind of subtle, compounding errors (Section 39.5) that make long-horizon autonomy hard. Durable, lossless agent memory remains one of the genuinely unsolved problems of the frontier.
Every memory strategy loses something Summarize and you blur detail; retrieve and you depend on the query; use a scratchpad and you trust the model’s judgment about what to save. Managing an agent’s growing context is a space of lossy trade-offs, not a settled technique.
39.7 Multi-Agent Systems
Section titled “39.7 Multi-Agent Systems”An alternative to making one agent more capable is to compose several. In a multi-agent system, specialized agents collaborate on a task — a planner decomposes the goal, a coder implements, a critic reviews, each a language model with its own role and prompt, exchanging messages. The appeal is a division of labor that mirrors how human teams work, with the added benefit that one agent can critique another’s output (Section 38.4), catching errors a single agent would miss in its own work.
The benefits are real but so are the costs, and the balance is genuinely contested. Decomposition and self-criticism can improve quality and make complex workflows tractable, and independent agents can work in parallel. But coordination has overhead, errors compound as they pass between agents (the same fragility as Section 39.5, now across multiple hops), and every additional agent multiplies the inference cost. The empirical record is mixed: for some tasks a well-orchestrated multi-agent system clearly outperforms a single model, while for others a single strong agent with a good prompt matches or beats an elaborate multi-agent pipeline at a fraction of the cost. The frontier has not settled where the line falls, and a recurring lesson is to reach for multi-agent complexity only when a single agent has demonstrably failed — the architecture is a tool for specific hard problems, not a default that more agents always improves.
More agents is not automatically better Multi-agent systems offer ‘vision of labor and cross-criticism, but pay in coordination overhead, compounding inter-agent errors, and multiplied cost. Sometimes one strong agent wins; reach for multiple only when one has provably fallen short.
39.8 Tool-Use Protocols and Standardization
Section titled “39.8 Tool-Use Protocols and Standardization”As tool use (Section 39.5) moved from a research curiosity to a central capability, the ad hoc ways of describing tools and invoking them began to give way to standards. The pattern itself is stable: the model is given a description of each available tool — its name and the parameters it accepts — it emits a structured call (typically JSON, made well-formed by the constrained decoding of Section 27.9), the call is executed, and the result is fed back into the context for the model to continue.
What is changing is the move toward shared protocols for this interface. When every model and framework described tools and parsed calls in its own format, a tool built for one system had to be rebuilt for the next. Standardizing the schema for declaring tools and the format for calling them lets a tool be built once and used across many models and agents — the same interoperability win that a standardized model hub brought to distribution (Section 33.8), now for the actions a model can take. This matters because the value of tool use grows with the ecosystem: an agent is only as capable as the tools it can reach, and a shared protocol lets tools and agents from different sources compose, rather than each being locked to one platform. The trajectory is clear — from bespoke, per-model tool integration toward a common substrate over which a rich ecosystem of interoperable tools and agents can grow, which is likely how agentic systems (this chapter) move from impressive demos to dependable infrastructure.
Standard interfaces let tools compose Tool use follows a stable pattern — declare schemas, emit structured calls, feed back results — and is standardizing so a tool built once works across models and agents. As with the model hub, shared protocols turn isolated capabilities into an interoperable ecosystem.