Your agent needs a sanity check loop — here's how to stop it from confidently shipping nonsense
Your coding agent just spent 20 minutes implementing a brilliant solution to connect your React app to a MongoDB database. There's just one problem: your app uses PostgreSQL and always has.
This isn't a hallucination. It's worse. Your agent remembered that you use PostgreSQL, acknowledged it in the conversation, then proceeded to write MongoDB connection code anyway. The context was right there. It just... ignored it.
I've watched this pattern kill more agent projects than any other failure mode. Your agent gets 90% of the way to a perfect solution, then confidently delivers something that makes no sense in your actual context.
The fix isn't better prompts or bigger context windows. It's a sanity check loop.
The pattern: Before your agent delivers any work, it has to explain why its solution makes sense given your specific constraints.
Here's the verification prompt I add to every coding agent:
Before implementing this solution, verify it makes sense: 1. What technology stack is this project using? 2. What are the key constraints I mentioned? 3. How does this solution fit those constraints? 4. What would break if I'm wrong about the context? If anything doesn't align, ask for clarification before proceeding.
This catches the MongoDB-for-PostgreSQL mistakes, but it also catches subtler context drift. Like when your agent suggests a microservice architecture for your solo project, or recommends enterprise patterns for your weekend hack.
The magic happens in step 4. Forcing your agent to consider what would break makes it actually think about consequences instead of just pattern-matching from training data.
I run this check at three points:
- Before starting: "Does this approach make sense for our setup?"
- Mid-implementation: "Am I still on the right track given our constraints?"
- Before delivery: "Will this actually work in their environment?"
The mid-implementation check is crucial. That's where I catch agents that start correct but drift toward generic solutions as they work through the problem.
Here's what this looks like in practice. Instead of:
"I'll implement user authentication using Firebase Auth..." *proceeds to write Firebase code*
You get:
"Let me verify this approach: 1. You're using Next.js with PostgreSQL 2. You want to avoid external dependencies where possible 3. You mentioned you're already using NextAuth Firebase Auth would add an external dependency and duplicate NextAuth. Let me implement this with NextAuth instead..."
The sanity check adds 30 seconds to each major decision. It saves hours of rework.
Some agents resist this pattern — they want to jump straight to implementation. Push back. An agent that can't explain why its solution makes sense is an agent that's about to waste your time.
The best part? This pattern works for any domain, not just coding. Content agents that verify they understand your brand voice. Research agents that confirm they're answering your actual question. Business agents that check their recommendations against your budget constraints.
Your agent is smart enough to solve complex problems. Make sure it's disciplined enough to solve the right problem.