惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

The GitHub Blog
The GitHub Blog
Engineering at Meta
Engineering at Meta
博客园 - 聂微东
博客园 - Franky
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
雷峰网
雷峰网
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
L
LangChain Blog
WordPress大学
WordPress大学
H
Help Net Security
H
Hackread – Cybersecurity News, Data Breaches, AI and More
Y
Y Combinator Blog
Blog — PlanetScale
Blog — PlanetScale
MyScale Blog
MyScale Blog
IT之家
IT之家
酷 壳 – CoolShell
酷 壳 – CoolShell
罗磊的独立博客
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
有赞技术团队
有赞技术团队
Apple Machine Learning Research
Apple Machine Learning Research
云风的 BLOG
云风的 BLOG
博客园 - 【当耐特】
P
Proofpoint News Feed
D
DataBreaches.Net

DEV Community

Authentication Security Deep Dive: From Brute Force to Salted Hashing (With Java Examples) Why AI Systems Don’t Fail — They Drift Spilling beans for how i learn for exam😁"Reinforcement Learning Cheat Sheet" I Replaced Chrome with Safari for AI Browser Automation. Here's What Broke (and What Finally Worked) How Python Borrows Other People's Work The $40 Architecture: Processing 1 Billion API Requests with 99.99% Uptime Vibe Coding: A Workflow Guide (From Zero to SaaS) Most webhook security guides protect the wrong side. The scary part is delivery. Headless CMS for TanStack Start: Build a Blog with Cosmic EU Age Verification App "Hacked in 2 Minutes" — What Actually Happened Comfy Cloud’s delete function does not actually remove files Running AI Models on GPU Cloud Servers: A Beginner Guide Event-driven media intelligence with AWS Step Functions and Bedrock I scored 500 AI prompts across 8 quality dimensions — here's what broke How to Call Google Gemini API from Next.js (Free Tier, No Backend Needed) The Portal Protocol: Reclaiming Human Connection in the Age of AI How to Fix Your Team's Scattered Knowledge Problem With a Self-Hosted Forum Intro to tc Cloud Functors: A Graph-First Mental Model for the Modern Cloud Designing Multi-Tenant Backends With Both Ownership and Team Access I Built a Neumorphic CSS Library with 77+ Components — Here's What I Learned PostgreSQL Performance Optimization: Why Connection Pooling Is Critical at Scale Cómo construí un SaaS multi-rubro para gestionar expensas en Argentina con FastAPI + Vue 3 🚀 I Built an Ethical Hacking Scanner Tool – Open Source Project I Replaced /usage and /context in Claude Code With a Single Statusline A Pythonic Way to Handle Emails (IMAP/SMTP) with Auto-Discovery and AI-Ready Design I Collected 8.9 Million Polymarket Price Points — Here's What I Found About How Markets Really Move EcoTrack AI — Carbon Footprint Tracker & Dashboard Everyone's Using AI. No One Agrees How. 5 self-hosted ebook managers worth trying in 2026 Building Your First AI Agent with LangChain: From Chatbot to Autonomous Assistant
How to Keep AI Coding Agents from Hallucinating: A Guide ...
Masih Maafi · 2026-06-14 · via DEV Community

Masih Maafi

How to Keep AI Coding Agents from Hallucinating: A Guide to Harness Engineering

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.

Yet, 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."

To fix this, we need Harness Engineering.

What is Harness Engineering?

An 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.

I 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.

Rather than complex code, this repo shares raw configuration rule sheets:

  • AGENTS.md: The global router and primary entry point. Every AI agent reads this first to understand the Priority Kernel (reply constraints, English defaults, local-first behavior).
  • CODEX_CODING_GUIDELINES.md: Surgical coding rules adapted from Andrej Karpathy's coding practices. Enforces minimal changes, style-matching, and goal-driven execution.
  • TERMINAL_AND_GIT_RULES.md: Safe terminal navigation, SOCKS/HTTP proxy configuration, explicit git scoping (e.g., never using git add -A), and change safety (using Ubuntu pkexec for root commands instead of raw CLI password prompts).
  • SESSION_HANDOFF_RULES.md: Guidelines for context awareness and continuity. Defines how state should be captured and transferred between sessions.
  • ARTIFACT_RULES.md: Rules for generating verified visual and math outputs, including local HTML layout verification, MathML for formula rendering, and PDF inspection.
  • abbn.md: A dictionary of standard abbreviations (e.g., ctu = continue, fmy = familiarize, ver = verify) to save token count and maintain short, high-efficiency communication.

The Karpathy Connection: Simplicity & Rigor

This harness approach is heavily inspired by Andrej Karpathy's open-source education repos (like micrograd and makemore).

Karpathy’s projects are celebrated because they strip away bloat. They focus on clear, reproducible mathematical baselines and avoid over-engineering.

We applied that same philosophy to agent-driven code generation. The core rules of our harness require:

  1. Surgical Changes: Touch only the exact lines requested. No drive-by style refactoring of adjacent code.
  2. Verifiable Outcomes: Before the agent claims a feature is complete, it must run a local script to verify the baseline, compile the code, and attach raw terminal output logs to its session record as physical evidence of success.
  3. Harness Simplification: Every rule is considered overhead. As models grow smarter, we run tests to prune redundant rules.

Case Study: Autonomously Generating ML Research Reports

To test this, I used this exact harness to build a comparative machine learning research project: Sensor Fault Diagnosis.

The agent was given a realistic synthetic sensor dataset and tasked with:

  1. Training a Bayesian classifier (Gaussian Naive Bayes) and mapping conditional probabilities.
  2. Building and training a PyTorch MLP neural network.
  3. Calculating and comparing multiple losses—including standard Binary Cross-Entropy (BCE), micrograd-style Max-Margin (Hinge) loss, and makemore-style Cross-Entropy loss.
  4. Exporting ROC curves, decision boundaries, and generating a publication-grade LaTeX/HTML research paper.

By 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.

Without a harness, the agent would have bloated the repository with decorative dashboard scripts or fake performance metrics. The harness kept it grounded.


Get the Harness

If you are building code with AI agents, stop writing 2,000-word system prompts. Start building repository harnesses.

Check out the templates and configurations:
👉 MasihMoafi/harnesses-I-use

For more of my work, experiments, and research, check out my website:
👉 masihmoafi.tech