Java AI Agent Development: Architecture Design for Memory, Skills, Protocols, and Sandboxing

    This article examines the architectural capabilities required for production-grade AI agents beyond RAG and basic function calling. It covers a two-layer file-based memory design, long-term task scheduling, dynamically assembled skill packages, standardized tool protocol integration, and containerized sandbox code execution, including practical trade-offs and a suggested implementation sequence.

    From Conversational Apps to Agents: Breaking Down Core Capabilities

    Running retrieval-augmented generation, conversation memory, and basic function calling with mainstream development frameworks is not difficult—most framework getting-started examples can already do this. However, if the goal is to build an agent that can run in production for a long time and autonomously complete complex tasks, these capabilities alone are far from enough. A proper agent needs at least five capabilities: persistent memory, long-term task scheduling, pluggable capability packages, standardized tool protocols, and a secure code execution environment. This article discusses the design approaches and practical trade-offs for each of these modules.

    1. Memory Management: From In-Memory Windows to a File-Based Two-Layer Architecture

    The in-memory session repository provided by frameworks is cleared when the process restarts. That is sufficient for demos but unacceptable for production-grade agents. Referring to the practices of mainstream agent products in the industry, a feasible approach is to divide memory into two layers and place both under a shared workspace directory:

    Short-term memory: Maintained automatically by the framework layer. Each user message and session reply is automatically appended to a conversation history file, and the context is injected during each conversation turn. The model does not need to manage this layer; the framework handles it entirely.

    Long-term memory: The model decides when to write and how to organize it. Typical forms include task notes, user preferences, and project background descriptions, stored as ordinary files in the workspace. The model maintains them through file read/write tools.

    The integration point for the two memory layers is this shared workspace directory:

    1. workspace/
    2. ├── conversations/ # Short-term conversation history (written automatically by the framework)
    3. │ └── chat-channel-a.yaml
    4. ├── notes/ # Long-term memory (written by the model)
    5. ├── tasks/ # Intermediate task artifacts
    6. └── artifacts/ # Generated files and deliverables

    The core advantage of this design is clear responsibility and no interference: short-term memory ensures contextual continuity, while long-term memory carries knowledge accumulated across sessions. In implementation, the framework’s Advisor mechanism can be used to inject history before messages enter the model and persist history after messages are returned.

    Two points deserve attention in practice. First, history injection must control length; when exceeding the window limit, summarize and compress rather than simply truncate. Second, file naming conventions for long-term memory should be specified in the system prompt; otherwise, model-generated file names can easily become chaotic and affect later retrieval.

    2. Task Scheduling: Giving Agents Long-Term Commitment Capability

    Conversational function calling can only handle requests in the present, while many real tasks span hours or even days: scheduled report generation, periodic data inspection, and delayed follow-up. Integrating a professional task scheduling library into the agent is a natural choice; one-off tasks, scheduled tasks, and cron-based recurring tasks should all be supported.

    The key design decision is that the task queue stores not “business logic to be executed” but “agent instructions to be triggered.” When the scheduled time arrives, the scheduler wakes the agent, and the agent autonomously decides how to execute based on the context at that time. In this way, task definitions remain declarative, and execution logic can evolve with model capabilities and business changes.

    Engineering details to note include: task state must be persisted to a database to survive service restarts; execution results should be written back to the workspace so the agent can reference them in later conversations; and long-running tasks should preferably persist intermediate artifacts to avoid starting over entirely after a single-point failure.

    3. Skill Packages: Hot-Pluggable Capabilities

    Skills are packaged capability units—a combination of prompt templates, tool definitions, and reference documentation. The traditional approach hardcodes all tools into the application, causing prompt bloat and reducing the model’s accuracy in selecting tools.

    A better approach is to dynamically assemble skill packages with each request: decide which skills to activate for a given session based on user request characteristics such as channel, tenant, and task type, without restarting the process. This brings several benefits:

    1. Context slimming: Only tool descriptions relevant to the current task are injected each time, reducing token consumption.
    2. Capability isolation: Different tenants and scenarios get what they need, avoiding semantic conflicts among tools.
    3. Agile iteration: New capabilities only require registering a new skill package without touching the core flow.

    During implementation, it is recommended to define skill packages as a standard structure containing metadata (name, description, trigger conditions) and content (tool set, guidance documents), with a registry managing the assembly logic centrally.

    4. Tool Protocols: Connecting to a Standardized Ecosystem

    Do not build tools from scratch for every agent capability. The industry already has a mature standardized tool protocol ecosystem, which contains many ready-made tool implementations for data queries, browser operations, file processing, and more. By enabling the agent to support such protocols and implement dynamic registration, the entire ecosystem can be reused directly.

    Specifically, build a protocol client into the agent runtime that supports mounting and unmounting remote tool services at runtime. Tool discovery, authentication, and invocation follow the protocol specification, and tool results are converted uniformly into the framework’s message format and injected into the conversation. This fully decouples tool maintainers from agent developers and greatly reduces extension costs.

    5. Sandboxed Code Execution: Contain Generated Code in Containers

    When the model needs to generate and actually run code for data analysis, chart generation, or batch processing, security is the first constraint. Code produced by the model must never be run directly in the application process. The correct approach is to build a sandboxed execution environment:

    • Container isolation: Each execution runs in a disposable container with limits on CPU, memory, and execution time.
    • Network and file system controls: Default to no network access or whitelist-based access, exposing only designated input and output directories.
    • Artifact collection: After execution, return stdout, generated files, and error information to the model in a structured form so it can iterate and correct.

    This is consistent with the implementation approach of the industry’s mainstream “code interpreter” capability: the model works in a loop of “write code—execute—inspect results—fix,” which can significantly improve completion rates for complex tasks.

    Conclusion

    Moving from a conversational application to a production-grade agent is essentially about answering five questions clearly: how memory is persisted and layered, how tasks are scheduled across time, how capabilities are assembled on demand, how tools are integrated in a standardized way, and how code is executed securely. Only by laying these foundations well and combining them with general cloud infrastructure services such as container platforms, object storage, and monitoring and alerting can you build an enterprise-grade agent system that can truly be entrusted with business operations.

    The recommended implementation order is: first complete file-based memory, then add task scheduling, gradually introduce skill packages and protocol tools, and finally add the sandboxed execution environment. Each step can independently validate value, avoiding the risk of a one-time refactor.