Many bacteria, such as Clostridium freundii and Klebsiella pneumoniae, are know to produce vitamin B12. Studies have demonstrated that vitamin B12 is produced in tempeh when these bacteria are allowed to grow during fermentation [1]. These bacteria can enter the tempeh as a result of contamination, which means that the process is not under control. On the other hand, they cannot be deliberately added as they are not considered food-grade for humans.
Vitamin B12 results of tempeh
Only few vitamin B12 values of tempeh can be found in scientific literature. Areekul found between 0.18 and 4.1 mcg vitamin B12 analogues per 100 g tempeh he bought from various markets in Jakarta, Indonesia [2]. A Dutch study detected no vitamin B12 [2] whereas a US study showed only little vitamin B12 analogues (0.05 mcg per 100g) [3]. The USDA National Nutrient Database for Standard Reference (2010) mentions a value of 0.08 mcg/100 g fresh tempeh, which is very low, considering the fact that you need about 3 mcg daily. In conclusion, vegans should not rely on tempeh as their source of vitamin B12 and should consume vitamin B12 supplements.
https://www.tempeh.info/health/vitaminB12.php
I remember reading some cultures got their B12 by fecal contamination of crops.