LLM Agent Architecture Design and Systems Engineering Practices

    This article explores the architectural and systems engineering practices for building execution-oriented Large Language Model (LLM) agents. It covers the transition from conversational to execution models, the application of sparse expert architectures, hybrid attention mechanisms for long contexts, engineering essentials for million-token contexts, and the integration of multimodal inputs. Developers will gain insights into optimizing tool calling, state retention, load balancing, and inference system management.

    1. From Conversational Models to Execution Models

    Early large language models primarily focused on text generation and question-answering. As developers integrate these models into code repositories, terminals, browsers, and business systems, the evaluation criteria have shifted toward a new question: Can the model stably complete tasks across multiple steps? A single code fix often requires reading files, locating the issue, modifying the code, running tests, and making further modifications based on error messages. Each step depends on the previous one, and errors accumulate toward the final result.

    Consequently, the design focus of execution-oriented models differs from that of pure Q&A models. They must maintain stability in three key areas: format correctness for tool calling, state retention during long execution chains, and the ability to understand and correct based on environmental feedback. This article outlines the design approach for such models across four dimensions: architecture, training, deployment, and evaluation.

    2. Sparse Expert Architecture: Total vs. Activated Parameters

    The parameter size of large models is often directly equated with their capability. However, total parameters and the computational cost per inference are two distinct metrics. The sparse expert architecture splits the feedforward network into multiple expert sub-networks, with a router selecting a few experts to process each input token.

    This design involves two key metrics:

    • Total parameters: The storage size occupied by model weights, determining loading costs and the minimum VRAM requirement.
    • Activated parameters: The parameter scale actually involved in computing a single token, determining the computational consumption of a single inference.

    Consider a hypothetical example: if a model has 100 units of parameters and each token activates only 10 units, the activation ratio is 10%. The floating-point operations (FLOPs) during inference roughly correlate with the activated parameters, while the VRAM must still accommodate all parameters or their shards. Thus, the sparse architecture balances model capacity with per-inference cost, at the expense of more complex routing logic, communication overhead, and VRAM management.

    Router design also needs to address load balancing. If most tokens concentrate on a few experts, those experts become bottlenecks while others remain idle. Common practices include adding a load balancing term to the training loss, limiting the processing capacity of each expert, and applying constraints to routing results during inference. Engineering teams should monitor the hit distribution across experts; if a significant skew appears, they must adjust the training data or routing parameters.

    3. Hybrid Design of Attention Mechanisms

    The computational complexity of standard self-attention grows quadratically with sequence length, and the VRAM consumption of the KV cache grows linearly. When the context expands to hundreds of thousands or even millions of tokens, both costs become major constraints.

    A common industry approach is hybrid attention, which generally falls into three categories:

    1. Sparse attention: Each position only attends to a local window or a few key positions, reducing computational complexity.
    2. Linear attention: Reduces computational complexity to near-linear through kernel approximation or state recursion, though exact retrieval capability is somewhat affected.
    3. Interleaved full attention layers: Retains a few full attention layers between linear or sparse layers to supplement global information interaction.

    The core of hybrid design is division of labor. Local layers handle efficient language modeling, while global layers are responsible for long-distance information aggregation. The ratio of layers, window size, and placement of global layers are hyperparameters that must be determined experimentally; there is no universally optimal value.

    When evaluating hybrid attention, one should not solely look at average perplexity. Long-distance retrieval tests (e.g., inserting a key piece of information into a long text and asking the model to answer questions about it), multi-document Q&A, and cross-paragraph reasoning tasks are more effective at exposing the loss of global information.

    4. Engineering Essentials for Million-Token Contexts

    Expanding the context window to one million tokens involves four components: model structure, positional encoding, training data, and inference systems:

    • Positional encoding must maintain the distinguishability of relative positional information over long sequences. Common practices include scaling position frequencies or stage-wise length extension during training.
    • Training data must include real long documents, code repositories, and multi-turn dialogues. Short-text training cannot teach the model how to organize information over long distances.
    • The inference system must manage the KV cache, including paged storage, cache quantization, and on-demand eviction.
    • Input preprocessing needs to control costs, such as performing segment summarization or retrieval on ultra-long documents before handing them to the model.

    Window length is a capacity ceiling, not a performance guarantee. The actual usable length depends on the task type. When designing systems, developers should conduct length-segmented testing for critical tasks and record the curve of accuracy degradation as input length increases, rather than making decisions based solely on marketed maximum window sizes.

    5. Multimodal Input and Visual Self-Inspection Loop

    Multimodal models integrate text, images, audio, or vis…