Too many open files

On macOS, I’m getting the error in the log “too many open files.” I can temporarily solve this with sudo launchctl limit maxfiles unlimited unlimited but I wanted to know if there was a way to prevent this from happening in the first place.