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.
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.
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.
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.
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.
- 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.
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.v1exposes stateless REST endpoints. - Orchestration:
backend.services.customer_service - Execution:
backend.services.decision_service - Governance:
backend.services.model_registrymanages versioned ML models.
- Client Request: Sends
household_keyto API. - State Lookup: Retrieves current demographics and feature aggregations.
- Feature Engineering: Aggregates RFM metrics, spend velocity (90d trends), and discount dependencies.
- Inference: Feeds features to the
ModelRegistryfor concurrent scoring. - Decision: Rules engine evaluates margins and probabilities to select the best action.
- Response: Output is served, and side-effects (audit logs, telemetry) are handled asynchronously via SQLite.
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 |
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:
CRITICAL_PRESERVATION_PRIORITY(High Churn + High CLV)ENGAGEMENT_PRIORITY(Moderate Risk or Coupon Hunters)CROSS_SELL_PRIORITY(Healthy, High-Value)
- Recommendation: Combines the top prioritized action with the top recommended Next Best Offer (NBO).
- 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.
- 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
ModelRegistryloaded efficiently at applicationlifespanstartup.
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.
- LLM Timeouts: If the XAI LLM fails, the system returns a deterministic JSON explanation template.
- Invalid Requests: Missing customer keys intercept the
KeyErrorand return a standardized404instead of a500stack trace. - Graceful Degradation: Guarantees sub-millisecond response times even if secondary services degrade.
- Traceability: X-Request-ID propagation headers injected into all requests.
- Data Protection & Audit Logging: All critical decisions are immutable and logged to
decision_audit_logsin SQLite, tracking the exactrequest_id, priority, and action generated.
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- API Metrics:
MonitoringMiddlewareintercepts all traffic to calculate latency and status codes, logging tomonitoring_log.db. - ML Drift:
CustomerServicetriggersMonitoringService.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.
Discovery ➔ POC ➔ Pilot (A/B Holdout) ➔ UAT ➔ Go-Live ➔ Ongoing Optimization
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.
- Boot the server (
uvicorn backend.main:app --reload). - Start Observability (
streamlit run frontend/observability.py). - Run
scratch/test_pipeline.pyto simulate a prediction request forhousehold_key: 2500and view the transaction execute in real-time with sub-100ms latency.
- 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).
- Migration to fully distributed PostgreSQL.
- Integration of evidently.ai for automated drift statistical testing.
- OAuth2 / JWT Security implementations.
Engineered by Harsh Patel Specializing in applied machine learning, production engineering, and enterprise business value realization.