Introduction
Every enterprise embarking on generative AI faces a pivotal architectural decision: should we fine-tune a foundation model, or build a Retrieval-Augmented Generation (RAG) system? This choice is not merely technical it impacts budgets, timelines, maintainability, and long-term adaptability. Fine-tuning reshapes a model's internal weights to specialize in a domain, while RAG keeps the model general and injects knowledge dynamically at inference time.
The wrong choice leads to wasted resources: fine-tuning when data changes weekly, or building elaborate RAG pipelines when the task requires a specific tone and style that only weight updates can deliver. This article presents an enterprise-grade multi-agent LangGraph system that acts as a strategic advisor, analyzing your use case characteristics and recommending the optimal approach—fine-tuning, RAG, or a hybrid backed by quantitative reasoning and persistent memory of past decisions.
The Decision Framework: Seven Critical Signals
Before diving into code, let's establish the analytical framework our multi-agent system will use:
Signal | Favors RAG | Favors Fine-Tuning |
|---|---|---|
Data Freshness | Changes daily/weekly | Stable, curated corpus |
Knowledge Type | Factual, reference material | Behavioral, stylistic, procedural |
Source Attribution | Required (citations) | Not needed |
Domain Specificity | Broad, multi-source | Narrow, highly specialized |
Latency Requirements | Moderate acceptable | Ultra-low latency critical |
Budget & Expertise | Limited ML team | Strong ML engineering capacity |
Hallucination Risk | Must be minimized | Tolerable with guardrails |
Step-by-Step Implementation
1. Strategic Decision State Schema
from typing import List, Dict, Any, TypedDict, Optional, Literal
from langgraph.graph import StateGraph, END
from langchain_core.messages import HumanMessage, AIMessage
import redis
import json
class StrategyState(TypedDict):
messages: List
conversation_id: str
project_id: str
# Input characteristics
use_case_description: str
data_freshness_days: int
corpus_size: int
num_sources: int
requires_citations: bool
domain_specificity: Literal["broad", "moderate", "narrow"]
latency_budget_ms: int
budget_tier: Literal["low", "medium", "high"]
ml_team_size: int
hallucination_tolerance: Literal["none", "low", "moderate"]
task_type: Literal["qa", "summarization", "generation", "classification", "extraction"]
# Analysis outputs
rag_score: float
finetune_score: float
hybrid_score: float
key_factors: List[str]
risks: Dict[str, List[str]]
# Final recommendation
recommendation: Literal["rag", "finetune", "hybrid"]
reasoning: str
implementation_roadmap: List[str]
# Memory
past_decisions: List[Dict]
2. Requirements Analyzer Agent
from langchain_openai import ChatOpenAI
import re
class RequirementsAnalyzerAgent:
"""Parses and enriches the user's use case description"""
def __init__(self):
self.llm = ChatOpenAI(model="gpt-4", temperature=0.1)
def analyze(self, state: StrategyState) -> StrategyState:
prompt = f"""
You are an AI solutions architect. Analyze this use case and extract key factors.
Use Case: {state['use_case_description']}
Data Freshness: Every {state['data_freshness_days']} days
Corpus Size: {state['corpus_size']:,} documents
Number of Sources: {state['num_sources']}
Requires Citations: {state['requires_citations']}
Domain Specificity: {state['domain_specificity']}
Latency Budget: {state['latency_budget_ms']}ms
Budget Tier: {state['budget_tier']}
ML Team Size: {state['ml_team_size']} engineers
Hallucination Tolerance: {state['hallucination_tolerance']}
Task Type: {state['task_type']}
Extract 5-7 key factors that influence the fine-tuning vs RAG decision.
Format as a numbered list with brief explanations.
"""
response = self.llm.invoke(prompt)
state["key_factors"] = [
line.strip() for line in response.content.split("\n")
if re.match(r"^\d+[\.\)]", line.strip())
]
return state
3. Strategy Scoring Agent
class StrategyScoringAgent:
"""Computes quantitative scores for each approach"""
def score(self, state: StrategyState) -> StrategyState:
rag_score = 0.0
ft_score = 0.0
# Data freshness: RAG wins when data changes often
if state["data_freshness_days"] <= 1:
rag_score += 25
elif state["data_freshness_days"] <= 7:
rag_score += 15
else:
ft_score += 15
# Corpus size: RAG scales better with large corpora
if state["corpus_size"] > 100_000:
rag_score += 20
elif state["corpus_size"] < 10_000:
ft_score += 15
# Citations requirement
if state["requires_citations"]:
rag_score += 20
# Domain specificity
if state["domain_specificity"] == "narrow":
ft_score += 15
elif state["domain_specificity"] == "broad":
rag_score += 10
# Latency: Fine-tuning is faster (no retrieval step)
if state["latency_budget_ms"] < 500:
ft_score += 15
elif state["latency_budget_ms"] > 2000:
rag_score += 10
# Budget and team size
if state["budget_tier"] == "low" or state["ml_team_size"] < 3:
rag_score += 15
elif state["budget_tier"] == "high" and state["ml_team_size"] >= 5:
ft_score += 15
# Hallucination tolerance
if state["hallucination_tolerance"] == "none":
rag_score += 15
# Task type
if state["task_type"] in ["qa", "summarization", "extraction"]:
rag_score += 10
elif state["task_type"] in ["generation", "classification"]:
ft_score += 10
# Normalize to 0-100
max_possible = max(rag_score, ft_score, 1)
state["rag_score"] = min(100, (rag_score / max_possible) * 100)
state["finetune_score"] = min(100, (ft_score / max_possible) * 100)
# Hybrid is attractive when both scores are moderate
balance = 1 - abs(state["rag_score"] - state["finetune_score"]) / 100
state["hybrid_score"] = balance * 70 + (state["rag_score"] + state["finetune_score"]) / 4
return state
4. Risk Assessment Agent
class RiskAssessmentAgent:
"""Identifies risks for each approach"""
def __init__(self):
self.llm = ChatOpenAI(model="gpt-4", temperature=0.1)
def assess(self, state: StrategyState) -> StrategyState:
risks = {
"rag": [],
"finetune": [],
"hybrid": []
}
# RAG-specific risks
if state["latency_budget_ms"] < 500:
risks["rag"].append("Retrieval latency may exceed budget")
if state["corpus_size"] > 1_000_000:
risks["rag"].append("Vector search at scale requires careful sharding")
if state["task_type"] == "classification":
risks["rag"].append("RAG is suboptimal for pure classification tasks")
# Fine-tuning risks
if state["data_freshness_days"] <= 7:
risks["finetune"].append("Frequent retraining required as data changes")
if state["ml_team_size"] < 3:
risks["finetune"].append("Small team may struggle with training infrastructure")
if state["hallucination_tolerance"] == "none":
risks["finetune"].append("Fine-tuned models still hallucinate; guardrails needed")
if state["requires_citations"]:
risks["finetune"].append("Citations require additional attribution mechanisms")
# Hybrid risks
risks["hybrid"].append("Increased system complexity")
risks["hybrid"].append("Higher operational cost (retrieval + inference)")
if state["budget_tier"] == "low":
risks["hybrid"].append("May exceed budget constraints")
state["risks"] = risks
return state
5. Strategy Recommender Agent
class StrategyRecommenderAgent:
"""Synthesizes analysis into final recommendation"""
def __init__(self):
self.llm = ChatOpenAI(model="gpt-4", temperature=0.1)
def recommend(self, state: StrategyState) -> StrategyState:
# Determine winner
scores = {
"rag": state["rag_score"],
"finetune": state["finetune_score"],
"hybrid": state["hybrid_score"]
}
state["recommendation"] = max(scores, key=scores.get)
# Generate reasoning and roadmap
prompt = f"""
You are an AI architect delivering a strategic recommendation.
Use Case: {state['use_case_description']}
Scores - RAG: {state['rag_score']:.1f}, Fine-Tune: {state['finetune_score']:.1f}, Hybrid: {state['hybrid_score']:.1f}
Recommendation: {state['recommendation'].upper()}
Key Factors:
{chr(10).join(['- ' + f for f in state['key_factors']])}
Provide:
1. A concise 3-4 sentence reasoning paragraph explaining the recommendation.
2. A 5-step implementation roadmap with concrete actions.
Format as JSON:
{{"reasoning": "...", "roadmap": ["step1", "step2", ...]}}
"""
response = self.llm.invoke(prompt)
# Parse JSON response (simplified)
try:
import re
json_match = re.search(r'\{[\s\S]*\}', response.content)
if json_match:
parsed = json.loads(json_match.group())
state["reasoning"] = parsed["reasoning"]
state["implementation_roadmap"] = parsed["roadmap"]
else:
state["reasoning"] = response.content
state["implementation_roadmap"] = []
except Exception:
state["reasoning"] = response.content
state["implementation_roadmap"] = []
return state

Join the conversation! Your thoughts help the community grow.