It's decided by PHP at some point early in the request. What you really care about, is whether MODX thinks it's in CLI mode.
PHP is NOT in cli mode MODX is NOT in cli mode