Module 25: AI Chatbot Agent
Give the echo agent a brain: the prompt handler calls Claude and streams the answer back. This is also the point where the tutorial switches to the@AcpAgent annotation API.
Prerequisites
- Completed Module 12: Echo Agent
- An
ANTHROPIC_API_KEYfrom console.anthropic.com. Unlike the client modules, this key is actually used:AnthropicOkHttpClient.fromEnv()authenticates with it - Java 17+
What You’ll Learn
- Writing an agent as annotated methods:
@Initialize,@NewSession,@Prompt - Running an annotated agent without Spring, using
AcpAgentSupport - Streaming model output to the client as
agent_message_chunkupdates - Keeping per-session conversation history so follow-up prompts have context
The Code
Module 12 used the fluent builder. Once the prompt handler does real work, a labelled@Prompt method reads better than a builder lambda. @Initialize and @NewSession are the same boilerplate as the echo agent; only @Prompt changes:
Running an annotated agent without Spring
AcpAgentSupport scans the instance for the annotated methods and wires each one to the transport. run() starts the agent and blocks until the client disconnects:
Echo Agent vs Chatbot Agent
The client coalesces consecutive
agent_message_chunk updates into one growing assistant message, so streaming needs nothing extra on the client side.
The non-streaming version
If you want the answer in one piece, the handler body is shorter:Not Locked to One Provider
The module calls the Anthropic Java SDK directly because it makes the AI call site obvious. The ACP handlers do not depend on it. Module 26 builds the same agent with Spring AI’sChatClient, and Module 27 with LangChain4j’s ChatModel.
To use the agent in an editor, point the IDE configuration from Module 29 at chatbot-agent.jar instead of echo-agent.jar.
Source Code
View on GitHubRunning the Example
Next Module
Module 26: Spring AI Chatbot — the same agent, with the model behind Spring AI’sChatClient.