
How to Stop Cursor from Hallucinating: 5 Production Rules Every AI Engineer Needs If you use Cursor,...
How to Stop Cursor from Hallucinating: 5 Production Rules Every AI Engineer Needs
If you use Cursor, Claude Code, or GitHub Copilot on a non-trivial codebase, you've probably encountered the AI coding drift problem.
The model:
any or object to bypass TypeScript or Pydantic errors.When building production systems, manually fixing AI-generated drift can quickly erase the productivity gains from AI-assisted development.
The solution isn't simply "better conversational prompting."
It's explicit engineering rules that constrain the coding agent before it writes the code.
Here are five rules you can add to your .cursor/rules/ directory.
Prevent the AI from mixing database access, business logic, and HTTP concerns:
- Route handlers MUST only perform request validation and delegate to application services.
- Domain logic MUST remain independent of database ORMs and external APIs.
- External API clients and third-party SDKs MUST be encapsulated behind dedicated adapters.
- Database queries MUST NOT be placed directly inside HTTP route handlers.
This gives the agent explicit boundaries between the presentation, application, domain, and infrastructure layers.
Without these constraints, an AI agent will often choose the shortest path to a working implementation—even when that implementation creates unnecessary coupling.
AI-generated tests can look comprehensive while providing very little protection.
A common pattern is to mock almost every dependency and then assert that the mocked functions were called.
The test passes, but the actual boundary failure was never tested.
Give the agent explicit testing constraints:
- Unit tests MUST be hermetic: no real network calls and no unintended external filesystem dependencies.
- Tests MUST cover valid inputs, invalid inputs, and important boundary conditions.
- Avoid mocking internal domain logic.
- Mock external boundary adapters where appropriate.
- Tests MUST verify observable behavior rather than implementation details.
- Every bug fix SHOULD include a regression test when practical.
For example, don't only test that an API call succeeds.
Also test what happens when the external service:
The goal isn't maximum mock coverage.
It's meaningful behavioral coverage.
AI-generated applications often assume that incoming data is trustworthy.
That's particularly dangerous at API boundaries.
Tell the agent exactly how input should be handled:
- All incoming payloads MUST be validated using strict schemas such as Pydantic or Zod.
- Avoid implicit type coercion when strict validation is required.
- Invalid input MUST be rejected at the application boundary.
- Domain-specific failures MUST use typed exceptions rather than generic errors.
- Do not pass unvalidated request dictionaries through application layers.
This creates a clear boundary:
External input → Validation → Application logic → Domain logic
Instead of allowing malformed data to travel through the entire application before something eventually fails.
One of the most frustrating problems with AI coding assistants is confident guessing.
A model may generate an import, method, parameter, or dependency that looks perfectly reasonable but doesn't actually exist in your installed version.
Add rules that explicitly prohibit this behavior:
- NEVER assume an API, method, parameter, or configuration option exists without verification.
- Prefer APIs already used by the existing codebase when implementing new functionality.
- Do not invent dependencies or speculative import paths.
- Do not use deprecated APIs when a supported alternative exists.
- If an implementation depends on an unverified assumption, explicitly identify the assumption before proceeding.
- If the requested approach introduces architectural or security risks, explain the trade-off and propose a safer alternative.
The important principle is:
The project's installed dependencies and existing code are the source of truth—not the model's memory.
This is particularly useful when working with rapidly changing frameworks and libraries.
AI-generated APIs can also overlook what happens when clients retry requests.
Consider an endpoint that creates an order or processes a payment.
If the client sends the request, experiences a timeout, and retries it, you don't want the server to process the operation twice.
For state-changing operations, give the agent explicit constraints:
- State-changing endpoints SHOULD support idempotency when duplicate requests could cause unintended side effects.
- Financial, order, and payment operations MUST define an idempotency strategy.
- Idempotency keys MUST be persisted and associated with the resulting operation.
- Concurrent state transitions MUST use appropriate transactional or locking mechanisms.
- Do not rely on application-level checks alone when atomic database guarantees are required.
The exact implementation will depend on your database and architecture, but the important thing is that the agent is forced to consider retry and concurrency behavior instead of generating only the happy path.
AI coding assistants are extremely good at generating code.
But they don't automatically know:
Without explicit constraints, the model tends to optimize for producing code that looks plausible and solves the immediate request.
That's where coding drift begins.
Compare:
"Implement authentication."
with:
Use the existing authentication service. Do not access the database from route handlers. Validate all external input with the existing schema system. Do not introduce new dependencies. Add tests for expired tokens and invalid credentials.
The second instruction gives the agent a much smaller—and more useful—solution space.
You don't need an enormous system prompt containing every possible engineering rule.
Start with the constraints that matter most to your project:
Then adapt them to your framework and codebase.
The goal isn't to make your AI coding assistant less autonomous.
It's to make its autonomy bounded by engineering constraints.
I've collected these types of rules, along with additional prompts for architecture, refactoring, testing, security, and development workflows, in an open-source repository:
Developer Prompt Vault: https://github.com/AymaneWebDEV/developer-prompt-vault
The repository contains reusable Markdown rules and templates that you can adapt to your own AI-assisted development workflow.
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:
FastAPI AI Agent Starter Kit: https://nexusbuilds.gumroad.com/l/fastapi-ai-starter-kit
What constraints have had the biggest impact on your Cursor, Claude Code, or Copilot workflows?
I'd especially be interested in rules around architecture, testing, dependency verification, and preventing AI-generated technical debt.
csharpA weekly digest from the Agentic Architect persistence kit: 7 senior C#/.NET rules for engineers keeping Cursor honest across sessions.
aicodingOpenAI plans to end Cursor’s native access to its AI models following SpaceX's $60 billion acquisition of the coding startup. With a proposed November 12 transition date, the split highlights growing tensions across the AI industry.
aiHow to Give Cursor and Claude Code Long-Term Memory Across Sessions via MCP By MemorySync...
aiEvery Cursor session starts the same way: a blank slate. You explain your stack, your conventions,...
cursorHave you ever felt like you're drowning in a sea of context switches, managing an army of tiny tasks,...
litellmThis guide connects Cursor to a LiteLLM proxy, one you run yourself or one your team already hosts....
Workflows from the Neura Market marketplace related to this Cursor resource