Skip to content

Getting Started ​

Build and run your first durable Java 21 AI agent in under 5 minutes.

Prerequisites ​

  • JDK 21+ (uses Virtual Threads, sealed interfaces, and pattern matching)
  • Maven 3.9+ or Gradle 8+
  • An API key for OpenAI, Anthropic, Google Gemini, DeepSeek, or a local Ollama/LiteLLM endpoint (or test offline with a scripted LlmProvider)

1. Add Dependencies ​

openhusky-core transitively brings in openhusky-ai and openhusky-session. Add optional modules such as openhusky-session-sqlite only when you need them.

xml
<dependencies>
    <!-- Core agent runtime (includes openhusky-ai + openhusky-session) -->
    <dependency>
        <groupId>dev.openhusky</groupId>
        <artifactId>openhusky-core</artifactId>
        <version>0.1.0</version>
    </dependency>

    <!-- Optional: SQLite WAL + FTS5 persistence -->
    <dependency>
        <groupId>dev.openhusky</groupId>
        <artifactId>openhusky-session-sqlite</artifactId>
        <version>0.1.0</version>
    </dependency>
</dependencies>
kotlin
dependencies {
    // Core agent runtime (includes openhusky-ai + openhusky-session)
    implementation("dev.openhusky:openhusky-core:0.1.0")

    // Optional: SQLite WAL + FTS5 persistence
    implementation("dev.openhusky:openhusky-session-sqlite:0.1.0")
}

2. Define Declarative @Tool Methods ​

OpenHusky scans plain Java objects via AnnotatedToolScanner—no Spring beans or interface boilerplate required. Use ToolExecutionMode.PARALLEL_SAFE for read-only lookups so the runtime executes concurrent tool calls on Virtual Threads.

java
import dev.openhusky.core.tool.Tool;
import dev.openhusky.core.tool.ToolContext;
import dev.openhusky.core.tool.ToolExecutionMode;
import dev.openhusky.core.tool.ToolParam;
import dev.openhusky.core.tool.ToolResult;

public class OrderTools {

    @Tool(
            name = "lookup_order",
            description = "Look up order shipping status and ETA by order ID.",
            mode = ToolExecutionMode.PARALLEL_SAFE,
            readOnly = true
    )
    public ToolResult lookupOrder(
            ToolContext ctx,
            @ToolParam(name = "orderId", description = "Order identifier, e.g. #1042") String orderId
    ) {
        ctx.emitProgress("Querying fulfillment ledger for " + orderId + "...");
        return ToolResult.ok("{\"orderId\":\"" + orderId + "\",\"status\":\"IN_TRANSIT\",\"eta\":\"Tomorrow 14:00\"}");
    }

    @Tool(
            name = "refund_order",
            description = "Issue a full refund for an order (requires human approval).",
            mode = ToolExecutionMode.SEQUENTIAL,
            readOnly = false
    )
    public ToolResult refundOrder(
            @ToolParam(name = "orderId") String orderId,
            @ToolParam(name = "reason") String reason
    ) {
        return ToolResult.ok("{\"orderId\":\"" + orderId + "\",\"refunded\":true}");
    }
}

3. Wire AgentRuntime & Run a Session ​

Configure your LLM client, SQLite store, fail-closed PermissionPolicy, and interactive ApprovalDecision.Handler:

java
import dev.openhusky.ai.catalog.ModelCatalog;
import dev.openhusky.ai.gateway.RetryInterceptor;
import dev.openhusky.ai.provider.LlmClient;
import dev.openhusky.ai.transport.Providers;
import dev.openhusky.core.event.AgentEvent;
import dev.openhusky.core.interceptor.DoomLoopGuardInterceptor;
import dev.openhusky.core.interceptor.OutputOffloadToolInterceptor;
import dev.openhusky.core.policy.ApprovalDecision;
import dev.openhusky.core.policy.PermissionPolicy;
import dev.openhusky.core.runtime.AgentRuntime;
import dev.openhusky.core.runtime.AgentSession;
import dev.openhusky.core.runtime.RunResult;
import dev.openhusky.core.tool.Principal;
import dev.openhusky.session.sqlite.SqliteSessionStore;

import java.nio.file.Path;

public class QuickstartApp {
    public static void main(String[] args) {
        try (SqliteSessionStore store = SqliteSessionStore.open(Path.of("sessions.db"));
             AgentRuntime runtime = AgentRuntime.builder()
                     .llmClient(LlmClient.of(Providers.openai(System.getenv("OPENAI_API_KEY")))
                             .withInterceptor(RetryInterceptor.defaults()))
                     .model(ModelCatalog.defaults().require("gpt-4.1"))
                     .systemPromptSupplier(ctx ->
                             "You are a concise support assistant for user " + ctx.principal().id())
                     .sessionStore(store)
                     .toolsFrom(new OrderTools())
                     .permissionPolicy(PermissionPolicy.failClosed()
                             .allow("lookup_order")
                             .requireApproval("refund_order")
                             .build())
                     .approvalHandler((req, signal) -> {
                         System.out.println("[HITL] Approving " + req.toolName() + " args=" + req.arguments());
                         return ApprovalDecision.approveOnce();
                     })
                     .stepInterceptor(DoomLoopGuardInterceptor.defaults())
                     .toolInterceptor(OutputOffloadToolInterceptor.inMemory(32_768))
                     .build()) {

            AgentSession session = runtime.openSession("order-1042", Principal.of("buyer-42"));
            RunResult result = session.prompt("Check order #1042 and summarize its ETA.", event -> {
                switch (event) {
                    case AgentEvent.TextDelta td -> System.out.print(td.delta());
                    case AgentEvent.ToolCallProgress tp -> System.out.println("\n[PROGRESS] " + tp.message());
                    default -> {}
                }
            });

            System.out.printf("%nCompleted in %d step(s), tokens=%d%n",
                    result.stepsExecuted(), result.totalUsage().totalTokens());
        }
    }
}

4. Run the Repository Locally ​

Want to inspect the source or launch the interactive showcase without writing any setup code?

bash
git clone https://github.com/openhusky-ai/openhusky.git
cd openhusky

# Run the full offline test suite across all modules (JDK 21+)
./mvnw -B -ntp verify

# Launch the Unified Interactive Showcase (http://localhost:8080, zero API key required)
./mvnw -B -ntp -pl openhusky-examples -am compile exec:java

What to Explore Next ​

Released under the MIT License. Built with Java 21 Virtual Threads.