The user agent is in the request header, so it’s known before any response is sent from YouTube.
I don’t know if that’s what they’re doing, because it’s not possible to know what their server code is doing, making it a far better place to hide sleazy code.