LangChain4j with the AIM Java SDK
The AIM Java SDK has four helper classes for LangChain4j applications, in the package org.opena2a.aim.integration.langchain4j. They import no LangChain4j type, and the SDK declares no LangChain4j dependency, so nothing connects them to LangChain4j for you: your application calls them by hand, around the tool calls and model calls it already makes.
The four helper classes
AIMToolExecutor
Registers each tool under a capability string, rates the tool's risk from that string and records each run in the SDK's security log. With blocking turned on, it refuses a tool rated above its approval threshold; blocking is off by default.
AIMChatModelListener
Records each model request and response, with token counts and latency, in the security log.
AIMAgentCallback
Records an agent run step by step: its start, thoughts, actions, observations, answer and errors.
AIMLangChain4jConfig
Creates the other three helpers from one AIMClient.
The helpers make no call to the AIM server: the risk rating runs in the SDK, and their events go to the SDK's security log, a JSON Lines file. To check an action against the agent's capability grant on the AIM server, run it through the client's performAction, as the Java SDK docs show.
Prerequisites
- • Java 17+ and Maven/Gradle
- • LangChain4j 0.27.0+ dependency
- • AIM SDK installed
Install
The AIM Java SDK is not published to Maven Central. Build it from source first: mvn install compiles the SDK, runs its tests and installs it in your local Maven repository.
git clone https://github.com/opena2a-org/agent-identity-management.git
cd agent-identity-management/sdk/java
mvn installThen add LangChain4j to your project:
<dependency>
<groupId>dev.langchain4j</groupId>
<artifactId>langchain4j</artifactId>
<version>0.27.0</version>
</dependency>Add the AIM SDK beside it, with the groupId, artifactId and version declared at the top of sdk/java/pom.xml. For Gradle, see the Java SDK installation.
Call the tool executor from a tool
LangChain4j calls the @Tool method; the method calls the executor. aimClient is the AIMClient your application already registers, as in the Java SDK docs.
import dev.langchain4j.agent.tool.Tool;
import org.opena2a.aim.client.AIMClient;
import org.opena2a.aim.integration.langchain4j.AIMToolExecutor;
public class CatalogTools {
private final AIMToolExecutor executor;
public CatalogTools(AIMClient aimClient, ProductService productService) {
executor = new AIMToolExecutor(aimClient);
// The capability string sets the risk rating and appears in each logged run
executor.register("searchProducts",
query -> productService.search((String) query), "catalog:search");
}
@Tool("Search the product catalog")
public Object searchProducts(String query) {
return executor.execute("searchProducts", query);
}
}To refuse tools rated above a threshold, turn blocking on. A refused run throws ToolBlockedException.
import org.opena2a.aim.security.RiskLevel;
executor.setApprovalThreshold(RiskLevel.HIGH);
executor.setBlockHighRisk(true);Record model calls
Call the listener before and after each model call. The token counts are the ones your model call reports.
import org.opena2a.aim.integration.langchain4j.AIMChatModelListener;
AIMChatModelListener listener = new AIMChatModelListener(aimClient);
String requestId = listener.onRequest(userMessage, "gpt-4o-mini");
String answer = assistant.chat(userMessage); // your LangChain4j AI service
listener.onResponse(requestId, answer, inputTokens, outputTokens);Record an agent run
import org.opena2a.aim.integration.langchain4j.AIMAgentCallback;
AIMAgentCallback callback = new AIMAgentCallback(aimClient);
String executionId = callback.onAgentStart("support-assistant", userMessage);
callback.onAgentAction(executionId, "searchProducts", query);
callback.onAgentObservation(executionId, String.valueOf(result));
callback.onAgentFinish(executionId, answer);Create the helpers from one client
import org.opena2a.aim.integration.langchain4j.AIMLangChain4jConfig;
AIMToolExecutor executor = AIMLangChain4jConfig.createToolExecutor(aimClient);
AIMChatModelListener listener = AIMLangChain4jConfig.createListener(aimClient);
AIMAgentCallback callback = AIMLangChain4jConfig.createAgentCallback(aimClient);