Manuscript Offprint

arXiv:2610.22273

Librarian Agents: Training LLMs to Curate Long-Context Workspaces that Anticipate Future Queries

Anikait Singh, Teresa Zhang, Yoonho Lee, Roshen Sanjay Nair, Aviral Kumar, Chelsea Finn

Open access. PaperVantage converted this article to text, so layout and figures may differ from the original.

Anikait Singh1,⋄^{1,\diamond}, Teresa Zhang1,⋄^{1,\diamond}, Yoonho Lee1,⋄^{1,\diamond}, Roshen Sanjay Nair1^{1}, Aviral Kumar2^{2} and Chelsea Finn1^{1}

1^{1}Stanford University, 2^{2}Carnegie Mellon University, ⋄\diamondEqual Contribution

Reasoning over long document contexts remains a challenge for large language models (LLMs). Standard retrieval methods are inherently reactive, bottlenecking agents with complex, cross-document reasoning at inference time, while precomputed structures like knowledge graphs often fall short because they rely on rigid heuristics. Instead, can we allow agents to perform anticipatory directed curation: learning to proactively reorganize unstructured corpora prior to inference to efficiently serve diverse future queries. We build on a key asymmetry in modern agentic harnesses (e.g., Claude Code): while LLM agents are adept at navigating existing filesystems to solve an immediate task, they are rarely trained to construct reusable workspaces for future use. To instantiate this capability, we introduce Librarian Agents, a framework in which a builder agent transforms raw documents into a hierarchical filesystem of synthetic files and reference links, which forms a workspace tailored for a downstream answering agent. We formulate workspace construction as a Markov decision process and optimize the builder policy via reinforcement learning to directly maximize downstream answering accuracy. Empirically, Librarian Agents outperforms the strongest retrieval and precomputation baselines by 33 percentage points on BrowseComp-Plus, amortizes upfront construction costs across repeated queries, and generalizes zero-shot to unseen domains on BRIGHT with a 31-point average accuracy improvement.

1. Introduction

Large language models (LLMs) are increasingly tasked with reasoning over extensive corpora of documents, often found in domains such enterprise knowledge bases, complex software repositories, and user chat histories, that exceed the context windows of today's LLMs. In these domains, the information required to complete a given task is rarely centralized; instead, it is dispersed and disconnected across many disparate sources. Thus, for each incoming request, agents repeatedly process and revisit the corpus from scratch. Can we instead enable systems to efficiently navigate these massive context spaces without incurring high, redundant computational costs each time a user poses a new query?

A standard approach to this challenge is retrieval-augmented generation (RAG) (Gao et al., 2023), in which a system responds to a query by searching raw documents, retrieving relevant passages, and synthesizing an answer. Recent work has strengthened this pipeline with multi-agent frameworks (Jiang et al., 2023; Asai et al., 2023; Yan et al., 2024; Edge et al., 2024; Singh et al., 2025). However, these methods remain largely reactive: each query triggers a new round of search, requiring the model to repeatedly rediscover useful cross-document relationships at inference time. Other approaches pre-structure the search space before inference, for example, as a knowledge graph (Peng et al., 2025). These methods can improve retrieval, but they typically commit to a rigid set of abstractions such as extracting named entities (Keraghel et al., 2024). Such a structure is useful when future queries precisely align with the index, but it is less natural when queries require new task-specific intermediate artifacts. A common underlying theme of these approaches is that they impose an inductive bias of structure, which a downstream agent can utilize when answering a given query. We ask can we learn to anticipate this structure for a new set of context that we encounter, curating these documents in a form that that an answering agents can readily use. We propose to directly train an agent to reorganize a corpus into an anticipatory answer-facing workspace so that future LLM agents can efficiently find the right evidence for a query?

Corresponding author(s): anikait@stanford.edu.

Project Website: https://librarian-agents.github.io/, Code: https://github.com/teresaz05/librarian-agents

Building Anticipatory Workspace

Figure

Traversing Constructed Workspace

Figure

Figure 1: Overview of Librarian Agents. (Left) We train a builder agent to construct a workspace consisting of synthetic files and directories that anticipates future queries for a downstream answering agent. (Right) This workspace is built once and repeatedly used to answer different queries.

Motivated by approaches that shift computation across different aspects of the model training and deployment process (Lin et al., 2025; Wu et al., 2026; Suzgun et al., 2025; Zhang et al., 2025a), we study anticipatory workspace construction: the problem of augmenting and reorganizing a corpus before future queries arrive. The goal is to shift part of the costs from query time to a precomputation phase, thereby amortizing these costs across many downstream queries. To instantiate this setting, we introduce Librarian Agents, a framework built on a key asymmetry in current agentic systems: LLM agents are effective at navigating existing hierarchical filesystems, but are rarely trained to proactively construct such workspaces for future use. Librarian Agents addresses this gap by tasking a builder agent with transforming source documents into a multi-level workspace with references to the original corpus. An frozen answering agent then navigates this workspace to answer an individual user query. We formalize the builder agent's operations as a Markov Decision Process (MDP) and directly optimize the workspace construction policy for downstream answering performance by posing it as a reinforcement learning (RL) problem on this MDP.

We demonstrate the efficacy of Librarian Agents on BrowseComp-Plus, Librarian Agents improves accuracy by an average of 33 percentage points over the best performing baseline. Furthermore, for BRIGHT, a domain which our builder agent is untrained on, gains persist over baselines with an average 31 point improvement over baselines.

In summary, our key contributions are:

• We formulate anticipatory workspace construction as a learnable model behavior: agents should materialize useful context as an answer-facing workspace before receiving user queries.

• We propose Librarian Agents, a framework where a builder agent synthesizes and organizes memory artifacts into a filesystem-based workspace for future queries.

• We formulate filesystem-based workspace construction as an MDP and optimize the builder agent via reinforcement learning to improve downstream query-answering performance.

• We evaluate Librarian Agents against retrieval and pre-computation baselines on BrowseComp-Plus and test zero-shot transfer on question-answering tasks derived from BRIGHT.

References (45)