Manuscript Offprint

arXiv:2609.00749

ContextPipe: Database-Inspired Context Assembly for Long-Horizon Agents

Peng Xu, Zuyu Zhang, Yuze Sun, Feng Tian, Long Wang, Chen Zhang

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

Abstract

Long-horizon large language model (LLM) agents require context assembly: the runtime must decide what to include in each prompt, in what order, and when to compact history under a hard context-window budget and a byte-sensitive prompt cache. In production agentic systems, this logic is scattered across prompt builders, ad hoc compaction routines, cache-break workarounds, and per-provider shims. We argue that context assembly is structurally isomorphic to query execution in a relational database: both execute under a hard budget, exploit a tiered cache, and leverage statistics. We adopt this discipline in CONTEXTPIPE: a five-phase pipeline (Plan → Bind → Optimize → Execute → Feedback) backed by a structured data-source catalog, a deterministic cache-aware optimizer, and an EXPLAIN ANALYZE trace. We show the context in CONTEXTPIPE is auditable, replayable, and failure-isolated. A preliminary evaluation using the SWE-bench Pro Qutebrowser subset shows that, compared to the append-only context construction policy, CONTEXTPIPE reduces total token volume by 31%, LLM calls by 23%, and response time by 9% at a cost in lower KV cache-hit ratio.

VLDB Workshop Reference Format: Peng Xu, Zuyu Zhang, Yuze Sun, Feng Tian, Long Wang, Chen Zhang. ContextPipe: Database-Inspired Context Assembly for Long-Horizon Agents. VLDB 2026 Workshop: ADS 2026: The Joint Workshop on Agentic Data Systems and Data-Centric AI (The 1st ADS & 3rd DATAI).

1 Introduction

A large language model (LLM) agent runs in a loop: it receives a user message, assembles a prompt, calls the model, interprets the response (often a tool call), executes any tools, and feeds the result into the next turn [21, 27]. Each turn is fundamentally a request-assembly problem: the runtime must collect system prompts, tool schemas, conversation history, retrieved memory, skill instructions, and runtime identity into one API call whose input size cannot exceed the model's context window, under latency and cost pressure from providers that charge per token

and cache byte-identical prefixes [2, 19]. Raw concatenation works for short exchanges, but it fails once histories or tool results dominate the prompt; beyond that point the runtime must actively decide what remains, what is summarized, and what is deferred [20], and it must do so again on every turn of a long-horizon task.

In practice, production agentic systems have accumulated a large set of context-handling components to make these decisions: prompt builders per session, compaction routines for history, provider-specific cache markers, and attachment injection for tool output. The result resembles a pre-optimizer database inside the agent runtime: each site makes local decisions that are individually reasonable but collectively opaque, and two of those decisions carry outsized risk. First, caching imposes a byte-stability constraint: automatic prefix caching [19, 29] reuses the longest byte-identical prefix shared with a prior request, so appending after a cached prefix is inexpensive but a one-byte change anywhere in a stable block invalidates the suffix and forces full recomputation of the prefix [14]. Second, ordering is consequential even where caching is unaffected: long-context models do not attend uniformly across the prompt, and content placed in the middle is systematically under-attended relative to content near the beginning or end [16], so section order affects output quality as well as cost—putting volatile memory late can protect the cache while also changing whether the model uses that memory in the current turn. Typical runtimes cannot explain why the assembler chose a particular context, what a better choice would have cost, or whether a cache broke because of a tool-schema edit, a session-header change, or compacted placeholder drift; there is no catalog of context sources by lifecycle, no deterministic policy that jointly handles cache placement and compaction, and no trace explaining what happened and why.

We observe that this resembles the problem that relational database systems solved half a century ago [10, 22]. A database optimizer takes a declarative intent (a SQL query) and a catalog (tables, indexes, statistics), produces a physical plan that respects hard resource limits (memory and I/O), exploits a tiered cache (buffer pool), and exposes its reasoning (EXPLAIN ANALYZE). A context assembler has a declarative intent (the user input mes-sage), an implicit catalog (session states, available sources and their lifecycles and costs), a hard resource limit (the context window), and a tiered cache (the prompt prefix cache). However, it typically lacks a formal structure that makes these analogues visible.

This paper presents CONTEXTPIPE, a runtime that plans, optimizes, and executes LLM context construction using database-inspired execution principles. Its contributions are:

  • A pipeline abstraction for LLM context assembly with five explicit phases: Plan, Bind, Optimize, Execute, and Feedback. These phases mirror the shape of a query executor and unify functionality that is often distributed across many scattered modules (Section 2).

  • A deterministic, cache-aware optimizer that frames context assembly as an ordered, budget-constrained placement problem and solves it using cache-aware ordered placement: a fixed ascending-volatility sort and tier-gated, individually-gated compaction transforms, every one of which is recorded for audit whether applied or skipped (Section 3).

  • A structured data-source catalog (CONTEXTSOURCES) that organizes the session states in a typical agent loop into eight lifecycle tiers, two of which are SESSION-LATCHES and EMERGENTCONTEXT, to make otherwise implicit behaviors first-class (Section 4).

  • Two mechanisms that let the pipeline adapt across turns instead of reacting only to the current prompt: predictive context pressure in Plan, which sizes the next response from historical percentiles rather than a fixed reserve, and TTL-guarded emergent context across Bind and Feedback, which safely carries mid-turn discoveries (e.g., a tool result that triggers a new skill) into the next turn without staleness or duplication (Sections 2, 4).

The rest of this paper is organized as follows. Section 2 presents the architecture. Section 3 presents the optimizer, Section 4 describes the catalog design, Section 5 presents the trace format, and Section 6 examines the implementation techniques. Section 7 reports the empirical evaluation between CONTEXTPIPE and append-only context construction on long-horizon agent tasks. Section 8 covers previous work on context assembly, and Section 9 concludes.