Objectives: To propose a novel AI-based quantum key distribution optimization model to detect abnormal sensor readings, communication pattern between nodes, and intrusions during data transformation in long-range wireless sensor networks (LoRA-WSNs). In order to optimize the QKD in WSNs, machine learning boosting techniques are employed to minimize data loss and maximize data integrity. Methods: The CatBoost machine learning-based gradient boosting algorithm (CatBoost-MLGBA) is employed for QKD optimization and to detect abnormal node communications and patterns during data transfer by training historical network data. The linear regression method (LRM) with key generation rates is used to predict network attacks, which helps optimize the QKD more effectively. Lasso Regularization (L1R) is utilized to spot and recover the data in networks, and Deep Q-Networks (DQN-WSN) combined with the shortest path method is used to find alternate routing for the finest node search and data transfer. The WSN-DS historical dataset is utilized to train the CatBoost-MLGBA model to detect anomalies effectively. The OMNET++ tool is used to assess the performance of the proposed CatBoost-MLGBA model by comparing it with prevailing protocols such as ReLeC-WSN, RTM-ANN, and DL-IDSWSN. Findings: The new AI based optimization model, CatBoost-MLGBA outperforms the existing protocols in preventing data loss by enhancing security features. The proven results show that the data loss is minimized to 10%, with a 9% energy consumption rate, 95% network lifetime, 97% PDR rate, 91% robustness to anomaly attacks, and 6 seconds data transmission speed rate. Novelty: The CatBoost-MLGBA model has the ability to enhance security features and prevent data loss during data transfer in LoRa-WSNs. The new method effectively optimizes the key distribution for secured data transmission and improves the packet delivery ratio. The challenges of the prevailing security protocols, such as ReLeC-WSN, RTM-ANN, and DL-IDSWSN, are addressed. https://www.indjst.org/