Package com.tnsai.llm

Class FallbackLLMClient

java.lang.Object
com.tnsai.llm.FallbackLLMClient
All Implemented Interfaces:
LLMClient

public class FallbackLLMClient extends Object implements LLMClient
LLM client with automatic fallback support.

Wraps a primary LLM client with fallback options. If the primary fails, automatically retries with fallback clients.

Inspired by Mastra

Based on Mastra's auto model fallback pattern.

Usage


 LLMClient client = FallbackLLMClient.builder()
     .primary(new OpenAIClient("gpt-4o"))
     .fallback(new AnthropicClient("claude-3-sonnet"))
     .fallback(new OllamaClient("llama3.2"))
     .retryOnPrimary(2)
     .build();

 // If gpt-4o fails after 2 retries, automatically tries claude, then llama
 ChatResponse response = client.chat("Hello");