I can't speak to the "by law" part. But many universities, including two that I have experience with, consider the PhD stipend to be compensation for a part time job (RA/TA), accounting for 20 hours of the student's time, with the idea that the remaining 20 hours are to be devoted to taking classes. And during the summer, when a student may not be taking classes, a student may receive a full 40 hours' worth of compensation.
I work at an elite tier 1 research university that is an international household name. My spouse is a tenured faculty member who runs a lab and several concurrent DoD funded grants.
I understand perfectly well what a phd candidate does and what they are compensated for as well as what their fully loaded costs are.
If you believe that the cost of a phd student is 18k a year, it is you who does not understand what working with a phd student involves. In fact, 18k probably does not even cover their health care costs. We can debate about what a phd candidate "should" be compensated for and if their current levels of compensation are fair but any argument needs to include the full picture.
This is a silly take. The "true" cost of an employee is _always_ higher than their salary. Often by 50-100%.
Your argument here is like saying the minimum wage needn't be increased because the "true" cost of an employee is already $15/hr. They should be grateful for those administrative costs! And doubly so that we don't tax them!
Time spent performing thesis research is accounted as course credit hours (There's typically an 800 or 900 level course for exactly this.) After all, you must be "taking classes" every semester, with very limited exceptions, in order to even be enrolled in the program.
Moreover, in my field, RA work for an advisor should ideally coincide with your thesis work. So in that case, the main thing a PhD student does is two-for-one RA+thesis work. If it doesn't coincide, that's not a great situation to be in.
And I do understand what getting a PhD involves, as I have one.