Skip to content

Repository files navigation

RetailOS AI™

A Decision Intelligence Engine for Retail Customer Lifecycle Management

RetailOS AI™ bridges the gap between raw predictive analytics and automated, profit-maximizing business actions. It transforms predictive models (Churn, CLV, Next Best Offer) into an API-driven Decision Engine that orchestrates hyper-personalized, economically efficient CRM interventions in real-time.


1. Executive Summary

Modern retail analytics teams generate highly accurate machine learning models but struggle to translate these predictions into automated, high-ROI business actions. RetailOS AI solves this by introducing a Decision Engine and Explainability Layer that ingest customer telemetry, run multi-model inference, apply strict economic guardrails, and output the optimal business action (e.g., Retention Offer, Cross-Sell, Target Promotion).

Designed as a production-grade FastAPI microservice, RetailOS AI operates with sub-100ms latency, maintains full auditability via SQLite tracking, and continuously monitors its own business value through a dedicated Streamlit Observability suite.


2. Customer

RetailCo (Simulated): A multi-channel grocery and retail enterprise leveraging the Dunnhumby dataset. RetailCo serves millions of households, generating high-velocity transaction data but struggling to personalize outreach at scale. The retail landscape is highly competitive with razor-thin margins. RetailCo is facing pressure from e-commerce disruptors and needs to maximize the lifetime value (CLV) of its existing customer base while minimizing wasted promotional spend.

Key Stakeholders:

  • Chief Marketing Officer (CMO): Needs high Campaign ROI and reduced discount subsidization.
  • CRM / Campaign Managers: Need to drastically reduce the time spent defining audience segments.
  • Data Science Team: Needs a reliable path to deploy and govern their models in production.

3. Business Problem

RetailCo is over-discounting. By blasting mass promotions, they are subsidizing existing demand rather than driving incremental growth. Campaign generation takes 1–2 weeks of manual analyst effort. Predictive models exist but are not integrated into a real-time decision loop. This disconnect causes margin cannibalization, preventable churn of high-value customers, and high operational overhead.


4. Business Objectives

Strategic Objective: Shift from mass marketing to automated, precision-targeted decision intelligence.

Business KPIs:

  • Customer Churn: Reduce from baseline to target (e.g., 14.0% -> 11.5%).
  • Discount Efficiency: Reduce wasted promotional spend by 99% via precision targeting.
  • Decision Time: Reduce audience generation time from weeks to < 200 milliseconds.

5. Requirements

  • Functional: Multi-model inference via a single API request, business rule evaluation (eligibility, margins, priority), and natural language explainability (XAI) for every decision.
  • Non-Functional: High availability and sub-200ms response times.
  • Security: Traceability via X-Request-ID propagation and graceful error handling.
  • Data: Strict enforcement of feature contracts and temporal cutoff integrity (no future data leakage during inference).
  • Integration: RESTful OpenAPI v1 specifications for CRM integration.

6. Architecture

The application is built on FastAPI leveraging async I/O. It orchestrates domain-driven services: CustomerService, ModelService, DecisionService, RecommendationService, and ExplanationService.

  • API Layer: backend.routers.v1 exposes stateless REST endpoints.
  • Orchestration: backend.services.customer_service
  • Execution: backend.services.decision_service
  • Governance: backend.services.model_registry manages versioned ML models.

7. Data Flow

  1. Client Request: Sends household_key to API.
  2. State Lookup: Retrieves current demographics and feature aggregations.
  3. Feature Engineering: Aggregates RFM metrics, spend velocity (90d trends), and discount dependencies.
  4. Inference: Feeds features to the ModelRegistry for concurrent scoring.
  5. Decision: Rules engine evaluates margins and probabilities to select the best action.
  6. Response: Output is served, and side-effects (audit logs, telemetry) are handled asynchronously via SQLite.

8. AI/ML

Models are mapped in models_registry and trained with a strict historical cutoff (T=0) predicting forward (T+90 to T+180) to guarantee zero data leakage.

Model Component Algorithm Task Target Metric
Customer Health Rule-Based Index Pearson Corr
CLV Ridge Regression Regression R² / Spearman
Churn Logistic Regression Classification AUC-ROC
Coupon Binary Classifier Classification AUC-ROC
Promotion Binary Classifier Classification AUC-ROC
Affinity Apriori Association Mining Rule Count
Segmentation K-Means Clustering Silhouette Score

