{"slug": "how-to-keep-ai-coding-agents-from-hallucinating-a-guide-to-harness-engineering", "title": "How to Keep AI Coding Agents from Hallucinating: A Guide to Harness Engineering", "summary": "Developer Masih Moafi introduced a technique called Harness Engineering to prevent AI coding agents from hallucinating and losing focus. The approach uses structured, repository-local control layers with Markdown-based rules to guide agents, reducing context window pollution and scope drift. Moafi released a public repository of battle-tested configuration templates and demonstrated the method by building a machine learning research project autonomously.", "body_md": "AI coding agents (like Claude Code, Devin, or open-source equivalents like OpenClaw) are incredibly powerful. They can navigate directories, write tests, refactor modules, and submit PRs.\n\nYet, if you drop them into a raw repository without boundaries, they suffer from **context window pollution**, **agent amnesia**, and **scope drift**. A simple bug-fix refactor can trigger a 6-hour loop where the agent rewires half the project, deletes unrelated tests, and gets stuck in \"process theater.\"\n\nTo fix this, we need **Harness Engineering**.\n\nAn **Agent Harness** is a structured, repository-local control layer designed to guide and verify the agent's work. Instead of feeding your LLM a monolithic prompt, you embed a lightweight system of record and physical feedback loops directly inside the workspace.\n\nI have packaged the exact, battle-tested Markdown-based context rules I use to steer and constraint my local agents into a public repository: ** MasihMoafi/harnesses-I-use**.\n\nRather than complex code, this repo shares raw configuration rule sheets:\n\n`AGENTS.md`\n\n`CODEX_CODING_GUIDELINES.md`\n\n`TERMINAL_AND_GIT_RULES.md`\n\n`git add -A`\n\n), and change safety (using Ubuntu `pkexec`\n\nfor root commands instead of raw CLI password prompts).`SESSION_HANDOFF_RULES.md`\n\n`ARTIFACT_RULES.md`\n\n`abbn.md`\n\n`ctu`\n\n= continue, `fmy`\n\n= familiarize, `ver`\n\n= verify) to save token count and maintain short, high-efficiency communication.This harness approach is heavily inspired by **Andrej Karpathy's** open-source education repos (like ** micrograd** and\n\nKarpathy’s projects are celebrated because they strip away bloat. They focus on clear, reproducible mathematical baselines and avoid over-engineering.\n\nWe applied that same philosophy to agent-driven code generation. The core rules of our harness require:\n\nTo test this, I used this exact harness to build a comparative machine learning research project: **Sensor Fault Diagnosis**.\n\nThe agent was given a realistic synthetic sensor dataset and tasked with:\n\nBy restricting the agent to a single control surface (a structured manual) and enforcing strict keep/discard criteria, the agent completed the pipeline and wrote the final report **entirely autonomously**.\n\nWithout a harness, the agent would have bloated the repository with decorative dashboard scripts or fake performance metrics. The harness kept it grounded.\n\nIf you are building code with AI agents, stop writing 2,000-word system prompts. Start building repository harnesses.\n\nCheck out the templates and configurations:\n\n👉 [MasihMoafi/harnesses-I-use](https://github.com/MasihMoafi/harnesses-I-use)\n\nFor more of my work, experiments, and research, check out my website:\n\n👉 [masihmoafi.tech](https://masihmoafi.tech)", "url": "https://wpnews.pro/news/how-to-keep-ai-coding-agents-from-hallucinating-a-guide-to-harness-engineering", "canonical_source": "https://dev.to/masihmoafi/how-to-keep-ai-coding-agents-from-hallucinating-a-guide-to-harness-engineering-12mm", "published_at": "2026-06-14 10:05:48+00:00", "updated_at": "2026-06-14 10:40:56.502942+00:00", "lang": "en", "topics": ["artificial-intelligence", "large-language-models", "ai-agents", "developer-tools", "machine-learning"], "entities": ["Masih Moafi", "Claude Code", "Devin", "OpenClaw", "Andrej Karpathy", "micrograd", "Sensor Fault Diagnosis", "MasihMoafi/harnesses-I-use"], "alternates": {"html": "https://wpnews.pro/news/how-to-keep-ai-coding-agents-from-hallucinating-a-guide-to-harness-engineering", "markdown": "https://wpnews.pro/news/how-to-keep-ai-coding-agents-from-hallucinating-a-guide-to-harness-engineering.md", "text": "https://wpnews.pro/news/how-to-keep-ai-coding-agents-from-hallucinating-a-guide-to-harness-engineering.txt", "jsonld": "https://wpnews.pro/news/how-to-keep-ai-coding-agents-from-hallucinating-a-guide-to-harness-engineering.jsonld"}}