Building Intelligent Agents in the Java Ecosystem: Framework Selection and Hands-On Lessons
This article examines how to move beyond a chat-only Java LLM application to a production-grade Agent. Based on practical experience running RAG, session memory, and basic function calling, it identifies five capabilities that determine project success: layered memory management, task scheduling, hot-updatable skills, protocol-based tool ecosystem reuse, and sandboxed code execution. Each section covers design decisions, pitfalls, and a checklist for evaluating Java Agent frameworks.
1. Background: Bridging the Gap Between “Talking” and “Getting Things Done”
Over the past two years, the Java community has produced a wave of frameworks for building applications around large language models. Some focus on providing a unified model abstraction layer that hides differences between model service APIs. Others center on function calling and tool orchestration. Still others are lightweight and dependency-minimal, making them suitable for embedded scenarios.
Debating which framework is “best” misses the point. What really determines a project’s success is whether the framework can support evolving a “chatbot that talks” into an “Agent that gets work done.”
Last year, the author built a working prototype on top of a mainstream model-access framework, covering RAG (retrieval-augmented generation), session-window memory, and basic function calling. To be honest, the prototype stage went smoothly. But as soon as real business requirements appeared, it became clear that several critical pieces were missing for something that could be called a proper Agent. This article does not compare specific frameworks to declare a winner. Instead, it abstracts five capabilities that no Java team can avoid when building an Agent, and walks through the design decisions and pitfalls for each.
2. Memory Management: From In-Memory Windows to Layered Persistent Memory
2.1 The Nature of the Problem
The default memory implementation in most frameworks is an in-memory sliding window: keep the most recent N conversation turns and discard everything older. This is fine for demos, but in production it causes two fatal problems:
- A process restart wipes all memory.
- Key facts the user mentioned earlier—preferences, identity, business context—are completely forgotten once they slide out of the window during a long conversation.
2.2 Layered Memory Design
We split memory into two layers:
- Short-term memory: the complete message history of the current session, persisted to a file or database keyed by session ID. It can be archived after the session ends.
- Long-term memory: structured facts extracted through summarization, such as “the user prefers a particular type of solution” or “the user’s project is an e-commerce system.” These are organized by user or tenant and injected into subsequent sessions via retrieval.
Illustrative pseudocode:
class AgentMemory {void append(Message msg) {shortTermStore.save(sessionId, msg);}// Consolidate long-term memory asynchronously after each turnCompletableFuture<Void> consolidate() {return summarizer.extractFacts(shortTermStore.recent(sessionId, 20)).thenAccept(facts -> longTermStore.upsert(userId, facts));}}
2.3 Pitfalls
First, memory injection needs budget control. Unbounded long-term memory entries will crowd out the context window; retrieve by relevance and cap the number of injected entries. Second, summarization itself consumes model calls, so run it asynchronously in batches rather than synchronously on every turn.
3. Task Scheduling: Giving the Agent a Calendar
An Agent that only responds passively has limited value. The more advanced form is an Agent that can accept a delegation such as “every day at 9 a.m., pull yesterday’s reports, analyze anomalies, and report back,” execute it autonomously, and complete the loop. This requires integrating one-off tasks, delayed tasks, and recurring tasks into unified scheduling.
In practice, you can reuse mature distributed task-scheduling libraries from the Java ecosystem and wrap one Agent “execution round” as a schedulable task. Key design points include:
- Task-context binding: persist the prompt, required skill list, and authorization scope in the task metadata so context is not lost when the task is triggered.
- Idempotency and retries: model calls are inherently unreliable. The task body must be idempotent, and retries must use a backoff strategy.
- Result-notification loop: push execution results back to the delegator via a message queue or webhook to close the loop.
4. Skills: Making Capabilities Pluggable and Hot-Deployable
A “Skill” is a unit of Agent capability: a bundle of prompt templates, a set of tool functions, and optionally input-validation rules. Early on, we hardcoded these into the service. Every new capability required a release, which severely slowed iteration.
The improvement direction is dynamic skill registration and hot plugging:
- Store skill packages as independent configuration or scripts in object storage; services watch for changes and hot-reload them.
- Mount skills on demand per request. Skills that are not needed do not enter the system prompt, saving tokens.
- Give each skill a clear input/output contract to make composition and testing easier.
Essentially, this turns “capabilities” from code assets into data assets, enabling operations staff to participate in capability building.
5. Tool Ecosystem Reuse: Connecting to Open Context Protocols
If every tool requires a hand-written adapter layer, Agent capability expansion will be slow. A widely adopted approach in the industry is to use a standardized model context protocol: tool providers and the Agent runtime are decoupled. The Agent acts as a client, dynamically discovering, registering, and calling external tool services at runtime.
Implementation recommendations:
- Dynamic registration: don’t hardcode the tool list in configuration. Support add/remove at runtime so tool changes take effect immediately.
- Unified authentication and timeouts: all external tool calls must converge on a unified gateway layer that controls timeouts, rate limiting, and auditing.
- Result normalization: external tools return heterogeneous data structures. Normalize them into message fragments in the adaptation layer to reduce the model’s comprehension burden.
6. Sandboxed Code Execution: Caging Generated Code
Letting the model “write code and run it itself” is a key leap in Agent capability—data analysis, batch processing, and prototype validation all depend on it. But executing model-generated code directly in the service process means entrusting system security entirely to the model’s “good behavior.” That is never acceptable.
We adopted a containerized sandbox approach:
- Isolated execution: each execution request launches a one-off container (scheduled by the container platform as needed), with hard limits on CPU, memory, and execution duration set at creation time.
- Environment presetting: preinstall common data-processing runtimes in the image, and support dynamic dependency installation per request (subject to whitelist validation).
- Result return: stdout/stderr, artifact files, and exit status are all returned so the Agent can decide the next action.
- Network policy: disable network access by default, or allow only an internal-network whitelist, to prevent data exfiltration and malicious requests.
Illustrative flow:
Model-generated code → security scan → schedule sandbox container → execute with limits → collect results → inject into conversation context
7. Selection Advice and Summary
Returning to the title’s question: which framework should you “like”? The author’s answer is: first clarify what form of Agent you intend to build, then evaluate how well candidate frameworks support the five capabilities above. A reasonable evaluation checklist:
- Memory: Does it support persistent extensions and custom storage?
- Scheduling: Can it integrate naturally with your existing task-scheduling system?
- Skills: Are capability definitions sufficiently declarative and hot-updatable?
- Tools: Does it support protocol-based dynamic tool registration?
- Execution: Does it provide or reserve a secure code-execution channel?
A framework is only the starting point. What truly determines an Agent’s ceiling is the engineering effort you invest across these five dimensions: memory, scheduling, capability extension, ecosystem reuse, and security isolation. I hope this approach provides a practical, actionable blueprint for teams currently navigating framework selection.
