Packet size optimization, with the purpose of minimizing the wireless packet transmission energy consumption, is crucial for the energy efficiency of the Internet of Things nodes. Meanwhile, energy scavenging from ambient energy sources has gained a significant attraction to avoid battery issues as the number of nodes increasingly grows. Packet size optimization algorithms have so far been proposed for battery-powered networks that have limited total energy with continuous power availability to prolong their lifetime. On the other hand, batteryless networks based on energy harvesting offer unlimited total energy with the interruption in availability. This is due to changing ambient conditions or the required time for harvesting and storing in small capacitors. Packet size optimization of batteryless networks has not been addressed so far. In this paper, an AI-based packet size optimization algorithm is proposed for batteryless networks that consider the amount of harvested energy at each node. Therefore, packet size is optimized dynamically for each round of data transmission. The proposed method is then evaluated via numerical simulations for a heterogenous wireless body area sensor network as a case study, considering 1-hop, cooperative, and 2-hop communication networks. Cooperative topology yields optimum energy efficiency for highly dynamic sensors, such as ECG, while 2-hop has shown to be optimum for the same type of sensors in battery-powered networks. Also, for sensors with slower dynamics such as body temperature, 1-hop turns out to be optimum in networks solely dependent on energy scavenging while cooperative topology is optimum for battery-powered networks. The algorithm applies to any heterogeneous fully batteryless networks to dynamically optimize packet size at each transmission instance.