Rendering latex equations from the api response

I am building a chatbot which often needs to proccess latex language. I have a component that runs regex on the messages and identifies the latex and uses katex to render them out.

The problem I am running into is that i have pretty much no control, whatever I do with the system prompt, in how the response is generated. Sometimes chatgpt uses two \ sometimes it uses one and sometimes it uses four. I need it to always use four backslashes because \ being an escape character in itself.

I tried multiple system prompts but it is never consistent in its responses.

My current system prompt is:
If your response contains math terms, you should format your response in the proper latex delimiters. For inline equations you should use \\\\( and \\\\) for inline math equations and \\\\[ and \\\\] for block equations. It is important that you use four backslashes whenever you include a backslash, because of the way your response will be proccessed

but I tried using pretty much all combinations of wording and examples to get it to be consistent.

Did anyone have the same problem and came up with a solution?

have you found any solution?
I have had the same problem