Because the problem returns every 20–30 minutes and restarting temporarily restores normal operation, I would troubleshoot it in this order.
First, go to > System Settings > General > Software Update and install any available macOS update.
Next, test the Mac in Safe Mode. Shut down your M4 MacBook Air, press and hold the power/Touch ID button until Loading startup options appears, select your startup disk, hold Shift, and choose Continue in Safe Mode. Use the Mac long enough to see whether the problem returns.
If the problem still occurs, disconnect all external devices and accessories, including USB devices, hubs, drives, printers, and external displays.
If the problem stops, reconnect the devices one at a time and test the Mac after each connection.
Apple recommends this approach for isolating problems caused by connected devices. (Apple Support)
If disconnecting the accessories does not resolve the problem, run Apple Diagnostics with the accessories still disconnected.
Shut down the Mac, press and hold the power button until Options appears, release it, then hold Command (⌘)-D. Follow the instructions and save any reference code that appears. (support.apple.com)
When the problem occurs, note whether the keyboard, clicking, Dock, menu bar, trackpad movement, or trackpad gestures are also affected.
This information can help Apple diagnose the problem.
If the problem continues or Apple Diagnostics reports an issue, have Apple or an Apple Authorized Service Provider inspect the Mac.
Have the computer evaluated by the Professionals who have the Special Hardware Software and Expertise to diagnose the issue and offer possible solutions
Make an Appointment at the Apple Genius Bar or call 1 800 MyApple and have them evaluate the computer
Other location Call Centres
Contact Apple for support and service
You can check your coverage under > System Settings > General > AppleCare & Warranty. (support.apple.com)
I would not erase the Mac or reinstall macOS yet. Complete these diagnostic steps first.