<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: AymaneWebDEV</title>
    <description>The latest articles on DEV Community by AymaneWebDEV (@aymanewebdev).</description>
    <link>https://dev.to/aymanewebdev</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F4123217%2F9b1020d9-d33f-4132-a16d-a0760b9c3e6c.png</url>
      <title>DEV Community: AymaneWebDEV</title>
      <link>https://dev.to/aymanewebdev</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/aymanewebdev"/>
    <language>en</language>
    <item>
      <title>How to Stop Cursor from Hallucinating: 5 Production Rules Every AI Engineer Needs</title>
      <dc:creator>AymaneWebDEV</dc:creator>
      <pubDate>Sun, 13 Sep 2026 13:41:37 +0000</pubDate>
      <link>https://dev.to/aymanewebdev/how-to-stop-cursor-from-hallucinating-5-production-rules-every-ai-engineer-needs-18o9</link>
      <guid>https://dev.to/aymanewebdev/how-to-stop-cursor-from-hallucinating-5-production-rules-every-ai-engineer-needs-18o9</guid>
      <description>&lt;p&gt;How to Stop Cursor from Hallucinating: 5 Production Rules Every AI Engineer Needs&lt;/p&gt;

&lt;p&gt;If you use Cursor, Claude Code, or GitHub Copilot on a non-trivial codebase, you've probably encountered the &lt;strong&gt;AI coding drift&lt;/strong&gt; problem.&lt;/p&gt;

&lt;p&gt;The model:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Invents methods that don't exist in your framework version.&lt;/li&gt;
&lt;li&gt;Couples database queries or third-party APIs directly inside HTTP route handlers.&lt;/li&gt;
&lt;li&gt;Generates loose types like &lt;code&gt;any&lt;/code&gt; or &lt;code&gt;object&lt;/code&gt; to bypass TypeScript or Pydantic errors.&lt;/li&gt;
&lt;li&gt;Writes brittle unit tests that mock everything without testing actual boundary failures.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;When building production systems, manually fixing AI-generated drift can quickly erase the productivity gains from AI-assisted development.&lt;/p&gt;

&lt;p&gt;The solution isn't simply "better conversational prompting."&lt;/p&gt;

&lt;p&gt;It's &lt;strong&gt;explicit engineering rules that constrain the coding agent before it writes the code.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Here are five rules you can add to your &lt;code&gt;.cursor/rules/&lt;/code&gt; directory.&lt;/p&gt;




&lt;h2&gt;
  
  
  1. Bounded Context &amp;amp; Layer Isolation
&lt;/h2&gt;

&lt;p&gt;Prevent the AI from mixing database access, business logic, and HTTP concerns:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight markdown"&gt;&lt;code&gt;&lt;span class="p"&gt;-&lt;/span&gt; Route handlers MUST only perform request validation and delegate to application services.
&lt;span class="p"&gt;-&lt;/span&gt; Domain logic MUST remain independent of database ORMs and external APIs.
&lt;span class="p"&gt;-&lt;/span&gt; External API clients and third-party SDKs MUST be encapsulated behind dedicated adapters.
&lt;span class="p"&gt;-&lt;/span&gt; Database queries MUST NOT be placed directly inside HTTP route handlers.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This gives the agent explicit boundaries between the presentation, application, domain, and infrastructure layers.&lt;/p&gt;

&lt;p&gt;Without these constraints, an AI agent will often choose the shortest path to a working implementation—even when that implementation creates unnecessary coupling.&lt;/p&gt;




&lt;h2&gt;
  
  
  2. Hermetic Unit Testing
&lt;/h2&gt;

&lt;p&gt;AI-generated tests can look comprehensive while providing very little protection.&lt;/p&gt;

&lt;p&gt;A common pattern is to mock almost every dependency and then assert that the mocked functions were called.&lt;/p&gt;

&lt;p&gt;The test passes, but the actual boundary failure was never tested.&lt;/p&gt;

&lt;p&gt;Give the agent explicit testing constraints:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight markdown"&gt;&lt;code&gt;&lt;span class="p"&gt;-&lt;/span&gt; Unit tests MUST be hermetic: no real network calls and no unintended external filesystem dependencies.
&lt;span class="p"&gt;-&lt;/span&gt; Tests MUST cover valid inputs, invalid inputs, and important boundary conditions.
&lt;span class="p"&gt;-&lt;/span&gt; Avoid mocking internal domain logic.
&lt;span class="p"&gt;-&lt;/span&gt; Mock external boundary adapters where appropriate.
&lt;span class="p"&gt;-&lt;/span&gt; Tests MUST verify observable behavior rather than implementation details.
&lt;span class="p"&gt;-&lt;/span&gt; Every bug fix SHOULD include a regression test when practical.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;For example, don't only test that an API call succeeds.&lt;/p&gt;

