AgentClient vs ChatClient
Spring AI Agents follows the same design patterns as Spring AI’s ChatClient
to provide a familiar experience for Spring developers. This page shows the similarities and differences between the two APIs.
1. API Comparison
1.1. Basic Usage
Both APIs follow the same fluent pattern for simple interactions:
ChatClient | AgentClient |
---|---|
|
|
1.2. Fluent Configuration
Both support fluent configuration with method chaining:
ChatClient | AgentClient |
---|---|
|
|
2. Key Differences
2.1. Purpose and Scope
ChatClient | AgentClient |
---|---|
Conversational AI - Text-in, text-out interactions - Stateless request/response model - No external tool access - Immediate responses |
Autonomous Goal Execution - Goal-oriented execution - Multi-step workflows with planning - Full tool access (files, commands, web) - Variable execution time |
2.2. Input/Output Model
ChatClient | AgentClient |
---|---|
Messages and Prompts
|
Goals and Results
|
2.3. Configuration Options
ChatClient | AgentClient |
---|---|
Model Parameters
|
Execution Environment
|
3. Spring Boot Integration
Both integrate seamlessly with Spring Boot through auto-configuration:
3.1. Configuration Classes
ChatClient Configuration | AgentClient Configuration |
---|---|
|
|
3.2. Controller Usage
ChatClient in Controller | AgentClient in Controller |
---|---|
|
|
4. Response Handling
4.1. Success/Failure Patterns
ChatClient | AgentClient |
---|---|
|
|
4.2. Metadata Access
ChatClient | AgentClient |
---|---|
|
|
5. When to Use Each
5.1. Use ChatClient When:
-
Conversational interactions - Q&A, explanations, content generation
-
Text processing - Summarization, translation, analysis
-
Immediate responses - Real-time chat, quick queries
-
Stateless operations - Each request is independent
Example use cases:
// Content generation
String blogPost = chatClient.prompt("Write a blog post about Spring Security").call()...;
// Data analysis
String summary = chatClient.prompt("Summarize this sales report: " + data).call()...;
// Q&A
String answer = chatClient.prompt("How do I configure Spring Data JPA?").call()...;
5.2. Use AgentClient When:
-
Goal execution - Code generation, refactoring, debugging
-
Multi-step workflows - Complex development tasks requiring planning
-
Tool interaction - File manipulation, command execution, web research
-
Project-specific work - Tasks requiring codebase understanding
Example use cases:
// Code generation
agentClient.run("Create a REST controller for User entity with CRUD operations");
// Debugging
agentClient.run("Find and fix the memory leak in the OrderService class");
// Refactoring
agentClient.run("Convert this project from JUnit 4 to JUnit 5");
6. Migration Between APIs
While the APIs are similar in structure, they serve different purposes. You typically won’t migrate between them, but rather choose the appropriate one for each use case:
@Service
public class DevelopmentAssistantService {
private final ChatClient chatClient;
private final AgentClient agentClient;
public DevelopmentAssistantService(ChatClient chatClient, AgentClient agentClient) {
this.chatClient = chatClient;
this.agentClient = agentClient;
}
// Use ChatClient for explanations
public String explainPattern(String patternName) {
return chatClient.prompt("Explain the " + patternName + " design pattern").call()
.getResult().getOutput().getContent();
}
// Use AgentClient for implementation
public String implementPattern(String patternName, String context) {
return agentClient.run("Implement " + patternName + " pattern in " + context)
.getResult();
}
}
7. Best Practices
7.1. Consistent Error Handling
Both APIs benefit from consistent error handling patterns:
@Component
public class AIService {
public String askQuestion(String question) {
try {
return chatClient.prompt(question).call()
.getResult().getOutput().getContent();
} catch (Exception e) {
log.error("Chat request failed: {}", e.getMessage());
throw new ServiceException("Unable to process question", e);
}
}
public String executeTask(String goal) {
try {
AgentClientResponse response = agentClient.run(goal);
if (response.isSuccessful()) {
return response.getResult();
} else {
throw new GoalExecutionException("Goal failed: " + response.getResult());
}
} catch (Exception e) {
log.error("Goal execution failed: {}", e.getMessage());
throw new ServiceException("Unable to execute goal", e);
}
}
}
7.2. Resource Management
Both clients are thread-safe and designed for reuse:
@Configuration
public class AIConfiguration {
// Both clients are singleton beans - thread-safe and efficient
@Bean
public ChatClient chatClient(ChatModel chatModel) {
return ChatClient.create(chatModel);
}
@Bean
public AgentClient agentClient(AgentModel agentModel) {
return AgentClient.create(agentModel);
}
}
8. Advisor Pattern Comparison
Both ChatClient and AgentClient support the same advisor pattern for intercepting and augmenting execution flows. This pattern is one of the most powerful similarities between the two APIs.
8.1. What are Advisors?
Advisors provide interception points that let you inject custom logic before and after AI model calls. They’re perfect for:
-
Adding context (RAG data for ChatClient, workspace info for AgentClient)
-
Logging and metrics collection
-
Security validation and filtering
-
Post-processing and evaluation
8.2. Advisor Registration
Both APIs use identical builder patterns for registering advisors:
ChatClient Advisors | AgentClient Advisors |
---|---|
|
|
8.3. Advisor Implementation
The advisor interface follows the same around-style advice pattern in both APIs:
ChatClient Advisor | AgentClient Advisor |
---|---|
|
|
8.4. Use Case Comparison
While the pattern is identical, the specific use cases differ based on the domain:
ChatClient Advisor Use Cases | AgentClient Advisor Use Cases |
---|---|
Context Augmentation
Post-Processing
Observability
|
Context Engineering
Post-Execution Evaluation (Judges)
Observability
|
8.5. Spring Boot Auto-Configuration
Both support automatic discovery of advisor beans:
ChatClient Auto-Configuration | AgentClient Auto-Configuration |
---|---|
|
|
8.6. Context Sharing Between Advisors
Both APIs provide mutable context maps for advisors to share data:
ChatClient Context | AgentClient Context |
---|---|
|
|
8.7. Advisor Ordering
Both use Spring’s Ordered
interface for execution order control:
// Same pattern for both ChatClient and AgentClient advisors
public class SecurityAdvisor implements AgentCallAdvisor {
@Override
public int getOrder() {
return Ordered.HIGHEST_PRECEDENCE; // Run first
}
}
public class MetricsAdvisor implements AgentCallAdvisor {
@Override
public int getOrder() {
return Ordered.LOWEST_PRECEDENCE; // Run last
}
}
8.8. Learn More About Advisors
-
AgentClient Advisors: See Agent Advisors API for complete documentation
-
ChatClient Advisors: Refer to Spring AI Advisor Documentation
The advisor pattern demonstrates the deep architectural alignment between AgentClient and ChatClient, making it easy to apply familiar Spring AI patterns to autonomous agent development.
9. Summary
AgentClient and ChatClient share the same Spring AI design philosophy and fluent API patterns, making them familiar to Spring developers. The key difference is their purpose: ChatClient for conversational AI interactions, AgentClient for autonomous goal execution.
Both APIs integrate seamlessly into Spring Boot applications and follow established Spring patterns for configuration, dependency injection, and error handling. The advisor pattern is nearly identical between the two, providing powerful extension points for both conversational and autonomous AI workflows.
10. Next Steps
-
Explore the full AgentClient API documentation
-
Learn about Agent Advisors in detail
-
Learn about specific agent implementations in Claude Code SDK
-
See practical examples in Sample Agents