Chapter 3: LLM-Specific Challenges
1. Hallucination Management
LLMs can generate plausible but incorrect information:
class HallucinationDetector:
def __init__(self, fact_checker_model):
self.fact_checker = fact_checker_model
def check_response(self, query: str, response: str, sources: list) -> dict:
"""Check if response is grounded in sources."""
# Extract claims from response
claims = self.extract_claims(response)
# Verify each claim against sources
verified_claims = []
hallucinated_claims = []
for claim in claims:
if self.verify_against_sources(claim, sources):
verified_claims.append(claim)
else:
hallucinated_claims.append(claim)
return {
"hallucination_rate": len(hallucinated_claims) / len(claims),
"verified_claims": verified_claims,
"hallucinated_claims": hallucinated_claims
}
2. Safety and Content Filtering
class SafetyGuard:
def __init__(self):
self.blocked_topics = ["illegal activities", "harmful content", ...]
self.pii_patterns = [r"\b\d{3}-\d{2}-\d{4}\b", ...] # SSN, etc.
def check_input(self, user_input: str) -> tuple[bool, str]:
"""Check user input for safety issues."""
# Topic filtering
for topic in self.blocked_topics:
if self.detects_topic(user_input, topic):
return False, f"Blocked topic: {topic}"
return True, "OK"
def check_output(self, llm_output: str) -> str:
"""Sanitize LLM output."""
# Remove PII
for pattern in self.pii_patterns:
llm_output = re.sub(pattern, "[REDACTED]", llm_output)
return llm_output
3. Latency Optimization
| Technique | Latency Reduction | Trade-off |
|---|---|---|
| Streaming | Perceived latency | Implementation complexity |
| Caching | 90%+ on cache hits | Storage, cache invalidation |
| Smaller models | 50-80% | Quality |
| Batching | Variable | Response time |
4. Cost Management
class CostTracker:
def __init__(self, budget_limit: float):
self.budget_limit = budget_limit
self.total_cost = 0
def track_request(self, model: str, input_tokens: int, output_tokens: int):
cost = calculate_cost(input_tokens, output_tokens, model)
self.total_cost += cost
if self.total_cost > self.budget_limit * 0.8:
alert_team("Approaching budget limit")
if self.total_cost > self.budget_limit:
raise BudgetExceededError("Budget limit reached")
return cost
Next: Module 10 Quiz