I Built a Java Library for AI Agents. Here's Why It's Under 200KB.
There is a particular kind of frustration that comes from being a Java developer in the age of AI agents. You watch the Python world conjure intelligent systems with a few dozen lines, and you return to your IDE where the recommended solutions involve importing half of Spring Boot before you've written a single prompt. I felt this frustration keenly. So I did what any stubborn developer eventually does — I built something myself.
I called it AI Agent4J. It started life as gemini-react-java, a name so literal it embarrassed me slightly. It has since grown into something I'm genuinely proud of, though I try not to say so too loudly.
The Idea Was Simple. The Execution, Less So.
The central insight behind AI Agent4J is not original to me. It is borrowed from a paper — the ReAct framework — which describes how an LLM can reason through a problem by thinking, acting, observing the result, and thinking again. Thought, Action, Observation. A loop, like the way a good mechanic approaches an unfamiliar engine.
What I wanted was to make this loop available to Java developers without ceremony. No framework to inherit from. No annotation to memorize. Just a builder, a list of tools, and a call to agent.run().
ReActAgent agent = ReActAgent.builder()
.llmClient(client)
.addTool(new CalculatorTool())
.addTool(new CurrentTimeTool())
.maxIterations(10)
.build();
AgentResult result = agent.run("What is (15 * 23) + 47, and what time is it now?");
You can inspect every thought the agent had, every action it took, every observation it made. I find this deeply satisfying. It is, I think, how intelligent systems ought to behave — openly, accountably, without mystery.
On the Question of Which LLM
I built AI Agent4J primarily around Google Gemini, because that is the API key I had in my pocket. But I was careful — perhaps obsessively so — to ensure the library never truly depends on Gemini. There is a single interface, LLMClient, and anyone who implements it can plug in OpenAI, Anthropic, a locally running model, or something I haven't heard of yet.
I also added a RoutingLLMClient that automatically finds the cheapest capable provider and quietly handles rate limits when they come, which they always do.
The Features That Surprised Even Me
When you build a library gradually, over many evenings, you sometimes look up and find it has acquired capabilities you hadn't entirely planned. AI Agent4J now has:
Memory. Not just conversation history within a session, but semantic long-term memory — vector-backed, persisted to pgvector or running locally via ONNX — set up in a single line. The agent can remember, across conversations, that you prefer concise answers or work in the fintech industry.
Multi-agent orchestration. A manager agent can spawn isolated worker agents, delegate tasks to them, and collect results. I find this particularly elegant. It keeps each agent small and focused, and it keeps costs manageable.
Sarvam AI integration. This one I'm quietly most proud of. I integrated Sarvam AI's voice stack — text-to-speech, speech-to-text, translation — across more than ten Indian languages. A voice agent that listens in Hindi and responds in Tamil, typically in under two seconds, felt like something worth building.
Background scheduling. Agents can schedule their own future tasks. They become, in a small way, proactive.
RAG, knowledge graphs, OpenAPI tools, MCP support. The list grew longer than I intended, though the JAR remained stubbornly under 200KB. I considered this a personal victory.
What I Wanted to Avoid
I have spent time with Spring AI and LangChain4j. They are capable and well-maintained, and I mean no disrespect to the people who built them. But they carry weight — frameworks within frameworks, abstractions stacked upon abstractions. To do a simple thing, you must first learn many other things.
I wanted AI Agent4J to be the kind of library you can understand in an afternoon. Pure Java. Minimal concepts. The kind of code you can read without a guide.
I also cared, perhaps unfashionably, about explainability. The library is close to 95% compliant with xAI standards — audit logs, PII detection, bias monitoring hooks, versioned prompts. If you ever need to explain to someone what your agent did and why, you should be able to.
A Quiet Invitation
<dependency>
<groupId>io.github.srijithunni7182</groupId>
<artifactId>ai-agent4j</artifactId>
<version>0.1.0</version>
</dependency>
That is all that is required to begin. No parent POM. No starter module. No configuration class annotated with four different annotations whose purpose you will spend twenty minutes researching.
I built AI Agent4J because I wanted it to exist. If you are a Java developer who has felt, as I have, somewhat left out of the AI conversation — I hope you find it useful. It is small, it is tested, and it will not get in your way.
