Package com.tnsai.llm.routing
Class TaskBasedRouter
java.lang.Object
com.tnsai.llm.routing.TaskBasedRouter
- All Implemented Interfaces:
com.tnsai.llm.LLMClient,LLMRouter
Routes LLM requests to the most appropriate model based on task type.
TaskBasedRouter automatically classifies incoming prompts and routes them to specialized models. This enables cost optimization and quality improvement by using the right model for each task.
Key Benefits
- Cost Savings: Simple tasks go to cheap models (up to 77% savings)
- Quality: Complex tasks use specialized models (coding → Claude, math → o1)
- Automatic: No manual model selection needed
- Transparent: Full statistics on task routing
Usage Example
// Create router with task-specific models
TaskBasedRouter router = TaskBasedRouter.builder()
.forTask(TaskType.CODING, new AnthropicClient("claude-sonnet-4"))
.forTask(TaskType.CREATIVE, new OpenAIClient("gpt-4o"))
.forTask(TaskType.MATH, new OpenAIClient("o1-preview"))
.forTask(TaskType.FAST, new GroqClient("llama-3.3-70b"))
.forTask(TaskType.VISION, new OpenAIClient("gpt-4o"))
.defaultClient(new OpenAIClient("gpt-4o-mini"))
.build();
// Auto-routing based on prompt content
router.chat("Write a Python function to sort a list"); // → Claude (CODING)
router.chat("Solve: ∫x²dx"); // → o1 (MATH)
router.chat("Write a poem about autumn"); // → GPT-4o (CREATIVE)
router.chat("What is 2+2?"); // → GPT-4o-mini (FAST/default)
// Check routing statistics
TaskRoutingStats stats = router.getTaskStats();
System.out.println("Requests per task: " + stats.requestsPerTask());
System.out.println("Estimated savings: $" + stats.estimatedSavings());
Manual Override
// Force a specific task type
router.chatAs(TaskType.CODING, "Explain this concept");
// Get the client for a specific task
LLMClient codingClient = router.getClientForTask(TaskType.CODING);
With Fallback
// Combine with FallbackRouter for reliability
LLMRouter codingFallback = FallbackRouter.of(
new AnthropicClient("claude-sonnet-4"),
new OpenAIClient("gpt-4o")
);
TaskBasedRouter router = TaskBasedRouter.builder()
.forTask(TaskType.CODING, codingFallback) // With fallback
.defaultClient(new GroqClient("llama-3.3-70b"))
.build();
- See Also:
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic classBuilder for TaskBasedRouter.static final recordTask routing statistics.Nested classes/interfaces inherited from interface com.tnsai.llm.routing.LLMRouter
LLMRouter.RoutingStats -
Method Summary
Modifier and TypeMethodDescriptionstatic TaskBasedRouter.Builderbuilder()Creates a new builder for TaskBasedRouter.com.tnsai.llm.ChatResponsechat(String message, Optional<String> systemPrompt, Optional<List<Map<String, Object>>> history, Optional<List<Map<String, Object>>> tools) Routes a chat request to the appropriate client.com.tnsai.llm.ChatResponseChat with explicit task type override.com.tnsai.llm.ChatResponsechatAs(TaskType taskType, String message, Optional<String> systemPrompt, Optional<List<Map<String, Object>>> history, Optional<List<Map<String, Object>>> tools) Chat with explicit task type override and full parameters.classifyTask(String message) Classifies the message and returns the detected task type.classifyTask(String message, List<String> history) Classifies the message with conversation history context.com.tnsai.llm.LLMCapabilitiescom.tnsai.llm.LLMClientgetClientForTask(TaskType taskType) Returns the client assigned to a specific task type.List<com.tnsai.llm.LLMClient> Returns all registered LLM clients.getModel()com.tnsai.llm.LLMClientReturns the currently selected/primary client.getStats()Returns routing statistics (calls per provider, failures, latencies).Returns task-specific routing statistics.floatvoidResets routing statistics.Optional<com.tnsai.llm.LLMClient> selectByCapability(Predicate<com.tnsai.llm.LLMCapabilities> filter) Selects a client that matches the given capability predicate.streamChat(String message, Optional<String> systemPrompt, Optional<List<Map<String, Object>>> history, Optional<List<Map<String, Object>>> tools) Stream<com.tnsai.llm.ChatChunk> streamChatWithSpec(String message, Optional<String> systemPrompt, Optional<List<Map<String, Object>>> history, Optional<List<Map<String, Object>>> tools) Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface com.tnsai.llm.LLMClient
chat, chat, chat, chat, chat, getProviderName, getTopP, listModels, streamChat, streamChat, streamChatWithHandler, streamChatWithHandler, streamChatWithSpec
-
Method Details
-
builder
Creates a new builder for TaskBasedRouter. -
getClientForTask
Returns the client assigned to a specific task type.- Parameters:
taskType- The task type- Returns:
- The assigned client, or default client if not configured
-
classifyTask
Classifies the message and returns the detected task type.- Parameters:
message- The message to classify- Returns:
- Classification result with task type and confidence
-
classifyTask
Classifies the message with conversation history context.- Parameters:
message- The message to classifyhistory- Previous messages for context- Returns:
- Classification result with task type and confidence
-
chatAs
Chat with explicit task type override.- Parameters:
taskType- The task type to use (bypasses classification)message- The message- Returns:
- Chat response
-
chatAs
public com.tnsai.llm.ChatResponse chatAs(TaskType taskType, String message, Optional<String> systemPrompt, Optional<List<Map<String, Object>>> history, Optional<List<Map<String, Object>>> tools) Chat with explicit task type override and full parameters. -
chat
public com.tnsai.llm.ChatResponse chat(String message, Optional<String> systemPrompt, Optional<List<Map<String, Object>>> history, Optional<List<Map<String, Object>>> tools) Description copied from interface:LLMRouterRoutes a chat request to the appropriate client.The routing decision depends on the implementation:
- FallbackRouter: Uses primary, falls back on error
- CostBasedRouter: Selects cheapest viable option
- LatencyBasedRouter: Selects fastest option
-
streamChat
public Stream<String> streamChat(String message, Optional<String> systemPrompt, Optional<List<Map<String, Object>>> history, Optional<List<Map<String, Object>>> tools) - Specified by:
streamChatin interfacecom.tnsai.llm.LLMClient
-
streamChatWithSpec
public Stream<com.tnsai.llm.ChatChunk> streamChatWithSpec(String message, Optional<String> systemPrompt, Optional<List<Map<String, Object>>> history, Optional<List<Map<String, Object>>> tools) - Specified by:
streamChatWithSpecin interfacecom.tnsai.llm.LLMClient
-
getClients
Description copied from interface:LLMRouterReturns all registered LLM clients.- Specified by:
getClientsin interfaceLLMRouter
-
getPrimaryClient
public com.tnsai.llm.LLMClient getPrimaryClient()Description copied from interface:LLMRouterReturns the currently selected/primary client.- Specified by:
getPrimaryClientin interfaceLLMRouter
-
selectByCapability
public Optional<com.tnsai.llm.LLMClient> selectByCapability(Predicate<com.tnsai.llm.LLMCapabilities> filter) Description copied from interface:LLMRouterSelects a client that matches the given capability predicate.- Specified by:
selectByCapabilityin interfaceLLMRouter- Parameters:
filter- Predicate to filter by capabilities- Returns:
- The first matching client, or empty if none match
-
getModel
- Specified by:
getModelin interfacecom.tnsai.llm.LLMClient
-
getTemperature
public float getTemperature()- Specified by:
getTemperaturein interfacecom.tnsai.llm.LLMClient
-
getMaxTokens
- Specified by:
getMaxTokensin interfacecom.tnsai.llm.LLMClient
-
getCapabilities
public com.tnsai.llm.LLMCapabilities getCapabilities()- Specified by:
getCapabilitiesin interfacecom.tnsai.llm.LLMClient
-
getTaskStats
Returns task-specific routing statistics. -
getStats
Description copied from interface:LLMRouterReturns routing statistics (calls per provider, failures, latencies). -
resetStats
public void resetStats()Description copied from interface:LLMRouterResets routing statistics.- Specified by:
resetStatsin interfaceLLMRouter
-