The Architect in the Machine: Inside AutoCAD-Agent-CodeGeneration

How an experimental LangGraph workflow stops LLMs from hallucinating coordinates by forcing them to use a custom geometry API.

7 min read • View on GitHub • More from JainilPatel2502

A mechanical robot arm attempting to draw a precise blueprint on a drafting table, but the ink lines warp into impossible geometric shapes like an optical illusion. This represents the spatial hallucinations LLMs suffer when asked to calculate raw CAD coordinates.
LLMs are language engines, not geometry engines. Asking them to calculate raw coordinate arrays usually results in chaotic, impossible structures.
Key Takeaways

The Spatial Hallucination Problem

Large Language Models are notoriously terrible at calculating raw geometric coordinates. If you ask an AI to draw a floor plan using raw pyautocad commands, it will hallucinate overlapping lines and impossible geometries. They are language engines, not geometry engines. Asking an LLM to calculate the four corners of a polyline wall based on a center coordinate and a thickness is a recipe for disaster.

The "Library as a Prompt" Pattern

To solve this, AutoCAD-Agent-CodeGeneration uses a clever abstraction. Instead of asking the LLM to do the math, it provides the LLM with a custom, high-level Python library representing architectural primitives like walls and doors. The system prompt feeds the LLM a mini-documentation of custom classes from the /core directory. The LLM acts as the architect, describing the intent via simple Python scripts, while the underlying Python classes handle the unforgiving coordinate math.

# Raw pyautocad approach (Prone to LLM hallucination)
points = array('d', [0.0, 0.0, 5000.0, 0.0, 5000.0, 4000.0, 0.0, 4000.0, 0.0, 0.0])
model.AddLightWeightPolyline(points)

# Agentic Library approach (Safe and delegated)
room = Room(width=5000, length=4000)
room.draw()

The Three-Node Drafting Firm

The architecture relies on a LangGraph directed acyclic graph. It mimics a real-world drafting firm. The Planner node translates messy user text into strict architectural requirements. The Coder node writes the Python script using the custom API. Finally, the Executer node runs the script via a subprocess and captures the standard output for debugging.

The Geometry Engine and the Wall-Cutting Problem

A close-up top-down view of a thick black line being surgically sliced by a scalpel, while tweezers lower a tiny wooden door block into the gap. This illustrates the complex programmatic logic required to insert a door into an existing wall object.
Inserting a door requires deleting the existing wall object, recalculating vectors, drawing two new wall segments, and finally inserting the door block.

The hardest part of CAD automation is handling junctions and insertions. The logic in door.py reveals the complexity. When inserting a door, the system cannot simply place a block over a line. It must calculate the insertion point, delete the existing wall object, recalculate the vectors, draw two entirely new wall segments to create the gap, and then insert the door block.

Beyond the Chatbot Plugin

Simple CAD chatbot plugins map text directly to single commands. This project takes a fundamentally different agentic approach. It uses a Singleton COM connection to maintain persistent state and a multi-agent workflow to verify logic before execution.

AutoCAD Chatbot V1.0 is here! I'm excited to share the initial version of a project I've been developing: An AutoCAD Chatbot Plugin that uses the Gemini API to convert natural language commands directly into drawings.

Ankan Maity, Developer · AutoCAD Chatbot V1.0
FeatureStandard CAD ChatbotAutoCAD-Agent-CodeGeneration
LLM OutputRaw coordinates & commandsHigh-level Python scripts
Geometry CalculationHallucinated by LLMCalculated natively in Python
Error HandlingFails silentlyCaptures subprocess errors and retries