Welcome

Introduction

Introduction

Setup & Installation

Architecture & Design

Architecture

Features & Scenarios

Deployment

Deployment

API Reference

API Reference

Tools & Utilities

Scripts & UtilitiesTroubleshooting

Architecture

System architecture and design overview

Architecture Overview

SynapseAI is built on a modern, composable architecture that combines conversational AI, e-commerce platforms, and intelligent automation. This documentation uses FlavorFlux (a fictional CPG brand) as the demo use case.

Strategic Vision

SynapseAI enables CPG brands (like our demo brand FlavorFlux) to become truly adaptive through:

  • AI-powered community intelligence
  • Predictive analytics
  • Seamless B2B commerce automation
  • Conversational ordering experiences

High-Level Architecture

System Components

Core Technologies

Backend Stack

  • FastAPI: High-performance Python web framework
  • LangGraph: Stateful AI agent orchestration
  • LiteLLM: Multi-provider LLM interface
  • Redis: Session and state management
  • Pydantic: Data validation and serialization

Frontend Stack

  • React 18: UI component library
  • Vite: Fast build tool and dev server
  • TanStack Router: Type-safe routing
  • TanStack Query: Server state management
  • TanStack Store: Client state management
  • Shadcn/ui: Component library
  • Tailwind CSS: Utility-first styling

Infrastructure

  • AWS ECS Fargate: Containerized service hosting
  • AWS ALB: Load balancing and SSL termination
  • AWS ElastiCache: Redis cluster
  • AWS ECR: Container registry
  • Terraform: Infrastructure as Code

LangGraph Agent Architecture

The conversational AI is built using LangGraph with specialized nodes for different intents:

Agent Flow

Key Nodes

1. Intent Detection

  • intent_detector: Classifies user messages into intents
  • Routes to appropriate specialized handlers
  • Supports multi-turn conversations

2. Product Operations

  • product_processor: Lists and searches products
  • product_resolution: Matches user queries to products
  • product_disambiguation: Handles multiple matches

3. Cart Management

  • cart_get_processor: Views cart contents
  • cart_update_processor: Parses cart addition requests
  • cart_add_processor: Adds items to cart via MCP
  • cart_remove_processor: Removes items from cart

4. Order Operations

  • order_processor: Places orders
  • order_history_processor: Shows past orders
  • reorder_processor: Repeats previous orders

5. Campaign Management

  • promotion_processor: Lists active campaigns
  • Integrates with Voucherify via MCP

6. Response Generation

  • response_generator: Formats AI responses
  • Handles structured data display
  • Manages conversation context

MCP (Model Context Protocol) Integration

MCP provides a standardized way to integrate external services:

MCP Clients

Commercetools MCP

Endpoint: http://localhost:3000

Capabilities:

  • Product catalog management
  • Cart operations (create, update, view)
  • Order placement and tracking
  • Customer management

Voucherify MCP

Endpoint: http://localhost:3002

Capabilities:

  • Campaign listing
  • Promotion management
  • Voucher validation
  • Loyalty program integration

MCP Client Pattern

from app.mcp.client import MCPClient

# Initialize client
ct_client = MCPClient(base_url="http://localhost:3000")

# Make MCP calls
products = await ct_client.call_tool(
    tool_name="list-products",
    arguments={"filter": "categories:yogurt"}
)

Data Flow

Conversational Order Flow

  1. User Input → Frontend chat interface
  2. API Request → POST /agent/chat
  3. Intent Detection → LangGraph classifies intent
  4. Product Resolution → Match products via MCP
  5. Cart Update → Add items via Commercetools MCP
  6. Promotion Check → Validate via Voucherify MCP
  7. Response Generation → Format and return
  8. UI Update → Display in chat interface

Authentication Flow

  1. User Login → POST /auth/login
  2. Token Generation → JWT with claims
  3. Session Creation → Store in Redis
  4. Token Storage → Frontend localStorage
  5. API Requests → Include Bearer token
  6. Token Validation → Verify on each request
  7. Auto Refresh → Refresh before expiry

Session Management

Redis Session Store

Structure:

{
  "session_id": "user-123",
  "customer_id": "ct-customer-abc",
  "messages": [
    {"role": "user", "content": "Show me products"},
    {"role": "assistant", "content": "Here are our products..."}
  ],
  "cart_id": "cart-xyz",
  "context": {
    "last_intent": "product_operations",
    "disambiguation_state": null
  },
  "ttl": 3600
}

