Ghee becomes grainy due to the formation of large lactose crystals during slow cooling. Prevent this by cooling ghee rapidly or allowing it to cool undisturbed.
The grainy texture in ghee is primarily caused by the crystallization of lactose. Ghee, when properly made, should be almost entirely free of lactose, but trace amounts can remain. When ghee cools slowly, these lactose molecules have ample time to bind together and form larger crystals, resulting in a gritty or grainy texture. The rate of cooling is the most significant factor. Slow cooling promotes larger crystal formation, while rapid cooling inhibits it. Another contributing factor is disturbance during the cooling process. Agitating or stirring the ghee while it's cooling can also encourage crystal formation.
To prevent grainy ghee, after simmering and clarifying, pour the hot ghee into a wide, shallow container to increase the surface area for faster cooling. You can even place the container in an ice bath (being careful not to get water in the ghee) to accelerate the cooling process. Avoid stirring or disturbing the ghee while it cools.