Skip to main content

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:

FieldDescription
safety_rating1-10 scale (10 = safest)
concernsKnown safety issues
recommendationSAFE / CAUTION / AVOID
allergy_risk_flagHIGH / LOW
originNatural / Synthetic
regulatory_statusUS 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:

  1. Overall Verdict with safety bar
  2. Executive Summary
  3. Allergen/Ingredient Warnings
  4. Recommendations for skin type
  5. Detailed ingredient table

Critic Agentโ€‹

The Critic Agent validates report quality using a 5-gate validation system.

GateCheckPass Criteria
1. CompletenessAll ingredients addressed8/9 ingredients = PASS
2. FormatMarkdown table structureValid table exists
3. Allergen MatchUser allergies flaggedMatching ingredients highlighted
4. ConsistencyRatings match concernsRatings 1-10, valid recommendations
5. ToneAppropriate for expertiseReadable 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โ€‹

MetricTypical Value
Research time3-5 seconds
Analysis time5-8 seconds
Critic time2-3 seconds
Total (first run)10-15 seconds
Total (cached)5-8 seconds