Designing an Agent Runtime Framework: Using State Artifacts to Keep LLM Knowledge Bases Maintainable

    This article explores the engineering principles behind building a runtime framework—or agent harness—for large language models tasked with continuously maintaining a knowledge base. It explains why structuring state as external files (artifacts) is critical for long-running tasks, outlines a four-stage processing pipeline (ingestion, compilation, Q&A, and output generation), and discusses the human role in supervising automated systems. The guide also covers practical engineering practices such as permission minimization, output validation, and version control, alongside common pitfalls to avoid when designing such systems.

    Why the Runtime Framework Matters

    A large language model (LLM) only generates outputs from inputs. To handle multi-step tasks—such as continuously reading materials, organizing knowledge, and answering complex questions—you need an additional layer of engineering to manage context, invoke tools, save intermediate states, and constrain behavior. This layer is often referred to as the agent runtime framework, or agent harness.

    The quality of this framework directly determines whether a task can be completed reliably. When models have similar capabilities, performance differences often stem from three areas: how well context is organized, whether intermediate results are traceable, and whether the system can recover from failures at a checkpoint. Many teams have found that the same model can perform very differently under different frameworks. For this reason, framework design deserves attention as an independent engineering problem.

    Externalizing State as Artifacts

    The most common point of failure in long-running tasks is keeping state only in the conversation context. Once a conversation grows too long, early information gets truncated or diluted, and the task becomes difficult to continue. A more robust approach is to write the state of each step into files, making those files the source of truth for the task.

    These files are often called artifacts. Common forms include:

    • An index list of the original source materials;
    • Summaries of each document along with source information;
    • Concept entries and their cross-links;
    • Task execution logs and to-do lists.

    Taking Markdown files as an example, artifacts offer three key advantages. First, they are human-readable and editable, keeping review costs low. Second, they can be placed under version control, so every change is recorded and rollback is possible after errors. Third, a model in a new session only needs to read the relevant files to resume work, without relying on lengthy conversation history.

    A Four-Stage Processing Pipeline

    A typical knowledge base system can be broken down into four stages.

    Document Ingestion

    The goal of the ingestion stage is to bring heterogeneous materials into a readable, organized directory. It is recommended to establish a clear directory structure:

    1. knowledge-base/
    2. ├── raw/ # Original materials, read-only, no modifications
    3. │ ├── articles/
    4. │ ├── papers/
    5. │ └── assets/ # Localized images
    6. ├── wiki/ # Knowledge entries generated by the model
    7. │ ├── index.md
    8. │ └── concepts/
    9. └── outputs/ # Q&A and visualization results

    Practical recommendations:

    1. Keep original materials read-only. Write all processed results to other directories to preserve traceability.
    2. When converting web pages to Markdown, keep the original links and the time of retrieval.
    3. Download images locally and reference them with relative paths to prevent broken content when external links become unavailable.

    Knowledge Compilation

    In the compilation stage, the model reads documents from the raw directory and produces a structured wiki. A reliable compilation workflow generally includes the following steps:

    1. def compile_wiki(raw_dir: str, wiki_dir: str) -> None:
    2. for doc in list_documents(raw_dir):
    3. summary = summarize(doc) # Generate a summary
    4. write_summary(wiki_dir, doc, summary) # Save the summary file
    5. concepts = extract_concepts(summary) # Extract concepts
    6. for concept in concepts:
    7. append_backlink(wiki_dir, concept, doc) # Maintain backlinks
    8. rebuild_index(wiki_dir) # Rebuild the index file

    The focus of compilation is concept categorization. If the same concept appears in multiple documents, it should be merged into a single entry that lists all sources. This reduces duplication and makes it easier to locate complete information during retrieval.

    Q&A and Retrieval

    When a knowledge base is relatively small, the retrieval approach can stay simple. The model first reads the index file and short summaries of each document to judge which ones are relevant to the question. It then opens the full text of those documents for close reading before answering. For knowledge bases with only dozens to a hundred documents and a total volume of a few hundred thousand characters, this approach is usually sufficient. It also avoids the maintenance cost of building and updating vector indexes.

    As the scale grows beyond a single context window, you may need chunked retrieval, vector recall, or multi-level indexing. Choose the approach based on actual scale and query complexity rather than defaulting to the most complex option.

    When answering questions, the model should cite its sources and clearly distinguish between conclusions explicitly stated in the materials and inferences made by the model itself. This distinction is especially important for research-oriented tasks.

    Output Generation

    Knowledge base outputs should not be limited to chat text. Saving results as files makes them reusable and shareable. Common formats include:

    • Markdown reports for long-form analysis;
    • Slide markup files that can be rendered directly into presentations;
    • Charts generated by plotting scripts for data trends;
    • Structured data such as JSON or CSV for downstream programmatic processing.

    Output files should be saved to the outputs directory, along with metadata recording the question that generated them, the list of referenced documents, and the generation time.

    The Role of Humans in the Loop

    Human involvement in an agent system can be classified by the depth of intervention into two modes.

    In the in-the-loop mode, a human reviews the output of every step before deciding whether to continue. This approach offers strong control but lower efficiency. It is suitable for high-risk tasks or first-time runs.

    In the on-the-loop mode, the system runs autonomously while a human periodically checks artifacts and metrics. The human intervenes only when deviations are found, by adjusting rules or aborting the task. This mode works better for long-cycle, repetitive knowledge maintenance work.

    Both modes share a common requirement: artifacts must be readable, state must be inspectable, and operations must be reversible. Only under these conditions can humans supervise effectively rather than passively accepting results.

    Key Practices for Engineering Implementation

    The following principles can significantly reduce risk when building such systems.

    Principle of least privilege. The model should only have file read/write access required for the task. The raw directory should be read-only, and compilation scripts should not have permission to delete original materials.

    Keep secrets out of repositories. Access credentials for model services should be provided through environment variables or a key management service, not written into configuration files or the knowledge base repository.

    Validate outputs. After compilation, check that links are valid, required fields are complete, and no duplicate entries exist for the same concept. These checks can be performed with simple scripts and do not require model judgment.

    Use version control. Place the wiki and outputs under version control, committing each batch compilation as a single commit. If quality degrades, you can quickly identify the specific change responsible.

    Control costs. Compile incrementally by document, processing only newly added or modified materials instead of running a full rebuild every time.

    Common Pitfalls

    First, cramming all content into a single conversation. Context windows are limited, and information utilization from long contexts is not stable. Externalize state into files and read them on demand.

    Second, introducing complex retrieval too early. At small scales, an index plus summaries is often sufficient. Additional vector components only add maintenance overhead.

    Third, skipping manual spot checks. Even in automated pipelines, periodically sample and review generated summaries and concept categorizations. When systematic errors appear, correct the prompts or rules promptly.

    Fourth, ignoring source attribution. Conclusions without cited sources are difficult to verify. Over time, this erodes the credibility of the entire knowledge base.

    Summary

    The core of an agent runtime framework lies in making state visible, processes controllable, and results reusable. A pragmatic engineering path is to externalize task state into files, organize the pipeline into staged phases of ingestion, compilation, Q&A, and output, and select retrieval methods based on scale. The human role shifts from step-by-step operator to rule setter and result reviewer. A knowledge base built along these lines reduces repetitive work and allows research accumulation to grow continuously.