The bug is in createAgenticSystem method - it's registering non-dynamic ToolProvider instances multiple times, causing the activate_skill tool to be registered multiple times.
Let me understand the flow better. When AgenticServices.createAgenticSystem() is called, it:
- Creates an agentic system via
createComposedAgent() or agentBuilder()
- Each agent may have tool providers registered
The issue is that the activate_skill tool is added for every SkillProvider instance, causing duplicate registration even with a single agent.
Looking at the stack trace:
Caused by: dev.langchain4j.service.IllegalConfigurationException: Duplicated definition for tool: activate_skill
at dev.langchain4j.service.tool.ToolService.addTools(ToolService.java:398)
The error happens in ToolService.addTools() when putIfAbsent finds an existing key.
Let me trace the code path:
createAgenticSystem is called
- It calls
createBuiltInAgentExecutor which finds the agent type and builds it
- The agent builder registers tool providers
- When
configureAgent is called in DeclarativeUtil, it looks for @ToolProviderSupplier and calls agentBuilder.toolProvider()
- This adds the ToolProvider to the list
- When the agent is built, the AiServices builds and adds all tool providers
Wait, let me re-read the code more carefully. The issue is about how tools are being registered multiple times.
Looking at the createAgenticSystem method:
public static <T> T createAgenticSystem(
Class<T> agentServiceClass,
ChatModel chatModel,
AgentConfigurator agentConfigurator) {
T agent = createComposedAgent(agentServiceClass, chatModel, agentConfigurator);
if (agent == null) {
var agentBuilder = agentBuilder(agentServiceClass);
configureAgent(agentServiceClass, chatModel, agentBuilder, agentConfigurator);
agent = agentBuilder.build();
}
...
}
So if createComposedAgent returns null, it creates an agent builder, configures it, and builds it.
Now let me think about what happens when there's a @ToolProviderSupplier method:
configureAgent is called in DeclarativeUtil
- It looks for
@ToolProviderSupplier and calls agentBuilder.toolProvider(invokeStatic(method))
- This adds the tool provider to the AgentBuilder's list
- When the agent is built, AiServices is configured with these tool providers
But wait, the issue says "multiple registrations of non-dynamic ToolProvider instances". Let me trace through the AgentBuilder.build() method:
private void setupTools(AiServices<T> aiServices) {
if (objectsWithTools != null) {
aiServices.objectsWithTools(objectsWithTools);
}
if (toolsMap != null) {
aiServices.tools(toolsMap);
}
if (toolProviders != null && !toolProviders.isEmpty()) {
aiServices.toolProviders(toolProviders);
}
...
}
So the tool providers are passed to AiServices. And then in AiServices, they get added to the ToolService.
Let me look at how AiServices handles tool providers...
Actually, looking more carefully at the bug description:
The bug is caused by createAgenticSystem registering non-dynamic ToolProvider instances multiple times.
So the issue is that the same ToolProvider is being registered multiple times when calling createAgenticSystem.
But looking at the code, configureAgent is only called once, and the ToolProvider is only added once to the AgentBuilder...
Wait, let me re-read the stack trace again:
at dev.langchain4j.service.tool.ToolService.addTools(ToolService.java:398)
at dev.langchain4j.service.tool.ToolService.lambda$createContextFromStaticToolsAndProviders$0(ToolService.java:378)
This is in createContextFromStaticToolsAndProviders, which iterates over toolProviders and calls addTools for each non-dynamic provider.
The issue is that the same ToolProvider instance is being added multiple times to the list of toolProviders.
Wait, let me look at how the createComposedAgent method works again...
Actually, looking at the buildSequentialAgent method and other similar methods:
private static <T> T buildSequentialAgent(
Class<T> agentServiceClass,
Method agentMethod,
ChatModel chatModel,
AgentConfigurator agentConfigurator) {
SequenceAgent annotation = agentMethod.getAnnotation(SequenceAgent.class);
var builder = sequenceBuilder(agentServiceClass)
.subAgents(createSubagents(annotation.subAgents(), chatModel, agentConfigurator));
...
}
This calls createSubagents, which calls createSubagent for each subagent class. And in createSubagent:
private static AgentExecutor createSubagent(
Class<?> subgentClass, ChatModel chatModel, AgentConfigurator agentConfigurator) {
...
AgentExecutor agentExecutor = createBuiltInAgentExecutor(subgentClass, chatModel, agentConfigurator);
if (agentExecutor != null) {
return agentExecutor;
}
AgentBuilder<?, ?> agentBuilder = agentBuilder(subgentClass);
configureAgent(subgentClass, chatModel, agentBuilder, agentConfigurator);
return agentToExecutor(agentBuilder.build());
}
So for subagents, configureAgent is called which registers the tool providers.
But I'm still not seeing how the same ToolProvider would be registered multiple times for a single agent...
Wait, let me re-read the issue more carefully:
even with a single agent
So this is about a single agent case, not about subagents. Let me look at the createComposedAgent method again...
When createComposedAgent returns null (because the agent is not a composed agent like SequenceAgent, LoopAgent, etc.), the code falls through to:
var agentBuilder = agentBuilder(agentServiceClass);
configureAgent(agentServiceClass, chatModel, agentBuilder, agentConfigurator);
agent = agentBuilder.build();
In this case, the agent is just a simple agent with @Agent annotation. The configureAgent method in DeclarativeUtil is called, which handles @ToolProviderSupplier and adds the tool provider to the AgentBuilder.
But wait, the user says they're using @ToolProviderSupplier:
@ToolProviderSupplier
static ToolProvider testTool() {
return Skills.builder()
.skills(Skill.builder().name("test").description("test").content("test").build())
.build().toolProvider();
}
So when configureAgent is called, it finds the @ToolProviderSupplier method and adds the tool provider. This should only happen once...
Let me check if there's something in the agent builder that might be registering it multiple times...
Actually, I just realized something. Let me look at the stack trace again:
at dev.langchain4j.service.tool.ToolService.addTools(ToolService.java:398)
at dev.langchain4j.service.tool.ToolService.lambda$createContextFromStaticToolsAndProviders$0(ToolService.java:378)
This suggests that the toolProviders list in ToolService contains multiple entries that return the same activate_skill tool.
Let me look at what the Skills.toolProvider() method returns:
public ToolProvider toolProvider() {
return toolProvider;
}
And in createToolProvider:
private ToolProvider createToolProvider(Builder builder) {
...
return new ToolProvider() {
@Override
public ToolProviderResult provideTools(ToolProviderRequest request) {
...
return skillManagementResult;
}
@Override
public boolean isDynamic() {
return !skillScopedProviders.isEmpty();
}
};
}
So for a Skills object with only one skill and no resources, the tool provider is non-dynamic (returns false from isDynamic).
Now, the issue is that this same ToolProvider is being registered multiple times. But how?
Wait, let me look at the issue more carefully. The user says:
The activate_skill tool is automatically added for every SkillProvider instance
Hmm, but the user's code doesn't show any SkillProvider. It shows a @ToolProviderSupplier that returns a Skills.toolProvider().
Let me search for SkillProvider:
Actually, looking at the issue description again, I think the key insight is:
The bug is caused by createAgenticSystem registering non-dynamic ToolProvider instances multiple times.
Let me look at the code flow more carefully. When createAgenticSystem is called for a simple agent (not a SequenceAgent, LoopAgent, etc.):
createComposedAgent(agentServiceClass, chatModel, agentConfigurator) is called
- This returns null because the agent doesn't have any sequence, loop, etc. annotation
- Then
agentBuilder(agentServiceClass) is called
- Then
configureAgent(agentServiceClass, chatModel, agentBuilder, agentConfigurator) is called
- Then
agentBuilder.build() is called
In step 4, configureAgent finds the @ToolProviderSupplier method and adds the tool provider.
But wait, let me check what agentBuilder(Class<T> agentServiceClass) does...
public static <T> AgentBuilder<T, AgentBuilder<T, ?>> agentBuilder(Class<T> agentServiceClass) {
return new AgentBuilder<>(agentServiceClass);
}
And the AgentBuilder constructor:
public AgentBuilder(Class<T> agentServiceClass) {
this.agentServiceClass = agentServiceClass;
this.agenticMethod = validateAgentClass(agentServiceClass);
...
configureAgent(agentServiceClass, this);
...
}
There it is! The AgentBuilder constructor calls configureAgent(agentServiceClass, this). And then createAgenticSystem also calls configureAgent(agentServiceClass, chatModel, agentBuilder, agentConfigurator).
So configureAgent is called twice:
- Once in the
AgentBuilder constructor
- Once in
createAgenticSystem
And each time it calls agentBuilder.toolProvider(invokeStatic(method)), which adds the tool provider to the list.
So the same tool provider is added twice to the list, and when the agent is built, the ToolService tries to add both instances, causing the duplicate error.
The fix should be to not call configureAgent again in createAgenticSystem if the agent was already configured by the AgentBuilder constructor. Or to use a different approach that doesn't result in duplicate registrations.
Let me verify this by looking at the AgentBuilder constructor again:
public AgentBuilder(Class<T> agentServiceClass) {
...
configureAgent(agentServiceClass, this);
...
}
And configureAgent(Class<?> agentType, AgentBuilder<?, ?> agentBuilder):
public static void configureAgent(Class<?> agentType, AgentBuilder<?, ?> agentBuilder) {
configureAgent(agentType, null, true, agentBuilder, AgenticServices.AgentConfigurator.empty());
}
So yes, the AgentBuilder constructor calls configureAgent, and then createAgenticSystem calls it again:
var agentBuilder = agentBuilder(agentServiceClass);
configureAgent(agentServiceClass, chatModel, agentBuilder, agentConfigurator);
agent = agentBuilder.build();
The fix should be to NOT call configureAgent again in createAgenticSystem when using agentBuilder(agentServiceClass), since the agent builder was already configured in its constructor.
Actually wait, let me check if there's a difference between agentBuilder(agentServiceClass) and agentBuilder():
Looking at the code:
public static <T> AgentBuilder<T, AgentBuilder<T, ?>> agentBuilder(Class<T> agentServiceClass) {
return new AgentBuilder<>(agentServiceClass);
}
So agentBuilder(agentServiceClass) creates a new AgentBuilder which already configures itself.
But then in createAgenticSystem:
var agentBuilder = agentBuilder(agentServiceClass);
configureAgent(agentServiceClass, chatModel, agentBuilder, agentConfigurator);
agent = agentBuilder.build();
This calls configureAgent again, which adds the tool providers again.
The fix is simple: don't call configureAgent again if the agent was already configured by the AgentBuilder constructor.
One way to fix this is to just not call configureAgent at all in this path. But we need to ensure the chatModel and agentConfigurator are still applied.
Actually, looking at the overloaded configureAgent method:
public static void configureAgent(Class<?> agentType, ChatModel chatModel, AgentBuilder<?, ?> agentBuilder, AgenticServices.AgentConfigurator agentConfigurator) {
configureAgent(agentType, chatModel, false, agentBuilder, agentConfigurator);
}
This version accepts a chatModel and sets allowNullChatModel = false. The difference between true and false is whether a ChatModel is required.
So the fix could be:
- Don't call
configureAgent at all in this path (since it was already called in the AgentBuilder constructor)
- Instead, just set the chatModel on the agentBuilder if it wasn't already set
- Apply the agentConfigurator
Or another approach:
- Don't call
agentBuilder(agentServiceClass) (which triggers constructor-based configuration)
- Create a new AgentBuilder manually and then call configureAgent once
Let me think about the cleanest fix...
Actually, I think the cleanest fix is to modify the createAgenticSystem method to not call configureAgent again. The AgentBuilder constructor already calls configureAgent(agentServiceClass, this), so the tool providers and other configurations are already set up. We just need to override the chatModel if it was passed explicitly.
But wait, there's a subtle difference. The constructor calls configureAgent(agentType, null, true, agentBuilder, AgenticServices.AgentConfigurator.empty()), which allows a null ChatModel. But in createAgenticSystem, we want to use the provided chatModel.
So the fix should be:
- Don't call
configureAgent again (since it would duplicate tool provider registrations)
- Just set the chatModel on the agentBuilder if it wasn't already configured
- Apply the agentConfigurator (but this is tricky because configureAgent already handles it)
Actually, let me re-read the configureAgent method to see what it does with agentConfigurator:
private static void configureAgent(Class<?> agentType, ChatModel chatModel, boolean allowNullChatModel, AgentBuilder<?, ?> agentBuilder, AgenticServices.AgentConfigurator agentConfigurator) {
...
agentConfigurator.configurator().accept(new DefaultDeclarativeAgentCreationContext(agentType, agentBuilder));
}
The agentConfigurator is only used at the end. And the ChatModel is used to set the model on the agentBuilder if no @ChatModelSupplier is found.
So the fix could be:
- If the agent already has a @ChatModelSupplier, we don't need to do anything
- If not, we need to set the chatModel on the agentBuilder
- We should still apply the agentConfigurator
But this is getting complex. A simpler fix might be to just track whether the agent has already been configured and skip the duplicate call.
Actually, the simplest fix is to not call configureAgent at all in the createAgenticSystem method for the simple agent case. The AgentBuilder constructor already handles all the configuration. If a chatModel was passed, it can be set on the agentBuilder directly. And if agentConfigurator needs to be applied, it can be applied directly without going through configureAgent.
Let me draft the fix:
if (agent == null) {
var agentBuilder = agentBuilder(agentServiceClass);
// AgentBuilder constructor already calls configureAgent(agentServiceClass, this)
// So we don't need to call it again - that would duplicate tool provider registrations
// Instead, just set the chatModel if needed and apply the configurator
if (chatModel != null && !hasChatModel(agentBuilder)) {
agentBuilder.chatModel(chatModel);
}
if (agentConfigurator.configurator() != null) {
agentConfigurator.configurator().accept(new DefaultDeclarativeAgentCreationContext(agentServiceClass, agentBuilder));
}
agent = agentBuilder.build();
}
Wait, but the hasChatModel method is private on AgentBuilder. Let me check...
Looking at AgentBuilder:
boolean hasChatModel() {
return chatMemory != null || chatMemoryProvider != null;
}
Hmm, this checks for chatMemory, not chatModel. And model and chatModelProvider are private fields.
Actually, let me think about this differently. The issue is that when agentBuilder(agentServiceClass) is called, the AgentBuilder constructor calls configureAgent(agentServiceClass, this) with allowNullChatModel = true. This means if no @ChatModelSupplier is found, no chatModel is set on the agentBuilder.
But in createAgenticSystem, we want to use the provided chatModel. So we need to set it on the agentBuilder.
The cleanest approach might be:
- Check if the agent already has a @ChatModelSupplier by looking at the class
- If not, set the chatModel on the agentBuilder
But this adds complexity. A simpler approach is to just always set the chatModel on the agentBuilder if it was passed to createAgenticSystem, even if the agent already has one. The last one set would win.
Actually, looking more carefully at the code flow:
In createAgenticSystem:
T agent = createComposedAgent(agentServiceClass, chatModel, agentConfigurator);
if (agent == null) {
var agentBuilder = agentBuilder(agentServiceClass);
configureAgent(agentServiceClass, chatModel, agentBuilder, agentConfigurator);
agent = agentBuilder.build();
}
The createComposedAgent method also passes chatModel and agentConfigurator to its internal methods. So those composed agents handle chatModel and agentConfigurator correctly.
For the simple agent case (when createComposedAgent returns null), the fix should prevent double configuration.
Actually, let me look at how the chatModel flows into createComposedAgent:
private static <T> T createComposedAgent(
Class<T> agentServiceClass,
ChatModel chatModel,
AgentConfigurator agentConfigurator) {
...
Optional<Method> sequenceMethod = getAnnotatedMethodOnClass(agentServiceClass, SequenceAgent.class);
if (sequenceMethod.isPresent()) {
return buildSequentialAgent(agentServiceClass, sequenceMethod.get(), chatModel, agentConfigurator);
}
...
}
So createComposedAgent passes chatModel to the build methods. And in buildSequentialAgent:
private static <T> T buildSequentialAgent(
Class<T> agentServiceClass,
Method agentMethod,
ChatModel chatModel,
AgentConfigurator agentConfigurator) {
SequenceAgent annotation = agentMethod.getAnnotation(SequenceAgent.class);
var builder = sequenceBuilder(agentServiceClass)
.subAgents(createSubagents(annotation.subAgents(), chatModel, agentConfigurator));
...
}
And createSubagents calls createSubagent:
private static AgentExecutor createSubagent(
Class<?> subgentClass, ChatModel chatModel, AgentConfigurator agentConfigurator) {
...
AgentExecutor agentExecutor = createBuiltInAgentExecutor(subgentClass, chatModel, agentConfigurator);
if (agentExecutor != null) {
return agentExecutor;
}
AgentBuilder<?, ?> agentBuilder = agentBuilder(subgentClass);
configureAgent(subgentClass, chatModel, agentBuilder, agentConfigurator);
return agentToExecutor(agentBuilder.build());
}
Hmm, so for subagents, the same pattern exists: agentBuilder(subgentClass) followed by configureAgent(...). This would also cause the same duplicate registration for subagents!
But the issue says "even with a single agent", so maybe the subagent case doesn't trigger the error for some reason.
Wait, let me re-read the issue:
even with a single agent
OK so it's a single agent with no subagents. So the issue is specifically in the simple agent case.
Let me also check if there's a similar issue in createBuiltInAgentExecutor:
public static AgentExecutor createBuiltInAgentExecutor(Class<?> agentServiceClass) {
return createBuiltInAgentExecutor(agentServiceClass, declarativeChatModel(agentServiceClass), AgentConfigurator.empty());
}
private static AgentExecutor createBuiltInAgentExecutor(
Class<?> agentServiceClass,
ChatModel chatModel,
AgentConfigurator agentConfigurator) {
...
if (!agentServiceClass.isInterface()) {
Method agenticMethod = nonAiAgentMethod(agentServiceClass);
if (agenticMethod != null) {
...
}
}
return null;
}
This method doesn't create an AgentBuilder, so no duplicate configuration there.
OK so the fix is clear: in createAgenticSystem, don't call configureAgent again after creating an AgentBuilder, because the AgentBuilder constructor already calls it.
Here's my plan:
- In
createAgenticSystem, when calling agentBuilder(agentServiceClass), don't also call configureAgent
- Instead, just set the chatModel on the agentBuilder if it wasn't already set
- Apply the agentConfigurator directly
But there's a subtlety: the AgentBuilder constructor calls configureAgent(agentServiceClass, this) which internally calls agentConfigurator.configurator().accept(...) with an empty configurator. So if we want to apply the actual agentConfigurator, we need to do it separately.
Let me think about the simplest fix...
Actually, looking at the code again:
public static void configureAgent(Class<?> agentType, AgentBuilder<?, ?> agentBuilder) {
configureAgent(agentType, null, true, agentBuilder, AgenticServices.AgentConfigurator.empty());
}
This calls configureAgent with an empty agentConfigurator. And then in createAgenticSystem:
configureAgent(agentServiceClass, chatModel, agentBuilder, agentConfigurator);
This calls configureAgent again with the provided agentConfigurator. So the agentConfigurator is applied twice (once with empty configurator in the constructor, once with the actual configurator in createAgenticSystem).
Wait no, that's not right. The constructor calls configureAgent(agentServiceClass, this) which applies AgenticServices.AgentConfigurator.empty(). Then createAgenticSystem calls configureAgent(agentServiceClass, chatModel, agentBuilder, agentConfigurator) which applies the actual agentConfigurator.
So the issue is just with the tool provider registration, not the agentConfigurator. The agentConfigurator is actually applied correctly (once with empty, once with actual, but the actual one is what we want).
The fix should focus on preventing the duplicate tool provider registration.
Here's a cleaner approach:
if (agent == null) {
var agentBuilder = agentBuilder(agentServiceClass);
// AgentBuilder constructor already called configureAgent(agentServiceClass, this)
// which registered tool providers and other declarative annotations.
// Calling configureAgent again would cause duplicate registration.
// So we only set the chatModel if one was provided and the agentBuilder doesn't already have one.
if (chatModel != null) {
agentBuilder.chatModel(chatModel);
}
// Apply the agentConfigurator
if (agentConfigurator.configurator() != null) {
agentConfigurator.configurator().accept(new DefaultDeclarativeAgentCreationContext(agentServiceClass, agentBuilder));
}
agent = agentBuilder.build();
}
Wait, but I need to check if setting chatModel after the constructor would override a @ChatModelSupplier. Let me check how AiServices handles this...
Actually, looking at AiServices, the chatModel can be set at any time before build(), and it will be used. If @ChatModelSupplier was used in configureAgent, it would set a chatModelProvider on the AgentBuilder. Then if we call agentBuilder.chatModel(chatModel), it would set the model field.
In the AgentBuilder's configureChatModel method:
private void configureChatModel(AiServices<T> aiServices) {
validateChatModel();
if (model != null) {
aiServices.chatModel(model);
} else if (streamingChatModel != null) {
aiServices.streamingChatModel(streamingChatModel);
} else if (chatModelProvider != null) {
aiServices.chatModel(PLACEHOLDER_CHAT_MODEL);
} else if (streamingChatModelProvider != null) {
aiServices.streamingChatModel(PLACEHOLDER_STREAMING_CHAT_MODEL);
} else {
...
}
}
So if both model and chatModelProvider are set, model takes precedence. This means if we call agentBuilder.chatModel(chatModel) after the constructor, it would override the @ChatModelSupplier.
But looking at how createAgenticSystem is typically called:
createAgenticSystem(agentServiceClass) - uses declarativeChatModel(agentServiceClass)
createAgenticSystem(agentServiceClass, chatModel) - uses the provided chatModel
createAgenticSystem(agentServiceClass, agentConfigurator) - uses declarativeChatModel(agentServiceClass)
createAgenticSystem(agentServiceClass, chatModel, agentConfigurator) - uses the provided chatModel
In all cases, if the user provides a chatModel, it should take precedence over @ChatModelSupplier. And if no chatModel is provided, the @ChatModelSupplier should be used.
So the fix needs to check if the agent already has a @ChatModelSupplier before setting the chatModel.
Actually, let me simplify. The issue is that configureAgent is called twice. If we just don't call it the second time, we avoid the duplicate registration. The chatModel from the first configureAgent call (in the constructor) would already be set via @ChatModelSupplier if present.
If no @ChatModelSupplier is present and no chatModel is provided to createAgenticSystem, the AgentBuilder would have no chatModel set. But that's OK because the user didn't provide one.
If no @ChatModelSupplier is present and a chatModel IS provided to createAgenticSystem, we need to set it. But we can do this after calling agentBuilder(agentServiceClass) by calling agentBuilder.chatModel(chatModel).
So the fix is:
if (agent == null) {
var agentBuilder = agentBuilder(agentServiceClass);
// Note: AgentBuilder constructor already calls configureAgent(agentServiceClass, this)
// which handles @ToolProviderSupplier, @ToolsSupplier, etc.
// Calling configureAgent again would cause duplicate tool provider registration.
// If a chatModel was explicitly provided and no @ChatModelSupplier was found, set it
if (chatModel != null) {
// Check if the agent has a @ChatModelSupplier - if so, don't override it
if (!hasChatModelSupplier(agentServiceClass)) {
agentBuilder.chatModel(chatModel);
}
}
// Apply the agentConfigurator
if (agentConfigurator.configurator() != null) {
agentConfigurator.configurator().accept(new DefaultDeclarativeAgentCreationContext(agentServiceClass, agentBuilder));
}
agent = agentBuilder.build();
}
But hasChatModelSupplier would need to check if the class has a @ChatModelSupplier method. This is essentially duplicating the logic in DeclarativeUtil.
Actually, a simpler approach: just always call agentBuilder.chatModel(chatModel) if chatModel is not null. This would override any @ChatModelSupplier, but that's the expected behavior based on the method signatures:
createAgenticSystem(agentServiceClass, chatModel) - explicit chatModel should take precedence
Wait, but if we call agentBuilder.chatModel(chatModel) after the constructor, and the constructor already set a chatModelProvider from @ChatModelSupplier, the chatModel would take precedence. That's actually fine and probably the expected behavior.
But wait, there's another consideration. The agentConfigurator is a callback that the user can use to tweak the agentBuilder. In the original code, it's called inside configureAgent. If we remove the configureAgent call, we need to call the agentConfigurator directly.
Hmm, let me re-think this. The original code does:
- AgentBuilder constructor calls
configureAgent(agentServiceClass, this) which:
- Processes @ToolsSupplier, @ToolProviderSupplier, @ChatModelSupplier, etc.
- Applies an empty AgentConfigurator
- createAgenticSystem calls
configureAgent(agentServiceClass, chatModel, agentBuilder, agentConfigurator) which:
- Processes @ToolsSupplier, @ToolProviderSupplier, @ChatModelSupplier, etc. AGAIN
- Applies the actual AgentConfigurator
So the bug is in step 2 - it processes all the annotations again.
The fix should be to:
- Not call
configureAgent again in step 2
- Only set the chatModel if it was explicitly provided
- Call the agentConfigurator callback directly
Let me write the fix:
if (agent == null) {
var agentBuilder = agentBuilder(agentServiceClass);
// AgentBuilder constructor already called configureAgent(agentServiceClass, this)
// which registered all declarative annotations including ToolProviders.
// Avoid calling configureAgent again to prevent duplicate tool registrations.
if (chatModel != null) {
agentBuilder.chatModel(chatModel);
}
if (agentConfigurator != null && agentConfigurator.configurator() != null) {
agentConfigurator.configurator().accept(new DefaultDeclarativeAgentCreationContext(agentServiceClass, agentBuilder));
}
agent = agentBuilder.build();
}
Wait, I just realized something. Let me check what hasChatModelSupplier logic looks like in DeclarativeUtil:
getAnnotatedMethodOnClass(agentType, ChatModelSupplier.class)
.ifOrElse(...)
So it looks for @ChatModelSupplier annotation.
Actually, I think the simplest and safest fix is to just not call configureAgent at all in the createAgenticSystem path. The AgentBuilder constructor already handles everything. If chatModel is provided, we can set it on the agentBuilder after the constructor. The agentConfigurator can be applied separately.
But we need to be careful about the agentConfigurator. In the original code, configureAgent calls:
agentConfigurator.configurator().accept(new DefaultDeclarativeAgentCreationContext(agentType, agentBuilder));
This is at the end of configureAgent. So if we skip configureAgent, we need to call this explicitly.
Actually, looking at the original code more carefully:
if (agent == null) {
var agentBuilder = agentBuilder(agentServiceClass);
configureAgent(agentServiceClass, chatModel, agentBuilder, agentConfigurator);
agent = agentBuilder.build();
}
And in createAgenticSystem:
public static <T> T createAgenticSystem(
Class<T> agentServiceClass,
ChatModel chatModel,
AgentConfigurator agentConfigurator) {
So createAgenticSystem is called with a chatModel. This chatModel might be from:
- An explicit parameter passed to the public API
- The result of
declarativeChatModel(agentServiceClass) which looks for @ChatModelSupplier
In case 1, the explicit chatModel should override @ChatModelSupplier.
In case 2, the chatModel from @ChatModelSupplier should be used (and it's already set by the AgentBuilder constructor).
So for case 1, we need to check if the agent has a @ChatModelSupplier and only set the chatModel if it doesn't.
But actually, looking at the overloaded methods:
public static <T> T createAgenticSystem(Class<T> agentServiceClass) {
return createAgenticSystem(agentServiceClass, declarativeChatModel(agentServiceClass));
}
public static <T> T createAgenticSystem(Class<T> agentServiceClass, ChatModel chatModel) {
return createAgenticSystem(agentServiceClass, chatModel, AgentConfigurator.empty());
}
So createAgenticSystem(agentServiceClass) and createAgenticSystem(agentServiceClass, chatModel) both end up calling the main implementation with the chatModel.
If the user explicitly passes a chatModel, it should take precedence over @ChatModelSupplier.
If the chatModel comes from declarativeChatModel(agentServiceClass), it's the same as what the AgentBuilder constructor would find.
So the fix could be:
- Check if chatModel was explicitly provided by the user (not from declarativeChatModel)
- If so, set it on the agentBuilder
- If not, don't set it (the AgentBuilder constructor already set it from @ChatModelSupplier)
But this requires tracking whether the chatModel was explicitly provided.
A simpler approach: just always set the chatModel if it's not null. If @ChatModelSupplier was found, it would already have set a chatModelProvider on the AgentBuilder. Then setting agentBuilder.chatModel(chatModel) would set the model field, which takes precedence in configureChatModel.
So if the user explicitly provides a chatModel, it would correctly override @ChatModelSupplier. And if the chatModel comes from declarativeChatModel (same as @ChatModelSupplier), it would still work because the model would be the same.
OK I think I've analyzed this enough. Let me write the fix.
Actually wait, I should also check if there's a similar issue in the createSubagent method:
private static AgentExecutor createSubagent(
Class<?> subgentClass, ChatModel chatModel, AgentConfigurator agentConfigurator) {
...
AgentBuilder<?, ?> agentBuilder = agentBuilder(subgentClass);
configureAgent(subgentClass, chatModel, agentBuilder, agentConfigurator);
return agentToExecutor(agentBuilder.build());
}
Yes, this has the same issue! agentBuilder(subgentClass) creates an AgentBuilder which calls configureAgent in its constructor, and then configureAgent is called again here.
So the fix should also apply to this case.
OK, let me now write the fix. I'll modify both createAgenticSystem and createSubagent to not call configureAgent after creating an AgentBuilder.
Let me draft the changes:
- In
createAgenticSystem:
if (agent == null) {
var agentBuilder = agentBuilder(agentServiceClass);
// AgentBuilder constructor already calls configureAgent(agentServiceClass, this)
// which handles @ToolsSupplier, @ToolProviderSupplier, @ChatModelSupplier, etc.
// Calling configureAgent again would cause duplicate tool registrations.
if (chatModel != null) {
agentBuilder.chatModel(chatModel);
}
if (agentConfigurator.configurator() != null) {
agentConfigurator.configurator().accept(
new DefaultDeclarativeAgentCreationContext(agentServiceClass, agentBuilder));
}
agent = agentBuilder.build();
}
- In
createSubagent:
AgentBuilder<?, ?> agentBuilder = agentBuilder(subgentClass);
// AgentBuilder constructor already called configureAgent, so we skip the duplicate call
// Only set chatModel if it wasn't already configured
if (chatModel != null) {
agentBuilder.chatModel(chatModel);
}
agentToExecutor(agentBuilder.build());
Wait, but in createSubagent, there's also the agentConfigurator to consider. Let me look at it again:
private static AgentExecutor createSubagent(
Class<?> subgentClass, ChatModel chatModel, AgentConfigurator agentConfigurator) {
if (agentConfigurator.subAgentResolver() != null) {
Object subagent = agentConfigurator.subAgentResolver().apply(subgentClass);
if (subagent != null) {
return agentToExecutor(subagent);
}
}
AgentExecutor agentExecutor = createBuiltInAgentExecutor(subgentClass, chatModel, agentConfigurator);
if (agentExecutor != null) {
return agentExecutor;
}
AgentBuilder<?, ?> agentBuilder = agentBuilder(subgentClass);
configureAgent(subgentClass, chatModel, agentBuilder, agentConfigurator);
return agentToExecutor(agentBuilder.build());
}
In this case:
- First it tries subAgentResolver
- Then it tries createBuiltInAgentExecutor (which handles composed agents like SequenceAgent, etc.)
- If both return null, it creates a simple agent via AgentBuilder
For the AgentBuilder case, we have the same duplicate configuration issue.
But wait, the agentConfigurator has two parts:
agentConfigurator.configurator() - callback to tweak the agentBuilder
agentConfigurator.subAgentResolver() - callback to resolve subagents
In the createSubagent method, the subAgentResolver is handled before creating the AgentBuilder. And the configurator is handled in configureAgent.
If we skip configureAgent, we need to:
- Set the chatModel on the agentBuilder if needed
- Call the configurator callback directly
Let me also check if createBuiltInAgentExecutor has the same issue. Looking at it again:
private static AgentExecutor createBuiltInAgentExecutor(
Class<?> agentServiceClass,
ChatModel chatModel,
AgentConfigurator agentConfigurator) {
Optional<Method> sequenceMethod = getAnnotatedMethodOnClass(agentServiceClass, SequenceAgent.class);
if (sequenceMethod.isPresent()) {
Method method = sequenceMethod.get();
InternalAgent agent =
(InternalAgent) buildSequentialAgent(agentServiceClass, method, chatModel, agentConfigurator);
return new AgentExecutor(AgentInvoker.fromMethod(agent, method), agent);
}
...
return null;
}
This doesn't create an AgentBuilder, so no duplicate configuration.
But the buildSequentialAgent method calls:
var builder = sequenceBuilder(agentServiceClass)
.subAgents(createSubagents(annotation.subAgents(), chatModel, agentConfigurator));
And createSubagents calls createSubagent for each subagent. So subagents of composed agents also have the same issue.
OK, so the fix needs to be applied in createSubagent as well.
Let me now write the actual code changes.
Actually, I realize I should also check if there are any issues in the agentBuilder() (without class parameter) and configureAgent path. Let me look at the configureAgent method that takes an AgentBuilder:
public static void configureAgent(Class<?> agentType, AgentBuilder<?, ?> agentBuilder) {
configureAgent(agentType, null, true, agentBuilder, AgenticServices.AgentConfigurator.empty());
}
And the one with chatModel:
public static void configureAgent(Class<?> agentType, ChatModel chatModel, AgentBuilder<?, ?> agentBuilder, AgenticServices.AgentConfigurator agentConfigurator) {
configureAgent(agentType, chatModel, false, agentBuilder, agentConfigurator);
}
These are both used by:
- AgentBuilder constructor:
configureAgent(agentServiceClass, this) - no chatModel, empty configurator
- createAgenticSystem:
configureAgent(agentServiceClass, chatModel, agentBuilder, agentConfigurator) - with chatModel and configurator
- createSubagent:
configureAgent(subgentClass, chatModel, agentBuilder, agentConfigurator) - with chatModel and configurator
OK, I think I have a clear picture now. Let me implement the fix.
Here's my plan:
- In
createAgenticSystem, after calling agentBuilder(agentServiceClass), don't call configureAgent again. Instead:
- Set chatModel on agentBuilder if provided
- Call agentConfigurator directly
- In
createSubagent, after calling agentBuilder(subgentClass), don't call configureAgent again. Instead:
- Set chatModel on agentBuilder if provided
- Call agentConfigurator directly
Let me also consider the DefaultDeclarativeAgentCreationContext class. I need to make sure I'm creating it correctly.
Let me search for it:
<tool_call>
<function=bash_terminal>
<parameter=command>
grep -n "DefaultDeclarativeAgentCreationContext" /langchain4j/langchain4j-agentic/src/main/java/dev/langchain4j/agentic/AgenticServices.java | head -5
</tool_call>