AI Pricing Intelligence
Relevant source files
The following files were used as context for generating this wiki page:
AI Pricing Intelligence is a sophisticated system within DRX designed to provide automated, data-driven pricing recommendations and inventory insights. It leverages historical sales data, current stock levels, cost trends, and demand velocity to calculate optimal product prices that balance profitability with market competitiveness. The system is integrated directly into the inventory management workflow, allowing business owners to apply recommendations with a single click while maintaining a full audit trail of all AI-assisted decisions.
The module operates through a combination of deterministic logic (via the PricingEngine) and LLM-powered analysis (via Supabase Edge Functions). It aims to eliminate guesswork in retail pricing by identifying trends such as rising supplier costs, inventory overstock, or accelerating demand, and translating these into actionable price adjustments. Sources: README.md, lib/utils/pricing-engine.ts:182-250, app/(app)/ai-intelligence/page.tsx:16-25
System Architecture
The AI Pricing Intelligence architecture is divided into three layers: the Database Layer for persisting recommendations and logs, the Logic Layer for calculating adjustments, and the Interface Layer for user interaction.
Data Flow and Processing
The system follows a reactive flow where product data is gathered, analyzed against predefined factors, and then presented as a recommendation.
The diagram above illustrates the end-to-end flow from a user triggering an analysis to the eventual application of a new price. Sources: lib/utils/ai-engine.ts:40-100, db/migrations/20250128000001_ai_pricing_inventory_intel.sql:1-60
Pricing Engine Logic
The PricingEngine class is the core component responsible for generating recommendations. It evaluates several PricingFactors to arrive at a recommendedPrice.
Pricing Factors
The engine considers the following technical metrics:
- Sales Velocity: Tracked over 7, 30, and 90-day intervals to determine demand acceleration.
- Inventory Position: Calculates "Days Supply" to identify overstock or critical shortages.
- Cost Trends: Analyzes
costHistoryto detect if supplier prices are rising or declining. - Margin Requirements: Uses a base multiplier (e.g., 0.3) to ensure minimum profitability.
Sources: lib/utils/pricing-engine.ts:5-23, edge/ai_pricing_inventory/index.ts:100-125
Adjustment Modifiers
The engine applies standard multipliers based on the analysis results:
| Condition | Price Adjustment | Logic / Reasoning |
|---|---|---|
| Critical Stock | +8% | Opportunity for premium pricing due to high urgency. |
| Overstock | -8% | Recommended discount to clear stagnant inventory. |
| Demand Accelerating | +6% | High demand elasticity supports higher prices. |
| Cost Rising (>5%) | +5% | Maintaining margins against supplier price hikes. |
| Stagnant Product | -15% | Deep discount to stimulate first-time demand. |
Sources: lib/utils/pricing-engine.ts:184-245, edge/ai_pricing_inventory/index.ts:110-120
Database Schema and Audit Trails
To maintain transparency and compliance, all AI actions are logged in dedicated tables. This ensures that every price change can be traced back to its original recommendation and the reasoning provided by the AI.
Key Database Tables
| Table Name | Description | Key Fields |
|---|---|---|
ai_pricing_recommendations | Stores pending and applied AI price suggestions. | recommended_price, confidence_score, reasoning, status |
ai_decision_log | Immutable audit trail for all pricing decisions. | old_value, new_value, decided_by, override_reason |
sales_velocity_cache | Performance cache for pre-calculated sales metrics. | velocity_7d, velocity_30d, last_30d_units |
Sources: db/migrations/20250128000001_ai_pricing_inventory_intel.sql:8-23, db/migrations/20250128000001_ai_pricing_inventory_intel.sql:63-76
Recommendation Lifecycle
Recommendations move through a defined state machine:
- Pending: Recommendation generated but not yet reviewed.
- Applied: User accepted the price; inventory updated.
- Ignored: User dismissed the recommendation (logged in audit trail).
AI Engine Integration
The AIEngine acts as a coordinator between the raw pricing logic and the business intelligence features. It generates "Composite Insights" which combine pricing suggestions with inventory forecasts.
// lib/utils/ai-engine.ts:145-155
if (pricing) {
const pricingAction =
pricing.recommendedPrice > pricing.recommendedRangeMin
? `increase to $${pricing.recommendedPrice.toFixed(2)}`
: `consider reducing to $${pricing.recommendedPrice.toFixed(2)}`;
insights.push(
`PRICING: ${productName} confidence ${pricing.confidenceScore.toFixed(0)}% - ${pricingAction}`
);
}
Insights Analysis Types
The system supports multiple analysis types via the ai_insights Edge Function:
- Pricing: Analyzes current inventory against recent sales history.
- Inventory: Evaluates reorder levels and transaction logs.
- Credit: Examines pending credit transactions and customer limits.
- Cashflow: Projects liquidity based on recent sales and credit payments.
Sources: lib/utils/ai-engine.ts:140-180, edge/ai_insights/index.ts:30-100
Implementation Details
Constraint Application
The system enforces safety boundaries to prevent the AI from suggesting unrealistic prices. The applyConstraints method ensures that recommendations never fall below a minimum margin (cost + 10%) or exceed a global safety factor (typically 2.0x cost).
// lib/utils/pricing-engine.ts:110-120
const absoluteMin = cost * (1 + minMarginRequired);
const absoluteMax = cost * globalSafetyFactor;
const finalMin = Math.max(absoluteMin, minPrice || absoluteMin);
const finalMax = Math.min(absoluteMax, maxPrice || absoluteMax);
const finalPrice = Math.max(finalMin, Math.min(recommendedPrice, finalMax));
Sources: lib/utils/pricing-engine.ts:102-125
Conclusion
AI Pricing Intelligence provides a critical layer of automation for DRX, turning raw inventory and sales data into actionable financial strategies. By combining deterministic pricing engines with AI-driven insights, the system helps businesses maintain healthy margins while responding dynamically to stock levels and market demand. Every decision is backed by a robust audit trail, ensuring full accountability for all automated adjustments. Sources: README.md, lib/utils/ai-engine.ts:200-220