Skip to content

Model Gateway (openhusky-ai) ​

openhusky-ai is a pure Java 21 unified LLM protocol and lightweight gateway library—built directly on java.net.http.HttpClient and a linear streaming SseReader with zero Spring, Reactor, or third-party HTTP SDKs.

xml
<dependency>
    <groupId>dev.openhusky</groupId>
    <artifactId>openhusky-ai</artifactId>
    <version>0.2.0</version>
</dependency>
kotlin
implementation("dev.openhusky:openhusky-ai:0.2.0")

(Included transitively when you depend on openhusky-core.)

4 Native Wire Protocols ​

Construct providers in one line with the Providers factory:

Protocol DriverFactory MethodsKey Capabilities
OpenAiChatCompletionsProviderProviders.openai(key), deepseek(key), qwen(key), zhipu(key), mistral(key), openRouter(key), ollama(), litellm(url, key)Driven by OpenAiCompat flags for vendor-specific quirks, streaming tool calls, and usage accounting.
OpenAiResponsesProviderProviders.openaiResponses(key)OpenAI /v1/responses protocol with reasoning.effort, prompt_cache_key, and encrypted_content.
AnthropicMessagesProviderProviders.anthropic(key)/v1/messages with extended thinking, cryptographic signature round-tripping, automatic cache_control: ephemeral breakpoints, and multimodal tool results.
GoogleGeminiProviderProviders.gemini(key):streamGenerateContent?alt=sse and :generateContent via x-goog-api-key, supporting Gemini 2.5 thinkingBudget, Gemini 3/4 thinkingLevel, and thoughtSignature preservation.

Resilient Gateway Middleware (LlmClient) ​

Wrap any LlmProvider in LlmClient and stack LlmInterceptors for retries, cost tracking, and failover:

java
ModelCatalog catalog = ModelCatalog.defaults();
ModelSpec primary = catalog.require("deepseek-chat");
ModelSpec backup = catalog.require("claude-sonnet-4-5-20250929");

LlmProvider router = FallbackProvider.of(
        new FallbackProvider.Route(primary, Providers.deepseek(System.getenv("DEEPSEEK_API_KEY"))),
        new FallbackProvider.Route(backup, Providers.anthropic(System.getenv("ANTHROPIC_API_KEY")))
);

// Or point directly to a LiteLLM Proxy:
// LlmProvider litellm = Providers.litellm("https://litellm.example.com", System.getenv("LITELLM_API_KEY"));

CostTrackerInterceptor costTracker = new CostTrackerInterceptor(catalog, null);
LlmClient client = LlmClient.of(router)
        .withInterceptor(costTracker)
        .withInterceptor(RetryInterceptor.defaults());

ChatRequest req = ChatRequest.builder(primary)
        .systemPrompt("You are a concise Java architect.")
        .messages(List.of(Message.UserMessage.of("Explain Java 21 virtual threads in 2 sentences.")))
        .reasoningEffort(ReasoningEffort.HIGH)
        .build();

ChatResponse resp = client.stream(req, AbortSignal.none(), event -> {
    if (event instanceof StreamEvent.TextDelta td) {
        System.out.print(td.delta());
    }
});

Built-In Gateway Capabilities ​

  • RetryInterceptor: Exponential backoff with jitter, automatic Retry-After header honoring, and instant AbortSignal cancellation.
  • FallbackProvider: Ordered route failover with cooldown windows and a partial-output guard (never silently switches mid-stream after tokens have already been emitted).
  • CostTrackerInterceptor + ModelCatalog: Exact USD cost calculation including cacheRead and cacheWrite tokens.
  • Cross-Provider MessageTransformer: Retains cryptographic thinking signatures when staying on the same model/provider, strips incompatible signatures when switching providers mid-session, and deterministically normalizes toolCallIds.
  • ContextOverflowDetector & HttpErrorClassifier: Recognizes 20+ provider overflow and transient error patterns (plus NON_OVERFLOW_PATTERNS and silent overflow checks) so openhusky-core can trigger emergency compaction and retry automatically.
  • EnvProxySelector: Respects HTTPS_PROXY / HTTP_PROXY / NO_PROXY while bypassing loopback addresses cleanly.

Custom & Local Models ​

Use ModelSpec.custom(...) for private deployments, fine-tuned models, or local vLLM / Ollama servers:

java
ModelSpec localLlama = ModelSpec.custom("llama3.3:70b", 131_072, 8_192);
LlmClient client = LlmClient.of(Providers.ollama("http://localhost:11434"));

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