Reducing API Material Scrap and Dynamic Costing in South African Pharma Manufacturing
The brief we were given The client requested a machine learning demand forecasting model to predict API purchasing needs, believing over-ordering caused drum expiry in warehouses.
Scrap was driven by rigid MRP batch logic, unmonitored API potency degradation over time, and LIMS re-testing backlogs that caused valid raw materials to be written off prematurely.
- Customer
- A mid-sized generic pharmaceutical manufacturer in Gauteng
- Sector
- Pharmaceuticals
- Location
- South Africa
- Size
- 650 employees
- Engagement
- 18 weeks
The situation
The manufacturer produces generic oral solid dosage drugs across three production lines in Gauteng. High raw material costs, particularly imported Active Pharmaceutical Ingredients (APIs), directly impact operating margins. Executive leadership believed their core issue was imprecise sales forecasting, leading procurement teams to buy excess API volume that sat in warehouses until passing official expiration dates.
Operations were bound by strict South African Health Products Regulatory Authority (SAHPRA) compliance rules. Raw materials nearing their nominal shelf life had to be quarantined or re-tested. The engineering brief focused entirely on building time-series sales forecasting models to prevent excess inventory from entering the warehouse in the first place.
How it was approached
The Forward Deployed Engineer spent the first two weeks shadowing warehouse staff, quality control chemists, and production planners rather than tuning forecasting algorithms. Physical inventory audits revealed drums of high-value APIs marked as scrap despite maintaining sufficient chemical potency. The constraint was not purchasing volume, but how material potency degradation was tracked and accounted for in manufacturing batch sizes.
The FDE built an integrated data pipeline linking warehouse storage sensors, LIMS laboratory re-test records, and SAP ERP batch scheduling logic. By predicting true potency degradation and automating re-testing queues, the team enabled dynamic batch adjustment, using older APIs safely before write-off.
The engagement, as it happened
Each step carries what the engineer did and, separately, what a Forward Deployed Engineer should take from it.
-
Week 0 Discovery
Initial Brief and Warehouse Floor Observation
The client asked for a sales-driven demand forecasting model to stop API over-purchasing. Walkthroughs of the Gauteng warehouse revealed dozens of sealed API drums marked for destruction despite having months of valid shelf-life left. The FDE observed that material was quarantine-locked because testing backlogs prevented re-certification before automated ERP write-off dates.
What an FDE takes from this Never take the client's problem diagnosis at face value; observe physical processes to locate where value actually leaks.
-
Weeks 1-2 Discovery
Data Mapping and ERP/LIMS Disconnect
The FDE audited data flows between SAP ECC and the LabWare LIMS database. While LIMS recorded precise chemical assay potency percentages for unsealed drums, SAP assumed fixed nominal potency (100%) for all formula calculations. When potency dropped slightly, planners manually calculated compensating API weights on paper, leading to frequent calculation errors and skipped batches.
What an FDE takes from this System disconnects between quality control and production planning create hidden material inefficiency.
-
Weeks 3-4 Build
Early Win: Re-Testing Queue Prioritization
To deliver early value, the engineer deployed a light Streamlit dashboard for the Quality Control lab. The tool queried SAP inventory and LIMS re-test schedules, flagging drum batches that were within 60 days of re-test deadlines and tied to upcoming production orders. Lab technicians prioritized these samples, clearing the immediate write-off backlog.
What an FDE takes from this Shipping a basic operational tool early builds trust and opens access to deeper production data.
-
Weeks 5-7 Evaluation
Potency Degradation Modeling Setback
The FDE built a linear decay model to predict API potency loss over time based on manufacturer certificates of analysis. However, test accuracy failed during validation, with errors exceeding 14%. Investigations showed that summer humidity spikes in storehouse Bay C accelerated degradation for hygroscopic compounds, breaking the linear assumptions.
What an FDE takes from this Assumptions built on clean vendor spec sheets fall apart under real ambient storage conditions.
-
Weeks 8-10 Build
Revised Degradation Model with Ambient Data
The engineer upgraded the architecture to an XGBoost regressor trained on historical LIMS assay results combined with IoT temperature and humidity telemetry from storehouse sensors. The new model predicted active assay concentration within a 1.2% error margin across all stored API families, accounting for environmental storage drifts.
What an FDE takes from this Physical environment data is often the missing feature in industrial chemical modeling.
-
Weeks 11-12 Integration
SAP Integration for Dynamic Batch Costing
The model outputs were wired into SAP via custom FastAPI endpoints running on internal Docker containers. When a production order was created, the service fetched current assay potency for available API drums and automatically calculated the required raw material weight adjustment. This ensured exact active mass per batch while accurately adjusting batch unit costing.
What an FDE takes from this Integration must alter core ERP logic directly to change real operational behavior.
-
Weeks 13-14 Automation
ETL Pipeline Automation with Airflow
Data ingestion from LIMS, IoT storage logs, and SAP inventory was automated using Apache Airflow DAGs running nightly. The pipeline updated potency forecasts, recalculated batch costing models, and flagged decaying drums for priority consumption in the next day's scheduling run.
What an FDE takes from this Automated pipelines convert statistical models into dependable daily operational tools.
-
Weeks 15-16 Adoption
Planner Workflow and Floor Adoption
Production planners initially resisted automated batch adjustments, preferring manual spreadsheet checks. The FDE spent four days on the floor conducting side-by-side validations. Once planners saw the system automatically generate valid SAHPRA batch records with exact active mass conversions, adoption reached 100%.
What an FDE takes from this Engineer-led floor support turns suspicious users into active advocates.
-
Weeks 17-18 Outcome
Impact Audit and Standard Operating Procedures
The final review showed a 73% drop in discarded API value over the quarter. Batch yield variance stabilized to near zero, and formula costing errors disappeared. The system was formally documented and integrated into the plant's SAHPRA standard operating procedures.
What an FDE takes from this A technical solution is only complete when embedded in regulatory and operational compliance standards.
Measured outcomes
Before and after, so each claim is checkable rather than an adjective.
| Measure | Before | After | Change |
|---|---|---|---|
|
Annual API scrap value
Cost
Direct savings from consuming near-expiry drums via dynamic batching.
|
ZAR 14.2M | ZAR 3.8M | -73% |
|
API re-testing cycle time
Speed
Prioritized lab queue based on urgent production order mapping.
|
14 days | 2 days | -85.7% |
|
Potency-adjusted batch costing accuracy
Accuracy
Calculated actual active API mass used instead of nominal weight.
|
78% | 96% | +18% |
|
Expired API volume discarded
Cost
Physical mass reduction of destroyed active ingredients.
|
18.5 tonnes/yr | 4.2 tonnes/yr | -77.3% |
|
Planner manual scheduling hours
Speed
Eliminated manual spreadsheet potency adjustment calculations.
|
22 hrs/week | 3 hrs/week | -86.4% |
|
Batch yield variance
Accuracy
Reduced need for mid-run formula rework due to exact API assay targeting.
|
6.4% | 1.1% | -82.8% |
|
On-time full batch release rate
Capacity
Prevented line stoppages caused by missing API re-test certificates.
|
81% | 95% | +14% |
The reading
The deployed pipeline replaced static ERP material allocation with real-time, potency-adjusted batch sizing. Raw material scrap dropped by 73%, saving millions in active ingredient purchases within six months of deployment. The quality control lab reduced re-testing backlogs, ensuring material was consumed while chemically viable.
Production planners transitioned from manually calculating API potency adjustments on spreadsheets to accepting automated batch recommendations directly inside SAP. The system established an auditable trail for SAHPRA regulatory compliance.
Learn to work like this
The AI Forward Deployed Engineer Program is twenty weeks of exactly this — discovery, honest evaluation, integration into systems you didn’t build, and automating the expensive work nobody put in the brief.
The €2.1M inspection bottleneck a model didn't fix
A supplier asked for a defect-detection model. Eighteen weeks later the model was the third most valuable thing delivered. This is how the engineer worked out what the problem actually was, what got automated, and what it earned.
Read the engagement →Reducing Customs Port Demurrage via Automated Document Reconciliation
A freight forwarder faced substantial port demurrage penalties caused by documentation discrepancies during Indian customs filing. An FDE deployed a document extraction and cross-reconciliation pipeline, cutting port clearance holds dramatically.
Read the engagement →