Session Lifecycle

  • Creation: On first message
  • Updates: After each interaction
  • Expiry: 1 hour of inactivity
  • Cleanup: Automatic Redis TTL

Security Architecture

Authentication

  • JWT Tokens: Stateless authentication
  • Refresh Tokens: Long-lived renewal
  • Token Claims: User ID, customer ID, roles

Authorization

  • Role-Based: Admin, buyer, store rep
  • Resource-Based: Own orders only
  • API Gateway: Validates all requests

CORS Configuration

CORS_ORIGINS = [
    "http://localhost:3000",
    "http://localhost:5173",
    "https://your-production-domain.com"
]

Secrets Management

  • Development: .env files
  • Production: AWS Secrets Manager
  • Rotation: Automatic for sensitive keys

AI and LLM Strategy

Dual LLM Approach

LiteLLM (Basic Tasks)

  • Intent classification
  • Product search
  • Text parsing
  • Response generation

Models Supported:

  • OpenAI (GPT-4, GPT-3.5)
  • Cerebras (Llama 3.3 70B)
  • Groq (Fast inference)
  • Together AI

Advanced AI (Complex Tasks)

Campaign Template AI:

  • Community sentiment analysis
  • Campaign generation
  • Smart targeting
  • AWS Bedrock integration

Demand Forecasting AI:

  • Historical sales analysis
  • Inventory optimization
  • Trend prediction
  • AWS Forecast integration

Predictive Reordering AI:

  • Sales pattern analysis
  • Stockout prevention
  • Inventory turnover
  • AWS Forecast integration

LLM Router

Intelligent routing between LiteLLM and Advanced AI:

  • Cost optimization
  • Performance balancing
  • Fallback mechanisms
  • Request queuing

Composable Commerce Core

Commercetools Integration

Purpose: E-commerce platform Capabilities:

  • Product catalog
  • Cart management
  • Order processing
  • Customer management

Voucherify Integration

Purpose: Promotion engine Capabilities:

  • Dynamic promotions
  • Loyalty programs
  • Volume discounts
  • Campaign analytics

Scalability Considerations

Horizontal Scaling

  • ECS Tasks: Auto-scaling based on CPU/memory
  • Redis Cluster: Sharded for high throughput
  • Load Balancer: Distributes traffic evenly

Caching Strategy

  • Redis: Session data, product catalog cache
  • LiteLLM: Response caching for common queries
  • CDN: Static assets (frontend)

Performance Optimization

  • Connection Pooling: HTTP clients, Redis
  • Async Operations: FastAPI async/await
  • Lazy Loading: Frontend code splitting

Monitoring and Observability

Logging

  • Application Logs: CloudWatch Logs
  • Access Logs: ALB logs
  • Error Tracking: Structured logging

Metrics

  • Application Metrics: Custom CloudWatch metrics
  • Infrastructure Metrics: ECS, Redis, ALB
  • Business Metrics: Orders, sessions, conversions

Health Checks

  • Backend: GET /health
  • MCP Servers: Connection checks
  • Redis: Ping checks
  • Dependencies: External service monitoring

Next Steps

  • Backend Setup - Set up the backend
  • Frontend Setup - Set up the frontend
  • User Scenarios - Learn about user workflows
  • Deployment - Deploy to production
  • C4 Architecture - Detailed C4 diagrams

MCP Servers

Set up Model Context Protocol servers

Deployment

Infrastructure setup and deployment guide

On this page

Architecture OverviewStrategic VisionHigh-Level ArchitectureSystem ComponentsCore TechnologiesBackend StackFrontend StackInfrastructureLangGraph Agent ArchitectureAgent FlowKey Nodes1. Intent Detection2. Product Operations3. Cart Management4. Order Operations5. Campaign Management6. Response GenerationMCP (Model Context Protocol) IntegrationMCP ClientsCommercetools MCPVoucherify MCPMCP Client PatternData FlowConversational Order FlowAuthentication FlowSession ManagementRedis Session StoreSession LifecycleSecurity ArchitectureAuthenticationAuthorizationCORS ConfigurationSecrets ManagementAI and LLM StrategyDual LLM ApproachLiteLLM (Basic Tasks)Advanced AI (Complex Tasks)LLM RouterComposable Commerce CoreCommercetools IntegrationVoucherify IntegrationScalability ConsiderationsHorizontal ScalingCaching StrategyPerformance OptimizationMonitoring and ObservabilityLoggingMetricsHealth ChecksNext Steps