- Java 21 or later installed
- Maven 3.9 or later
- Autohand CLI binary available on your system or in the
cli/directory
Official Agent SDK docs: https://autohand.ai/docs/agent-sdk/
Add the dependency to your pom.xml:
<dependency>
<groupId>ai.autohand</groupId>
<artifactId>code-agent-sdk-java</artifactId>
<version>1.0.0</version>
</dependency>cd java/
mvn clean installSDKConfig config = SDKConfig.builder()
.cwd(".")
.cliPath(System.getenv("AUTOHAND_CLI_PATH"))
.model("openrouter/auto")
.appendSystemPrompt("Prefer concise Java examples.")
.build();The SDK uses the CLI's configuration file (~/.autohand/config.json). You can configure providers there:
{
"provider": "openrouter",
"openrouter": {
"apiKey": "sk-or-...",
"model": "openrouter/auto"
}
}import ai.autohand.sdk.sdk.Agent;
import ai.autohand.sdk.sdk.AgentOptions;
import ai.autohand.sdk.types.*;
public class HelloAgent {
public static void main(String[] args) throws Exception {
Agent agent = Agent.create(AgentOptions.builder()
.cwd(".")
.model("openrouter/auto")
.instructions("Be concise and helpful.")
.build());
try {
var run = agent.send("Explain what this SDK does");
run.stream(event -> {
if (event instanceof Events.MessageUpdateEvent mue) {
System.out.print(mue.delta());
}
});
System.out.println("\nDone!");
} finally {
agent.close();
}
}
}The SDK emits the following events via sealed interface Event:
AgentStartEvent- Agent started a sessionAgentEndEvent- Agent ended a sessionTurnStartEvent- Turn startedTurnEndEvent- Turn endedMessageStartEvent- Message generation startedMessageUpdateEvent- Message content update (streaming)MessageEndEvent- Message generation endedToolStartEvent- Tool execution startedToolUpdateEvent- Tool output update (streaming)ToolEndEvent- Tool execution endedFileModifiedEvent- File was modifiedPermissionRequestEvent- Permission request from agentErrorEvent- Error occurred
Use pattern matching to handle events:
run.stream(event -> {
switch (event) {
case Events.MessageUpdateEvent mue -> System.out.print(mue.delta());
case Events.ToolStartEvent tse -> System.out.println("Running: " + tse.toolName());
case Events.PermissionRequestEvent pre -> handlePermission(pre);
default -> {}
}
});sdk.streamPrompt(params, event -> {
if (event instanceof Events.PermissionRequestEvent pre) {
System.out.println("Allow " + pre.tool() + "?");
sdk.allowPermission(pre.requestId(), DecisionScope.SESSION);
}
});Ergonomic helpers:
sdk.allowPermission(requestId, DecisionScope.ONCE);
sdk.denyPermission(requestId, DecisionScope.SESSION);
sdk.suggestPermissionAlternative(requestId, "Run mvn test first");- See
examples/for more examples - Read
docs/advanced-patterns.mdfor streaming, plan mode, and hooks - Read
docs/publishing.mdfor Maven Central release setup - Run
scripts/validate-examples.shbefore changing example code