Abstract
Infrastructure-as-Code (IaC) generation poses significant challenges for Large Language Models (LLMs) due to strict syntactic requirements, complex resource dependencies, and organizational security policies. We present MACOG (Multi-Agent Code-Orchestrated Generation), a framework that treats IaC generation as a collaborative, multi-agent engineering process. By integrating specialized agents, constrained decoding, and a counterexample-guided repair loop, MACOG significantly improves deployment success rates and policy compliance. Evaluations on the IaC-Eval benchmark demonstrate that MACOG enables frontier models to achieve substantial performance gains over traditional retrieval-augmented generation methods.
Introduction
Writing reliable Infrastructure-as-Code (IaC), such as Terraform, is notoriously difficult for LLMs. Unlike general programming, IaC requires managing a complex graph of interdependent resources (like VPCs, subnets, and security groups) that must adhere to strict provider schemas and security policies. Current LLMs often fail in three primary ways: syntactic hallucinations (using non-existent fields), policy violations (creating insecure configurations), and a lack of global context regarding resource dependencies.
MACOG addresses these issues by replacing "one-shot" generation with a structured, tool-backed multi-agent workflow. It transforms natural language prompts into production-ready code through a rigorous repair loop and specialized role-playing.
Methods
MACOG orchestrates a team of specialized agents to transform natural language prompts into production-ready code. The architecture utilizes a central "Blackboard" for state management and deterministic orchestration of the workflow.
Key components of the framework include:
- Specialized Multi-Agent Team: The task is split among roles including an Architect (planning), Engineer (coding), Security Prover (checking policies via Open Policy Agent), and DevOps (testing in a sandbox).
- Infrastructure Intermediate Representation (I-IR): Instead of passing raw code snippets, agents communicate via a typed graph format, ensuring a consistent "source of truth."
- Constrained Decoding: The system uses a grammar-aware decoder that physically prevents the LLM from generating tokens that violate Terraform (HCL) syntax or provider schemas.
- Counterexample-Guided Repair Loop: If a validator fails, the system generates a structured "counterexample" (error log) that a specialized agent uses to perform a surgical, minimal repair rather than a full rewrite.
Evaluation and Results
The researchers evaluated MACOG using IaC-Eval, a benchmark consisting of 458 cloud infrastructure specification tasks ranging from simple networking to complex multi-service deployments. MACOG demonstrated significant performance gains across a variety of models:
- Frontier Models: Using MACOG, GPT-5 (as labeled in the study) improved its success score from 54.90 to 74.02. Gemini-2.5 Pro improved from 43.56 to 60.13.
- Smaller Models: Mid-tier models like WizardCoder-33B also saw improvements, rising from 11.70 to 15.80.
- Ablation Impact: Removing the DevOps sandbox (which provides real-world deployment feedback) caused the biggest performance hit, dropping the success rate by over 17 points. This confirms that "offline" validation is insufficient for ensuring true deployment readiness.
Key Insights
Analysis of the framework's performance yielded several critical insights:
- Structure over Reasoning: Surprisingly, Chain-of-Thought (CoT) prompting often performed worse than simple few-shot prompting. For declarative code like IaC, strict adherence to schemas is more valuable than free-form natural language reasoning.
- Surgical Repairs: Most LLM repair attempts introduce new bugs by rewriting large chunks of code. MACOG’s use of structured error logs allows for minimal edits that preserve working parts of the configuration.
- The Practical Triad: Success depends on the combination of constrained decoding (syntax), security proving (policy), and runtime feedback (deployment).
Conclusion and Future Work
While MACOG introduces higher latency and computational cost due to its iterative nature, it provides a robust path toward automated, reliable cloud infrastructure deployment. Future work will focus on expanding provider support beyond Terraform to languages like Pulumi or AWS CDK, and integrating human-in-the-loop checkpoints for high-consequence production environments.
Summary updated