Sometimes keyboard and trackpad do not respond immediately from sleep, it may take few seconds but not few minutes.
Please uncheck if "Open Bluetooth Setup Assistant at startup if no keyboard is detected" is marked check.
- Choose Apple menu > System Preferences, then click Bluetooth.
- Click Advanced
- Uncheck "Open Bluetooth… … if no keyboard is detected".
Please check if you have same issue in safe mode.
https://support.apple.com/en-us/HT201262
Note: Safe mode prevents your Mac from loading certain software as it starts up, including login items, system extensions not required by macOS, and fonts not installed by macOS. It also does a basic check of your startup disk, similar to using First Aid in Disk Utility. And it deletes some system caches, including font caches and the kernel cache, which are automatically created again as needed.
Try reset the SMC of your Mac :
https://support.apple.com/en-us/HT201295
And perform Apple Diagnostics Test if you still have same issue:
https://support.apple.com/en-us/HT202731
Further you can Contact Apple Support for service / repair.