Skip to main content

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

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_chunk updates
  • 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:
Module 23 uses the same annotations and lets Spring Boot discover the bean and manage its lifecycle instead.

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’s ChatClient, 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 GitHub

Running the Example

The demo launches the agent as a subprocess, asks it for a haiku about debugging, then asks it to turn that haiku into a limerick. The follow-up only works because the agent keeps per-session history.

Next Module

Module 26: Spring AI Chatbot — the same agent, with the model behind Spring AI’s ChatClient.