&lt;p&gt;Also test what happens when the external service:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Times out&lt;/li&gt;
&lt;li&gt;Returns malformed data&lt;/li&gt;
&lt;li&gt;Returns an unexpected status code&lt;/li&gt;
&lt;li&gt;Returns an empty response&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The goal isn't maximum mock coverage.&lt;/p&gt;

&lt;p&gt;It's meaningful behavioral coverage.&lt;/p&gt;




&lt;h2&gt;
  
  
  3. Fail-Fast Input Boundaries
&lt;/h2&gt;

&lt;p&gt;AI-generated applications often assume that incoming data is trustworthy.&lt;/p&gt;

&lt;p&gt;That's particularly dangerous at API boundaries.&lt;/p&gt;

&lt;p&gt;Tell the agent exactly how input should be handled:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight markdown"&gt;&lt;code&gt;&lt;span class="p"&gt;-&lt;/span&gt; All incoming payloads MUST be validated using strict schemas such as Pydantic or Zod.
&lt;span class="p"&gt;-&lt;/span&gt; Avoid implicit type coercion when strict validation is required.
&lt;span class="p"&gt;-&lt;/span&gt; Invalid input MUST be rejected at the application boundary.
&lt;span class="p"&gt;-&lt;/span&gt; Domain-specific failures MUST use typed exceptions rather than generic errors.
&lt;span class="p"&gt;-&lt;/span&gt; Do not pass unvalidated request dictionaries through application layers.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This creates a clear boundary:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;External input → Validation → Application logic → Domain logic&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Instead of allowing malformed data to travel through the entire application before something eventually fails.&lt;/p&gt;




&lt;h2&gt;
  
  
  4. Hallucination &amp;amp; Assumption Defense
&lt;/h2&gt;

&lt;p&gt;One of the most frustrating problems with AI coding assistants is confident guessing.&lt;/p&gt;

&lt;p&gt;A model may generate an import, method, parameter, or dependency that looks perfectly reasonable but doesn't actually exist in your installed version.&lt;/p&gt;

&lt;p&gt;Add rules that explicitly prohibit this behavior:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight markdown"&gt;&lt;code&gt;&lt;span class="p"&gt;-&lt;/span&gt; NEVER assume an API, method, parameter, or configuration option exists without verification.
&lt;span class="p"&gt;-&lt;/span&gt; Prefer APIs already used by the existing codebase when implementing new functionality.
&lt;span class="p"&gt;-&lt;/span&gt; Do not invent dependencies or speculative import paths.
&lt;span class="p"&gt;-&lt;/span&gt; Do not use deprecated APIs when a supported alternative exists.
&lt;span class="p"&gt;-&lt;/span&gt; If an implementation depends on an unverified assumption, explicitly identify the assumption before proceeding.
&lt;span class="p"&gt;-&lt;/span&gt; If the requested approach introduces architectural or security risks, explain the trade-off and propose a safer alternative.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The important principle is:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;The project's installed dependencies and existing code are the source of truth—not the model's memory.&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;This is particularly useful when working with rapidly changing frameworks and libraries.&lt;/p&gt;




&lt;h2&gt;
  
  
  5. Idempotent State Mutations
&lt;/h2&gt;

&lt;p&gt;AI-generated APIs can also overlook what happens when clients retry requests.&lt;/p&gt;

&lt;p&gt;Consider an endpoint that creates an order or processes a payment.&lt;/p&gt;

&lt;p&gt;If the client sends the request, experiences a timeout, and retries it, you don't want the server to process the operation twice.&lt;/p&gt;

