Multi-Agent Workflow
The AI Ingredient Scanner uses a sophisticated multi-agent architecture powered by LangGraph. This design enables specialized agents to handle different aspects of ingredient analysis while maintaining quality through validation loops.
Workflow Overviewโ
Agent Responsibilitiesโ
Supervisor Agentโ
The Supervisor acts as the workflow orchestrator, determining which agent should process next based on the current state.
# Routing Logic
def route_next(state: WorkflowState) -> str:
if not has_research_data(state):
return "research"
if not has_analysis_report(state):
return "analysis"
if needs_validation(state):
return "critic"
return "end"
Key Decisions:
- Route to Research if ingredient data is missing
- Route to Analysis if report needs generation
- Route to Critic for quality validation
- Handle retry logic (max 2 attempts)
Research Agentโ
The Research Agent fetches ingredient safety data from multiple sources.
Features:
- Parallel Processing: Handles 3+ ingredients concurrently
- Dual-Source Strategy: Qdrant first, Google Search fallback
- Confidence Threshold: 0.7 minimum for Qdrant results
- Auto-Learning: Saves search results back to Qdrant
Data Retrieved:
| Field | Description |
|---|---|
safety_rating | 1-10 scale (10 = safest) |
concerns | Known safety issues |
recommendation | SAFE / CAUTION / AVOID |
allergy_risk_flag | HIGH / LOW |
origin | Natural / Synthetic |
regulatory_status | US FDA and EU status |
Analysis Agentโ
The Analysis Agent generates personalized safety reports using Gemini 2.0 Flash.
Personalization Factors:
- User allergies (prominent warnings)
- Skin type (relevant recommendations)
- Expertise level (beginner vs expert tone)
Output Sections:
- Overall Verdict with safety bar
- Executive Summary
- Allergen/Ingredient Warnings
- Recommendations for skin type
- Detailed ingredient table
Critic Agentโ
The Critic Agent validates report quality using a 5-gate validation system.
| Gate | Check | Pass Criteria |
|---|---|---|
| 1. Completeness | All ingredients addressed | 8/9 ingredients = PASS |
| 2. Format | Markdown table structure | Valid table exists |
| 3. Allergen Match | User allergies flagged | Matching ingredients highlighted |
| 4. Consistency | Ratings match concerns | Ratings 1-10, valid recommendations |
| 5. Tone | Appropriate for expertise | Readable and informative |
Validation Outcomes:
- APPROVED: All gates pass โ deliver report
- REJECTED: Critical failures โ retry analysis (max 2)
- ESCALATED: Max retries exceeded โ deliver with warning
State Managementโ
The workflow uses a typed state dictionary to maintain context across agents:
class WorkflowState(TypedDict):
session_id: str
product_name: str
raw_ingredients: list[str]
user_profile: UserProfile
ingredient_data: list[IngredientData]
analysis_report: AnalysisReport | None
critic_feedback: CriticFeedback | None
retry_count: int
routing_history: list[str]
stage_timings: StageTiming
error: str | None
Execution Flowโ
Performance Characteristicsโ
| Metric | Typical Value |
|---|---|
| Research time | 3-5 seconds |
| Analysis time | 5-8 seconds |
| Critic time | 2-3 seconds |
| Total (first run) | 10-15 seconds |
| Total (cached) | 5-8 seconds |