[BUG] AnthropicMapper.toAnthropicTools() applies cache_control to multiple tools when ToolSpecifications are equal
Describe the bug
AnthropicMapper.toAnthropicTools() identifies "the last tool" (the only one that should
receive a cache_control breakpoint) by calling toolSpecification.equals(lastToolSpecification)
instead of comparing by index:
ToolSpecification lastToolSpecification =
toolSpecifications.isEmpty() ? null : toolSpecifications.get(toolSpecifications.size() - 1);
return toolSpecifications.stream()
.map(toolSpecification -> {
boolean isLastItem = toolSpecification.equals(lastToolSpecification);
...ToolSpecification.equals() compares by value (name, description, parameters, metadata, strict),
not by reference/position. If two tools in the list are structurally identical, both get
isLastItem = true, so both receive a cache_control breakpoint instead of only the actual last one.
This silently spends more of Anthropic's 4-breakpoint-per-request budget than intended, and can cause a hard 400 error ("A maximum of 4 blocks with cache_control may be provided") if the budget is exceeded elsewhere in the same request (e.g. cached system messages + cached tools).
This is the same root cause as #6051 (fixed for system messages in #6070 / #6374), but
toAnthropicTools() was not touched by that fix and still has the identical bug.
Log and Stack trace Not applicable — this is a silent correctness bug, not an exception, unless the 4-breakpoint budget is exceeded elsewhere in the request, in which case Anthropic returns:
{
"type": "error",
"error": {
"type": "invalid_request_error",
"message": "A maximum of 4 blocks with cache_control may be provided"
}
}To Reproduce
List<ToolSpecification> tools = List.of(
ToolSpecification.builder()
.name("getWeather")
.description("Gets the current weather for a city")
.build(),
ToolSpecification.builder()
.name("getWeather")
.description("Gets the current weather for a city")
.build() // structurally identical to the one above
);
List<AnthropicTool> result = AnthropicMapper.toAnthropicTools(
tools, AnthropicCacheType.EPHEMERAL, Set.of(), false);
// Actual: both tools have cacheControl != null
// Expected: only tools.get(1) (the true last element) should have cacheControl != null
result.forEach(t -> System.out.println(t.name + " -> " + t.cacheControl));Expected behavior
Only the tool at the actual last index of the input list should receive cache_control,
regardless of whether any other tool in the list is structurally equal to it.
Please complete the following information:
- LangChain4j version: main (latest)
- LLM(s) used: Anthropic (Claude)
- Java version: 17
- Spring Boot version (if applicable): N/A
Additional context
Same pattern as #6051. Suggested fix (mirroring the fix merged for #6051): compare by index
instead of equals():
int lastIndex = toolSpecifications.size() - 1;
for (int i = 0; i < toolSpecifications.size(); i++) {
boolean isLastItem = i == lastIndex;
...
}I'd like to submit a fix for this — will open a PR shortly with a unit test covering the
duplicate-ToolSpecification case.
Source: langchain4j/langchain4j