&lt;p&gt;For state-changing operations, give the agent explicit constraints:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight markdown"&gt;&lt;code&gt;&lt;span class="p"&gt;-&lt;/span&gt; State-changing endpoints SHOULD support idempotency when duplicate requests could cause unintended side effects.
&lt;span class="p"&gt;-&lt;/span&gt; Financial, order, and payment operations MUST define an idempotency strategy.
&lt;span class="p"&gt;-&lt;/span&gt; Idempotency keys MUST be persisted and associated with the resulting operation.
&lt;span class="p"&gt;-&lt;/span&gt; Concurrent state transitions MUST use appropriate transactional or locking mechanisms.
&lt;span class="p"&gt;-&lt;/span&gt; Do not rely on application-level checks alone when atomic database guarantees are required.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The exact implementation will depend on your database and architecture, but the important thing is that the agent is forced to &lt;strong&gt;consider retry and concurrency behavior&lt;/strong&gt; instead of generating only the happy path.&lt;/p&gt;




&lt;h1&gt;
  
  
  Why These Rules Matter
&lt;/h1&gt;

&lt;p&gt;AI coding assistants are extremely good at generating code.&lt;/p&gt;

&lt;p&gt;But they don't automatically know:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Your architecture&lt;/li&gt;
&lt;li&gt;Your dependency versions&lt;/li&gt;
&lt;li&gt;Your domain boundaries&lt;/li&gt;
&lt;li&gt;Your testing philosophy&lt;/li&gt;
&lt;li&gt;Your security requirements&lt;/li&gt;
&lt;li&gt;Your tolerance for technical debt&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Without explicit constraints, the model tends to optimize for producing code that looks plausible and solves the immediate request.&lt;/p&gt;

&lt;p&gt;That's where coding drift begins.&lt;/p&gt;

&lt;p&gt;Compare:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;"Implement authentication."&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;with:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Use the existing authentication service.&lt;br&gt;
Do not access the database from route handlers.&lt;br&gt;
Validate all external input with the existing schema system.&lt;br&gt;
Do not introduce new dependencies.&lt;br&gt;
Add tests for expired tokens and invalid credentials.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;The second instruction gives the agent a much smaller—and more useful—solution space.&lt;/p&gt;




&lt;h1&gt;
  
  
  Start With Constraints, Then Generate Code
&lt;/h1&gt;

&lt;p&gt;You don't need an enormous system prompt containing every possible engineering rule.&lt;/p&gt;

&lt;p&gt;Start with the constraints that matter most to your project:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;Architecture boundaries&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Dependency verification&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Strict input and type validation&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Boundary-focused testing&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;State and concurrency safety&lt;/strong&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Then adapt them to your framework and codebase.&lt;/p&gt;

&lt;p&gt;The goal isn't to make your AI coding assistant less autonomous.&lt;/p&gt;

&lt;p&gt;It's to make its autonomy &lt;strong&gt;bounded by engineering constraints&lt;/strong&gt;.&lt;/p&gt;




&lt;h1&gt;
  
  
  Open-Source Developer Prompt Vault
&lt;/h1&gt;

&lt;p&gt;I've collected these types of rules, along with additional prompts for architecture, refactoring, testing, security, and development workflows, in an open-source repository:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Developer Prompt Vault:&lt;/strong&gt;&lt;br&gt;
&lt;a href="https://github.com/AymaneWebDEV/developer-prompt-vault" rel="noopener noreferrer"&gt;https://github.com/AymaneWebDEV/developer-prompt-vault&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The repository contains reusable Markdown rules and templates that you can adapt to your own AI-assisted development workflow.&lt;/p&gt;

&lt;p&gt;If you're building AI-powered applications with FastAPI, I've also put together a separate starter architecture covering authentication, streaming APIs, rate limiting, Docker, and automated testing:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;FastAPI AI Agent Starter Kit:&lt;/strong&gt;&lt;br&gt;
&lt;a href="https://nexusbuilds.gumroad.com/l/fastapi-ai-starter-kit" rel="noopener noreferrer"&gt;https://nexusbuilds.gumroad.com/l/fastapi-ai-starter-kit&lt;/a&gt;&lt;/p&gt;




&lt;h2&gt;
  
  
  What Rules Have Helped You?
&lt;/h2&gt;

&lt;p&gt;What constraints have had the biggest impact on your Cursor, Claude Code, or Copilot workflows?&lt;/p&gt;

&lt;p&gt;I'd especially be interested in rules around &lt;strong&gt;architecture, testing, dependency verification, and preventing AI-generated technical debt&lt;/strong&gt;.&lt;/p&gt;

</description>
      <category>cursor</category>
      <category>ai</category>
      <category>programming</category>
      <category>webdev</category>
    </item>
  </channel>
</rss>
