The module-25 chatbot, built with LangChain4j — the other top Java AI framework. Same ACP agent, different way to reach the model.
| Module | How it calls the model |
|---|---|
| 25 | Raw Anthropic Java SDK (clearest call site) |
| 26 | Spring AI ChatClient |
| 27 (this) | LangChain4j ChatModel |
The ACP skeleton is identical to the echo agent. The only change from echo is the prompt handler:
String answer = model.chat(req.text()); // model is a LangChain4j ChatModel
context.sendMessage(answer);ChatModel is LangChain4j's provider-agnostic interface. We build an
AnthropicChatModel; swap the dependency + builder to use OpenAI, Gemini, or a
local Ollama model — the ACP handler doesn't change:
<!-- this module -->
<dependency>
<groupId>dev.langchain4j</groupId>
<artifactId>langchain4j-anthropic</artifactId>
</dependency>
<!-- swap for: langchain4j-open-ai / langchain4j-google-ai-gemini / langchain4j-ollama -->- Java 17
ANTHROPIC_API_KEYexported (used byAnthropicChatModel)
./mvnw package -pl module-27-langchain4j-chatbot -q
./mvnw exec:java -pl module-27-langchain4j-chatbotNote:
langchain4j-anthropicis fetched from Maven Central on first build.
This module keeps it to a single turn for clarity. LangChain4j adds memory via
MessageWindowChatMemory (or an AiServices interface with @MemoryId) — see the
LangChain4j docs.