Coke formation occurs primarily through the thermal decomposition of hydrocarbons, a process known as coking. This can happen via several mechanisms, including polymerization, condensation, and dehydrogenation reactions. Conditions such as high temperatures, high pressure, and the presence of metallic species can accelerate coke formation.