DEV Community

shohei-ai-lab
shohei-ai-lab

Posted on

5 Essential System Prompt Rules to Stop Your AI Agents from Going Rogue

We are in the era of autonomous AI agents. Tools like Claude Code, Cline, Cursor, and other agentic coding frameworks are replacing simple Q&A chat interfaces. They can read your workspace, run shell commands, write code, and execute tests.

However, with great power comes great chaos. Every developer using AI agents eventually faces these frustrating moments:

  • The Loop of Doom: Your agent gets stuck in a compilation-error loop, burning your API credits.
  • The Overzealous Refactor: The agent rewrites unrelated files, breaking your main branch.
  • The Hallucination: The agent claims "all tests passed" when they didn't even run.

These issues are not failures of the AI models themselves—they are system prompt failures. Just like human developers, AI agents need clear boundaries, explicit constraints, and rigorous execution rules to perform at their best.

Based on our ongoing experiment, AI Autonomous Revenue Project (where we build and iterate on web products using AI agent workflows with structural verification), we compiled 5 core rules for designing system prompts for AI agents.


Rule 1: Tighten the Role and Grant Specific Authority

Instead of a generic role like "You are a world-class software engineer," define a highly specialized domain expert with distinct behavioral boundaries.

  • Weak: "You are a senior full-stack developer."
  • Strong: "You are a strict, no-nonsense senior backend engineer specializing in Next.js, Prisma, and PostgreSQL. Your coding style is minimal, avoiding any unnecessary helper libraries or duplicate imports."

Rule 2: Explicitly Restrict File System Mutability

AI agents have powerful file-writing tools. If left unrestricted, they will modify configuration files, GitHub actions, or package dependencies in unexpected ways. You must establish write-protected zones.

### Write-Protected Files (Read-Only)
- `package.json` and `package-lock.json` (Any dependency update requires explicit human approval)
- `.github/workflows/` (Never modify CI pipelines)
- `tsconfig.json` and linter configurations
Enter fullscreen mode Exit fullscreen mode

Rule 3: Design for Non-Determinism (Stochastic Behavior)

Large Language Models are probabilistic. Never assume the agent's action will succeed on the first try. Your system prompt must define a fallback protocol.

  • Bad: "Fetch the API data and save it to the database."
  • Good: "When calling the external API, write error-handling logic with exponential backoff up to 3 retries. If the call still fails, write the complete error payload to logs/error.log and stop the task."

Rule 4: Mandate Automated Evidence Preservation (No Verbal Claims)

Agents are prone to hallucinating task completion. Do not allow them to just say "Everything is working!"

Force the agent to execute real tests and preserve the output files.

  • Require the agent to output shell execution logs, test suite status codes, and built artifact hashes to a designated folder (e.g., verification/results/).
  • The system prompt should state: "A task is only considered done when the corresponding test runner output file is generated and matches the expected schema."

Rule 5: Enforce Tool-specific Multi-Line Editing

When editing multiple non-contiguous lines of code, agents often break code files using simple single-line replacements. Instruct them to use multi-replace tools or structural parsers specifically designed for non-adjacent lines. This preserves file integrity and prevents syntax breakage.


Further Reading & Resources

We put these 5 rules into practice in our daily workflow. If you want to dive deeper into agent instruction design or jumpstart your agentic workflow with structurally verified prompts, here are some resources:

  • Free: AI Agent Config Starter Pack — A free starter set of agent configuration files, format-checked and ready for customization. Grab it on Gumroad.
  • Zenn Book (Japanese): AI Agent Instruction Design — A comprehensive guide covering instruction architecture for AI coding agents, with practical examples. Read it on Zenn.
  • The AI Coding Prompt Toolkit — A curated collection of 50+ system prompts and templates for coding, testing, and system design, structurally verified via automated format checks. Available on Gumroad.

We'll keep sharing what we learn as we iterate on autonomous agent workflows. Follow along and let us know what rules have worked for you!

Top comments (0)