๐ฏLLM Pattern Decision Guide
๐ฏ Pattern Decision Matrix
User Need
Primary Patterns
Implementation Time
Enterprise Examples
Expected ROI
โก Quick Implementation Templates
1. Multi-Provider LLM Integration (Most Common)
from abc import ABC, abstractmethod
from typing import Dict, Any, List
# Adapter Pattern - Unified Interface
class LLMProvider(ABC):
@abstractmethod
def complete(self, prompt: str, **kwargs) -> str:
pass
class OpenAIAdapter(LLMProvider):
def complete(self, prompt: str, **kwargs) -> str:
# OpenAI-specific implementation
return openai.ChatCompletion.create(
model=kwargs.get("model", "gpt-4"),
messages=[{"role": "user", "content": prompt}]
).choices[0].message.content
class AnthropicAdapter(LLMProvider):
def complete(self, prompt: str, **kwargs) -> str:
# Anthropic-specific implementation
return anthropic.Anthropic().messages.create(
model=kwargs.get("model", "claude-3-sonnet"),
messages=[{"role": "user", "content": prompt}]
).content
# Strategy Pattern - Provider Selection
class ProviderStrategy(ABC):
@abstractmethod
def select_provider(self, providers: List[LLMProvider], context: Dict) -> LLMProvider:
pass
class CostOptimizedStrategy(ProviderStrategy):
def select_provider(self, providers: List[LLMProvider], context: Dict) -> LLMProvider:
# Select cheapest provider for the request
return min(providers, key=lambda p: p.get_cost_per_token())
class PerformanceStrategy(ProviderStrategy):
def select_provider(self, providers: List[LLMProvider], context: Dict) -> LLMProvider:
# Select fastest provider
return min(providers, key=lambda p: p.get_average_latency())
# Factory Pattern - Provider Creation
class LLMProviderFactory:
@staticmethod
def create_provider(provider_type: str) -> LLMProvider:
providers = {
"openai": OpenAIAdapter,
"anthropic": AnthropicAdapter,
"google": GoogleAdapter
}
return providers[provider_type]()
# Unified Client
class UnifiedLLMClient:
def __init__(self, strategy: ProviderStrategy):
self.providers = [
LLMProviderFactory.create_provider("openai"),
LLMProviderFactory.create_provider("anthropic")
]
self.strategy = strategy
def complete(self, prompt: str, **kwargs) -> str:
provider = self.strategy.select_provider(self.providers, kwargs)
return provider.complete(prompt, **kwargs)
# Usage
client = UnifiedLLMClient(CostOptimizedStrategy())
response = client.complete("Explain quantum computing")2. AI Tool Registration System (FastMCP Style)
3. Enterprise AI Gateway (Production-Ready)
๐ซ Common Anti-Patterns in LLM Systems
โ What NOT to Do
1. Direct API Calls Everywhere
2. No Cost Tracking
3. Synchronous Processing Only
4. No Error Handling or Fallbacks
๐๏ธ Advanced Pattern Combinations
Enterprise AI Platform Stack
๐ Performance & Cost Optimization
Pattern-Based Optimizations
Pattern
Optimization
Typical Savings
Implementation Time vs Value Matrix
๐ฏ Pattern Selection Flowchart
๐ง Quick Start Commands
1. Multi-Provider Setup (5 minutes)
2. Tool Registration (3 minutes)
3. Enterprise Gateway (15 minutes)
Last updated