Self correcting code agent for IaS

I would like to ask for the opinion about whether using RAG or assistant api is a better solution if I want to make a self correcting code agent for infrastructure as code

I planned to use some state file as knowledge base for the AI. I have tried to use Completions API to build a self correcting code agent but seems the output not really good if I want to based on the some existing infra

I am really new to LLM, may I ask about whether it is possible and any suggestion?

Thank you