[BUG] Duplicate activate_skill tool definition error when using AgenticServices.createAgenticSystem()Describe the bug
When creating an agentic system with AgenticServices.createAgenticSystem(), even with a single agent, the application throws an IllegalConfigurationException: Duplicated definition for tool: activate_skill during execution, caused by multiple registrations of non-dynamic ToolProvider instances.
Log and Stack trace
Caused by: dev.langchain4j.service.IllegalConfigurationException: Duplicated definition for tool: activate_skill
at dev.langchain4j.service.tool.ToolService.addTools(ToolService.java:398)
at dev.langchain4j.service.tool.ToolService.lambda$createContextFromStaticToolsAndProviders$0(ToolService.java:378)
at java.base/java.lang.Iterable.forEach(Iterable.java:75)
at dev.langchain4j.service.tool.ToolService.createContextFromStaticToolsAndProviders(ToolService.java:371)
at dev.langchain4j.service.tool.ToolService.createContext(ToolService.java:338)
at dev.langchain4j.service.DefaultAiServices$1.invoke(DefaultAiServices.java:260)
at dev.langchain4j.service.DefaultAiServices$1.invoke(DefaultAiServices.java:154)
at jdk.proxy2/jdk.proxy2.$Proxy5.test(Unknown Source)
at java.base/jdk.internal.reflect.DirectMethodHandleAccessor.invoke(DirectMethodHandleAccessor.java:104)
... 5 more
To Reproduce
public class Main {
public static void main(String[] args) {
var result=AgenticServices.createAgenticSystem(FooAgent.class).test();
// var result=AgenticServices.agentBuilder(FooAgent.class).build().test();
System.out.println(result);
}
public interface FooAgent {
@Agent
@UserMessage("Hello")
String test();
@ToolProviderSupplier
static ToolProvider testTool() {
return Skills.builder()
.skills(Skill.builder().name("test").description("test").content("test").build())
.build().toolProvider();
}
@ChatModelSupplier
static ChatModel chatModel() {
return OpenAiChatModel.builder()
.baseUrl("http://192.168.31.32:8000/v1")
.apiKey("none")
.modelName("qwen3.6")
.logRequests(true)
.logResponses(true)
.build();
}
}
}
Expected behavior
The agentic system should run successfully, properly deduplicating tools (including the built-in activate_skill tool) when registering non-dynamic ToolProvider instances.
Please complete the following information:
Assistant
Let me analyze the issue. The bug is about duplicate activate_skill tool definition when using AgenticServices.createAgenticSystem(). The error occurs because non-dynamic ToolProvider instances are being registered multiple times, and the activate_skill tool is automatically added for every SkillProvider instance.
Let me start by exploring the repository structure to understand the codebase.
Assistant
Let me explore more specifically the AgenticServices file mentioned in the issue.
Assistant
Now let me read the AgenticServices.java file to understand the createAgenticSystem method.
Assistant
I need to use the correct format for the open command. Let me try with just the file path.
Assistant
I see, the open command might have a different syntax. Let me try without the file parameter.
Assistant
Let me see the createAgenticSystem method and the buildAgent method more closely.
Assistant
I need to see more of the file, particularly the createAgenticSystem method and the buildAgent method to understand where the duplicate tool registration happens.
Assistant
Now I need to look at the configureAgent method in DeclarativeUtil to understand how agents are configured and where the duplicate registration might occur. Let me find the DeclarativeUtil class.
Assistant
Now I can see that configureAgent calls agentBuilder.toolProvider(invokeStatic(method)) for each @ToolProviderSupplier method. The issue is that the activate_skill tool is automatically added by the Skills class (which implements SkillProvider). Let me look at the AgentBuilder class to understand how tools are registered.
Assistant
Now let me look at the createSubagent and createSubagents methods in AgenticServices to see how tools are being registered multiple times:
Assistant
Now I understand the flow better. When createAgenticSystem is called, it creates an AgentBuilder via agentBuilder(agentServiceClass). In the AgentBuilder constructor, it calls configureAgent(agentServiceClass, this) which in turn registers the ToolProvider via agentBuilder.toolProvider(invokeStatic(method)). This is line 141 in AgentBuilder.java.
Then later in createAgenticSystem, when agent == null (for a simple agent case), it calls:
var agentBuilder = agentBuilder(agentServiceClass);
configureAgent(agentServiceClass, chatModel, agentBuilder, agentConfigurator);
This means configureAgent is called twice: once in the AgentBuilder constructor and once in createAgenticSystem. This causes the same ToolProvider to be registered twice!
Let me confirm this understanding by looking more closely at the code flow:
Assistant
Yes! I confirmed the issue. The AgentBuilder constructor calls configureAgent(agentServiceClass, this) at line 141. Then in createAgenticSystem at line 341:
var agentBuilder = agentBuilder(agentServiceClass);
configureAgent(agentServiceClass, chatModel, agentBuilder, agentConfigurator);
The configureAgent method is called twice:
First in the AgentBuilder constructor (via configureAgent(agentServiceClass, this))
Second explicitly in createAgenticSystem (via configureAgent(agentServiceClass, chatModel, agentBuilder, agentConfigurator))
Looking at the two versions of configureAgent:
Line 46-48: calls
Assistant
<parameter name="thought>
Assistant
Now let me look at the ToolService to understand how the duplicate detection works:
Assistant
Now I understand the issue completely:
When createAgenticSystem is called, it creates an AgentBuilder via agentBuilder(agentServiceClass).
In the AgentBuilder constructor, is called, which registers the via .
Assistant
Assistant
Thought
Assistant
Now I understand the design better. The AgentBuilder constructor calls configureAgent so that when you just do AgenticServices.agentBuilder(FooAgent.class).build(), it automatically picks up the annotations. But when using createAgenticSystem, it also calls configureAgent again.
The proper fix should be in createAgenticSystem to not call configureAgent again when creating a simple agent, since the AgentBuilder constructor already does it. However, the AgentBuilder constructor uses configureAgent(agentServiceClass, this) which doesn't pass a chat model or agent configurator.
Looking more carefully at the code flow:
AgentBuilder constructor calls configureAgent(agentServiceClass, this) - this registers tools, tool providers, etc. from annotations
Assistant
Assistant
Now I understand the fix. The createAgenticSystem method at lines 330-347 calls configureAgent again after creating the AgentBuilder. The AgentBuilder constructor already called configureAgent, so we need to remove the duplicate call but still apply the agent configurator callback.
Let me apply the fix:
Assistant
Now I'll make the fix. The solution is to remove the duplicate configureAgent call but still apply the agentConfigurator callback:
Assistant
Now let me verify the change was applied correctly:
Assistant
Now I need to verify that the import for DefaultDeclarativeAgentCreationContext is present. Let me check:
Assistant
Good, the DefaultDeclarativeAgentCreationContext is defined in the same class, so no additional import is needed. However, I notice that the record is defined as public record DefaultDeclarativeAgentCreationContext<T>(Class<T> agentServiceClass, AgentBuilder<T, ?> agentBuilder) which takes AgentBuilder<T, ?> but we're passing agentBuilder which is of type AgentBuilder<?, ?>. Let me check if this compiles:
Assistant
The agentBuilder variable is inferred from agentBuilder(agentServiceClass) which returns AgentBuilder<T, AgentBuilder<T, ?>>. Since agentServiceClass is Class<T>, the builder should be AgentBuilder<T, ?>. This should be compatible with the record's constructor.
Let me also check the existing tests to see if there are any that test this specific scenario:
Assistant
Let me check if there are any existing tests that use createAgenticSystem with ToolProviderSupplier:
Assistant
Now let me look at the experimental skills test to understand how Skills is used:
<tool_call>
<function=bash_terminal>
<parameter=command>
cat /langchain4j/experimental/langchain4j-experimental-skills-shell/src/test/java/dev/langchain4j/skills/shell/ShellSkillsIT.java | head -100
</tool_call>
Assistant
Let me run the command properly.
Assistant
Now let me compile the project to verify the fix compiles correctly:
Assistant
Good, the code compiles. Now let me run the existing tests to verify the fix doesn't break anything:
Assistant
Good, the DeclarativeAgentIT test passed. Let me run more tests related to agentic services to make sure I didn't break anything:
Assistant
The tests pass. Now let me write a test script to reproduce the original issue and verify the fix works:
Assistant
The tests passed! Now let me create a simple test to verify that the fix works for the specific scenario described in the issue. I should create a test that reproduces the bug (duplicate activate_skill tool) and confirm it's fixed.
Let me first look at how tests are structured in the agentic module to create a proper reproduction test.
Assistant
Let me look at how tests are structured and what mocking is available:
Assistant
Let me create a test to reproduce the issue. I need to check what test models are available and how to create a test that would have failed before the fix.
Assistant
Let me create a temporary test script to verify the fix works for the exact scenario described in the bug report:
Assistant
The Skills module is not a dependency of langchain4j-agentic. Let me create a simpler test that simulates the same duplicate ToolProvider issue without depending on the Skills module.
Assistant
Let me remove the test file with compilation errors and fix the other test file.
Assistant
Now let me fix the DuplicateToolProviderTest to use the correct API for AiServiceTool:
Assistant
Let me see how to properly construct an AiServiceTool and fix my test.
Assistant
Now I understand the API. Let me fix the test file:
Assistant
The test failed because of an access issue - the inner interface needs to be public. Let me fix this and also look at existing tests to see how they define agent interfaces.
The error is:
java.lang.IllegalAccessException: class dev.langchain4j.agentic.declarative.DeclarativeUtil cannot access a member of interface dev.langchain4j.agentic.DuplicateToolProviderTest$FooAgent with modifiers "public static"
This is a module access issue. Let me look at how other tests define agent interfaces.
Assistant
I see - the interfaces are defined as top-level or in a separate file. Let me move my test interface to a separate file in the Agents.java or create a proper public interface. But actually, the issue is that the inner interface of a test class cannot be accessed by other packages.
Let me look at how the test framework handles this - maybe I should put the interface in a separate public class.
Actually, looking more carefully, the issue is that Java module system prevents access to nested classes from test classes. Let me create a separate file for the agent interface.
Assistant
Let me continue with the fix verification. I need to update the test file to use the new agent interface:
Assistant
Now let me run the test to verify the fix works:
Assistant
The test passed. Now let me verify that reverting the fix would cause the test to fail, confirming that this test actually tests the bug fix:
Assistant
Let me verify that reverting the fix would cause the test to fail, confirming that this test actually tests the bug fix.
Assistant
The test passed even without the fix! This means my test doesn't actually reproduce the bug. The issue is that the duplicate registration error only occurs when the agent is actually invoked and the tools are processed. Let me update the test to actually invoke the agent method:
Assistant
Let me update the test to actually invoke the agent method to trigger the duplicate tool error:
Assistant
Now let me run the test with the fix to confirm it passes:
Assistant
Now let me verify that reverting the fix would cause the test to fail:
Assistant
The test correctly fails without the fix, showing the exact error from the bug report: Duplicated definition for tool: my_duplicate_tool. Now let me restore the fix and run the existing tests to make sure nothing is broken:
Assistant
Let me run the existing tests to make sure the fix doesn't break anything.
Assistant
All tests pass. Now let me clean up by removing the test files I created (since the instructions say not to add new tests):
Assistant
Now let me run the existing tests again to make sure everything still works:
Assistant
The tests pass. Let me verify the final state of the changes and then submit.
Assistant
Let me verify the fix is correct by reviewing the change one more time and checking the test results:
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
Additional context
The bug is caused by createAgenticSystem registering non-dynamic ToolProvider instances multiple times. The activate_skill tool is automatically added for every SkillProvider instance, leading to duplicate registration even with a single agent.
Interface
Type: Function
Name: createAgenticSystem
Path: langchain4j-agentic/src/main/java/dev/langchain4j/agentic/AgenticServices.java
Input: Class agentServiceClass, AgentConfigurator agentConfigurator
Output: T (the created agentic system instance of the requested type)
Description: Creates an agentic system from a declarative agent service class, applying the provided configurator to each agent. This overload resolves the default chat model from the agent class and deduplicates tool registrations across the system.
ADDITIONAL ADVICE
Since you are given a git repository, you can use git commands to simplify your work.
For example, if you made a mistake and want to revert your changes, you can use git checkout <file> to restore the file to its original state.
You can also reset all changes in the repository using git reset --hard command.
Additionally, you can use git stash and git stash pop to temporarily save your changes and restore them later.
Finally, you can see the changes that you've made (which will be submitted when you call submit) using git status or git diff commands.
However you don't need to use git add or git commit before submitting your changes. In fact,
submit only submits currently unstaged changes because it uses git diff to get the changes that need to be submitted.
CHECKLIST
Before submitting your solution, please go over the following checklist and make sure you've done everything:
If an error was reported in the issue description, I have successfully reproduced it.
If an error was reported in the issue description, I have confirmed that my fix resolves the error.
I have thought about edge cases and made sure my fix handles them as well.
I have run existing tests in the repository that might have been affected by the change I made and confirmed that they pass.
I want you to list every bullet from this checklist and write your reasoning for why you think you did it or didn't need to.
Repository has been uploaded and your shell is currently at the repository root. Time to solve the issue!
(Current directory: /langchain4j, current file: none) bash-$
The issue is that the first call (from constructor) uses AgentConfigurator.empty() and doesn't pass a chat model, while the second call does. But both calls add the same ToolProvider instances to the builder's list.
Now let me look at the toolProvider method:
public B toolProvider(ToolProvider toolProvider) {
this.toolProviders.add(toolProvider);
return (B) this;
}
It simply adds to the list without checking for duplicates. This causes the activate_skill tool (which comes from SkillProvider in the Skills class) to be registered twice.
Let me now check how the Skills class works:
configureAgent(agentServiceClass, this)
ToolProvider
agentBuilder.toolProvider(invokeStatic(method))
Then, back in createAgenticSystem, it calls configureAgent(agentServiceClass, chatModel, agentBuilder, agentConfigurator) again, which registers the sameToolProvider again.
When the agent is built and the tools are processed, the same non-dynamic ToolProvider is iterated twice, and the activate_skill tool is registered twice, causing the duplicate error.
The fix: The createAgenticSystem method in AgenticServices.java should not call configureAgent again since the AgentBuilder constructor already does it. But wait, the AgentBuilder constructor calls configureAgent(agentServiceClass, this) which uses AgentConfigurator.empty() and doesn't pass a chat model. So the second call is needed to set up the chat model properly.
Looking more closely at the two configureAgent calls:
The issue is that both calls add the same ToolProvider. The solution should be to deduplicate ToolProviders when adding them. Let me look at how other properties are handled - like chatModel:
Looking at AgentBuilder, I see that chatModel(ChatModel model) simply sets this.model = model; - so the second call would overwrite the first. But toolProvider(ToolProvider toolProvider) uses this.toolProviders.add(toolProvider) which adds to a list without deduplication.
The fix should be in the toolProvider method of AgentBuilder to check for duplicates before adding. Since the ToolProvider from Skills.builder()...toolProvider() is the same object instance (it's created by the static method), we can simply check if the list already contains the provider.
Let me verify this approach by checking if the same ToolProvider instance is created each time:
Now let me check what the invokeStatic(method) call does. Looking back at DeclarativeUtil.java:
Every time invokeStatic(method) is called, it invokes the static method testTool() which creates a new Skills instance and calls .toolProvider() on it. So each call creates a newToolProvider instance! That's why simple contains() check won't work.
Let me think about the solution more carefully. The issue is that configureAgent is called twice:
In AgentBuilder constructor with AgentConfigurator.empty()
In createAgenticSystem with the actual agentConfigurator
The cleanest fix would be to modify the AgentBuilder constructor to NOT call configureAgent and instead have the caller always call it explicitly. But this might break other usages.
Actually, let me re-read the code more carefully. Looking at the AgentBuilder constructor:
public AgentBuilder(Class<T> agentServiceClass) {
...
configureAgent(agentServiceClass, this);
...
}
The AgentBuilder constructor calls configureAgent(agentType, AgentBuilder<?, ?> agentBuilder) which is an overload that internally calls configureAgent(agentType, null, true, agentBuilder, AgentConfigurator.empty()).
The fix should be to make the toolProvider and toolProviders methods in AgentBuilder deduplicate based on the actual tools they provide, or to not call configureAgent twice.
Looking at the issue description again: "The bug is caused by createAgenticSystem registering non-dynamic ToolProvider instances multiple times."
The simplest fix would be to remove the duplicate call. Let me check what the configureAgent call in AgentBuilder constructor is for:
It registers tools, tool providers, content retrievers, etc. from the declarative annotations
But then createAgenticSystem does it again
Looking at it from a different angle - the AgentBuilder constructor is meant to auto-configure from the class annotations. But when using createAgenticSystem, the caller also wants to configure. So maybe the solution is:
Option 1: Don't call configureAgent in AgentBuilder constructor - let the caller do it
Option 2: Make toolProvider deduplicate when adding
Option 3: Only call configureAgent once in the flow
Let me look at how agentBuilder(agentServiceClass).build() is supposed to work (without createAgenticSystem):
Then createAgenticSystem calls configureAgent(agentServiceClass, chatModel, agentBuilder, agentConfigurator) - this does it again AND sets up the chat model
The issue is that the second call to configureAgent duplicates the tool provider registration. But we need the second call to set up the chat model properly.
The fix should be to avoid duplicating the tool provider registration. The simplest approach is to check if a tool provider is already registered before adding it again. Since each call to the @ToolProviderSupplier method creates a new instance, we can't just use contains().
Looking at the problem from another angle: The createAgenticSystem method should not call configureAgent again for the simple agent case. Instead, it should only pass the chat model if needed.
Let me look at how the chat model is handled in configureAgent:
I see the pattern now. The configureAgent method handles the ChatModel like this:
If there's a @ChatModelSupplier method, it uses that (regardless of the chatModel parameter)
Otherwise, if there's a @StreamingChatModelSupplier method, it uses that
Otherwise, it falls back to the chatModel parameter (or throws if it's null and !allowNullChatModel)
Since chatModel(ChatModel model) in AgentBuilder just sets this.model = model, calling it multiple times is fine - the last call wins. But for toolProvider, it adds to a list, causing duplicates.
The best fix is to make the toolProvider method in AgentBuilder check if the same tool provider has already been added. Since each invocation of the @ToolProviderSupplier method creates a new instance, we need a different approach.
Looking at the code flow again, I think the cleanest fix is to modify createAgenticSystem to NOT call configureAgent for the simple agent case (when createComposedAgent returns null). The AgentBuilder constructor already calls configureAgent, and the chat model from @ChatModelSupplier annotation will be picked up automatically.
But wait - looking at the issue description:
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();
}
...
}
The createAgenticSystem method passes a chatModel that comes from declarativeChatModel(agentServiceClass) which extracts it from the @ChatModelSupplier annotation. So the chat model IS available from the annotation.
The issue is that AgentBuilder constructor calls configureAgent(agentServiceClass, this) with allowNullChatModel=true and chatModel=null. This means if there's a @ChatModelSupplier annotation, it will be picked up. Then createAgenticSystem calls configureAgent again with the same chat model.
The fix should be to not call configureAgent again in createAgenticSystem. Let me verify that the AgentBuilder constructor properly picks up the chat model from the annotation:
Looking at the configureAgent method in DeclarativeUtil:
Yes, if there's a @ChatModelSupplier method, it will be invoked and the chat model will be set. The fallback to the chatModel parameter only happens if there's no @ChatModelSupplier or @StreamingChatModelSupplier annotation.
So the fix is simple: Remove the duplicate configureAgent call in createAgenticSystem. But we still need to apply the agentConfigurator callback. Let me check what that does:
This is the last line of configureAgent. So if we remove the second configureAgent call, we still need to call the configurator.
Actually, looking at the AgentBuilder constructor again:
configureAgent(agentServiceClass, this);
This calls configureAgent(agentType, null, true, agentBuilder, AgentConfigurator.empty()) which uses an empty configurator. So the agentConfigurator callback from createAgenticSystem is never called for simple agents!
So the proper fix is:
In createAgenticSystem, don't call configureAgent again (to avoid duplicate tool provider registration)
But still call agentConfigurator.configurator().accept(...) to apply any custom configuration