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. 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.
9. Next Steps
-
Explore the full AgentClient API documentation
-
Learn about specific agent implementations in Claude Code SDK
-
See practical examples in Sample Agents