Why/How do Cyanobacteria Produce Toxins?
This available paper has some ideas,
https://www.ncbi.nlm.nih.gov/pmc/articles/PMC3736421/
Search terms cyanotoxin and evolutionary give at least two more promising papers.

https://www.biorxiv.org/content/10.1101/615542v1.full
says:
i) photoprotection due to absorption of harmful ultra-violet radiation; (ii) facilitating the acquisition or more efficient use of available but limiting resources; (iii) impeding growth or resource use of competitors such as green algae or other cyanobacteria; and (iv) limiting predation by inducing toxicity in organisms predating on cyanobacteria. While the role of cyanobacterial metabolites in photoprotection, resource monopolization, and habitat dominance has been demonstrated experimentally, hypotheses regarding the anti-predation role are not entirely consistent