关键要点
Researchers had the LLM code assistant use the LangChain4j documentation and API to autonomously design and implement a multi-agent coding system.
This self-built coding agent successfully fixed real bugs, passed tests, and exposed its own execution flow via LangChain4j.
Comparing the two agent modes revealed that the stricter workflow mode eliminated LLM-induced coordination overhead, making it 3 times faster than the supervisor mode.
Through the newly introduced MonitoredAgent interface, agent invocation reports and system topology can be clearly obtained.
In the experiment, the same agent design fell into tool call loops and failed on older, cheaper models, while newer models successfully completed the task.
实验概述
Researchers conducted a meta-experiment: they gave the LangChain4j documentation to the code assistant and asked it to build "its own version" based on the documentation. Specifically, they aimed to design a multi-agent system that could write, test, and debug code like a human engineer.
The LLM's ability to build its own version from the documentation demonstrates both that the LangChain4j API is clear and easy to use, and that the framework provides sufficient orchestration capabilities for the generated system to run real debugging tasks end-to-end.
This project also helped test LangChain4j's new monitoring tools—when having AI build another AI, it is essential to see what is happening inside. This article details the experiment process and results; the project code is available at this repository.
“氛围编码”一个智能体系统
To have the code assistant build the first agent coder, the researchers wrote the following prompt:
Study the documentation and source code of the LangChain4j agent framework, and based on it, design a clone of your own agent coder.
After a few minutes of thinking, the assistant decided to use LangChain4j's supervisor pattern and proposed an initial architecture:
public interface SupervisorCoderSystem {
@SupervisorAgent(description = """
一个多智能体编码助手,可以探索代码库、规划实现、编写/编辑代码、运行构建/测试。
它编排专门的子智能体来完成编码请求。
""", subAgents = {
ExplorerAgent.class,
PlannerAgent.class,
ImplementerAgent.class,
ExecutorAgent.class
})
@Override
String code(@K(UserRequest.class) String request,
@K(WorkingDirectory.class) String workingDirectory);
@SupervisorRequest
static String request(@K(UserRequest.class) String userRequest,
@K(WorkingDirectory.class) String workingDirectory) {
return "将'" + workingDirectory + "'作为工作目录,完成以下用户请求:" + userRequest;
}
}The assistant also designed and developed four sub-agents, along with their system and user messages: an exploration agent to explore existing code, a planning agent to create an action plan, an implementation agent to write code according to the plan, and an execution agent to compile and run the code. Each sub-agent was equipped with appropriate tools: a file system browser, a code editor, a code runner, etc.
The results of the first iteration were impressive. In reality, "professional" code assistants often follow a similar pattern: execute exploration, planning, implementation, and execution in four steps, and orchestrate in a supervisor manner—this is consistent with the implementation generated in the experiment.
The code assistant's ability to design and implement such a system indicates that it has some understanding of its own internal workings and can translate that understanding into a concrete agent system design.
让智能体编码器投入工作To test the designed agent coder, the researchers had it write some buggy code and then used the newly created system to fix it. The assistant generated a Calculator class containing four methods with subtle bugs:
public class Calculator {
public int sum(List<Integer> numbers) {
int total = 0;
for (int i = 0; i <= numbers.size(); i++) { // Bug: 应该是 i < numbers.size()
total += numbers.get(i);
}
return total;
}
public double average(List<Integer> numbers) {
if (numbers.isEmpty()) return 0;
return sum(numbers) / numbers.size(); // Bug: 整数除法,应转为double
}
public int max(List<Integer> numbers) {
if (numbers.isEmpty()) throw new IllegalArgumentException("列表不能为空");
int max = 0; // Bug: 如果所有数都是负数,会返回0
for (int n : numbers) {
if (n > max) max = n;
}
return max;
}
public long factorial(int n) {
if (n < 0) throw new IllegalArgumentException("n必须非负");
long result = 1;
for (int i = 1; i < n; i++) { // Bug: 应包含n,即 i <= n
result *= i;
}
return result;
}
}Then a test was generated, which cloned the folder containing Calculator to a temporary directory and executed the LangChain4j agent coder:
@Test
void workflow_should_fix_buggy_calculator() throws Exception {
var coder = CoderAgenticSystem.supervisorCoder(coderModel());
Path source = Path.of("src/test/resources/buggy-project");
Path workDir = Path.of("/tmp/buggy-calculator");
String result = coder.code(
"测试当前失败,因为Calculator.java存在Bug。请修复它。",
workDir.toAbsolutePath().toString());
assertThat(result).isNotBlank();
System.out.println("结果: " + result);
}When running the code, OpenAI's gpt-4o model was used because it is the default model for LangChain4j and supports tool calls. However, the results were not ideal, and an error occurred after a few minutes:
dev.langchain4j.agentic.agent.AgentInvocationException: Failed to invoke agent method: public abstract...Pattern Comparison: Supervisor vs. Workflow
Since the supervisor pattern requires the LLM to make decisions before and after each sub-agent call, it introduces significant coordination overhead. The researchers instead tried a more disciplined workflow pattern, which orchestrates agents in a fixed sequence and only calls the LLM when necessary.
The workflow pattern executed 3 times faster than the supervisor pattern because it eliminated most context switches between LLMs. Experimental data shows that the supervisor pattern required 7 LLM calls to complete the fix, while the workflow pattern needed only 2.
Impact of Model Choice
The experiment also tested a cheaper model (e.g., gpt-4o-mini), which fell into a tool-calling loop and could not complete the task. In contrast, using the newer gpt-5-mini model successfully fixed all bugs. This indicates that model quality is crucial for the reliability of agent systems.
Monitoring Agent Calls
The newly introduced MonitoredAgent interface in LangChain4j can generate agent call reports, showing the input, output, and duration of each call. The researchers used this interface to analyze the system's topology and workflow.
Summary
This experiment demonstrates an interesting metaprogramming capability: LLMs can use existing framework documentation to build self-healing agent systems. The workflow pattern is more efficient than the supervisor pattern, and model choice directly determines the system's success or failure. LangChain4j provides sufficient APIs and monitoring tools to support such experiments.