9. Decision Engine

The core logic in backend/services/decision_service.py maps raw probabilities to actions based on eligibility and priority.

  • Eligibility Rules: Do not issue a coupon if the expected discount cost > $5.00 (Margin Protection).
  • Prioritization:
    1. CRITICAL_PRESERVATION_PRIORITY (High Churn + High CLV)
    2. ENGAGEMENT_PRIORITY (Moderate Risk or Coupon Hunters)
    3. CROSS_SELL_PRIORITY (Healthy, High-Value)
  • Recommendation: Combines the top prioritized action with the top recommended Next Best Offer (NBO).

10. Integration

  • REST APIs: POST /api/v1/customers/predict (Full 360 Prediction + Decision).
  • Authentication: Designed to ingest Webhooks/Events via standard HTTP protocols, prepared for Bearer token auth.
  • CRM / Campaigns: Mapped directly into CRM platforms (Salesforce/Braze) via the REST contract.

11. Implementation

  • Backend: Python 3.12, FastAPI, Pydantic, Starlette.
  • Frontend: Streamlit (for Observability and internal dashboards).
  • ML Pipeline: Scikit-learn (Models), Pandas (Inference Transforms).
  • Database: SQLite (Audit Logging / Telemetry).
  • Model Serving: Custom ModelRegistry loaded efficiently at application lifespan startup.

12. Evaluation

Validated via automated Master Pack Generation. Evaluated across 9 dimensions including Data Integrity, Model Precision, and API Resilience.

  • Churn AUC: 0.885
  • CLV R²: 0.713
  • Coupon AUC: 0.816
  • UAT: 100% Pass rate across all simulated Business User Acceptance Testing (UAT) scenarios.

13. Failure Handling

  • LLM Timeouts: If the XAI LLM fails, the system returns a deterministic JSON explanation template.
  • Invalid Requests: Missing customer keys intercept the KeyError and return a standardized 404 instead of a 500 stack trace.
  • Graceful Degradation: Guarantees sub-millisecond response times even if secondary services degrade.

14. Security

  • Traceability: X-Request-ID propagation headers injected into all requests.
  • Data Protection & Audit Logging: All critical decisions are immutable and logged to decision_audit_logs in SQLite, tracking the exact request_id, priority, and action generated.

15. Deployment

Ready to be containerized via Docker and deployed utilizing environment variables via backend.core.config.BaseSettings.

python -m venv venv
venv\Scripts\activate
pip install -r requirements.txt
uvicorn backend.main:app --reload --port 8000

16. Observability

  • API Metrics: MonitoringMiddleware intercepts all traffic to calculate latency and status codes, logging to monitoring_log.db.
  • ML Drift: CustomerService triggers MonitoringService.log_prediction() on every inference to record the feature payload and exact prediction output.
  • Dashboard: Visualized in real-time via streamlit run frontend/observability.py.

17. Customer Deployment

Discovery ➔ POC ➔ Pilot (A/B Holdout) ➔ UAT ➔ Go-Live ➔ Ongoing Optimization


18. Business Impact

RetailOS utilizes a precision targeting methodology. Instead of spending $6,247 on a mass campaign, it isolates the top 10 at-risk, high-value customers, spending only $15.00 to preserve $5,569 in revenue. This equates to a 99.8% reduction in promotional waste.


19. Demo

  1. Boot the server (uvicorn backend.main:app --reload).
  2. Start Observability (streamlit run frontend/observability.py).
  3. Run scratch/test_pipeline.py to simulate a prediction request for household_key: 2500 and view the transaction execute in real-time with sub-100ms latency.

20. Limitations

  • The promotion response model (AUC 0.555) requires retraining on actual production A/B test data to escape the bias of historical mass-campaigns.
  • SQLite concurrent write locks under extreme load (to be migrated to Postgres for production).

21. Roadmap

  1. Migration to fully distributed PostgreSQL.
  2. Integration of evidently.ai for automated drift statistical testing.
  3. OAuth2 / JWT Security implementations.

Engineered by Harsh Patel Specializing in applied machine learning, production engineering, and enterprise business value realization.

About

Enterprise AI decision-intelligence platform combining predictive ML, recommendation, business decisioning, explainability, APIs, deployment and value realization.

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages