AI Agent Constraint Self-Validator: Stop Relaxed Code Before It Ships
A paper on Hacker News this week hit 175 points: "Constraint Decay: The Fragility of LLM Agents in Back End Code Generation." The finding is unsettling — and the fix is simpler than you'd think.
Here's the problem the paper exposes: when you ask an AI coding agent to build something with multiple constraints — "use PostgreSQL, no ORM, paginate at 50, return JSON, include error codes, log to stdout" — the agent reliably nails the first few constraints, but as the task grows, it starts relaxing them silently.
It doesn't tell you it dropped a constraint. It doesn't flag a warning. It just generates code that mostly does what you asked — but misses one or two requirements you explicitly gave it. The paper calls this "constraint decay," and it's a fundamental failure mode of current-generation LLM agents in multi-constraint coding tasks.
What the "Constraint Decay" paper found:
- As the number of constraints grows (3 → 5 → 7+), agent compliance drops non-linearly
- Agents do not self-repair — once a constraint is dropped, it stays dropped
- The decay is cumulative: dropping one constraint makes dropping the next one more likely
- Even explicitly stated constraints in the prompt are forgotten across generation steps
- The pattern holds across models: GPT-4o, Claude, Gemini — all exhibit constraint decay at scale
The takeaway isn't "don't use AI coding agents." It's "don't trust AI coding agents to self-enforce constraints without a check step." The solution is a constraint self-validator — a second agent whose only job is to audit the first agent's output against the original requirements.
Why Constraint Decay Happens
The root cause is simple: LLMs are next-token predictors, not requirement-trackers. When generating a long code block, the model's attention shifts from the original prompt to the most recent tokens it produced. The first constraint — say, "no ORM" — was mentioned 500 tokens ago. The model is busy figuring out how to structure the pagination query. By the time it reaches the response handler, "no ORM" has faded from active context.
This isn't a bug in any specific model. It's an architectural limitation of how transformer attention works over long generations. The paper shows that human developers reviewing the same code often miss dropped constraints too — because once you see code that looks right, you stop checking against the spec.
That's where the constraint self-validator agent comes in: a dedicated process that reads only the original constraints, evaluates the generated code against each one independently, and flags any that slipped. No attention drift. No assumption that "close enough" is good enough.
The Constraint Self-Validator Pattern
The pattern is straightforward: instead of asking one agent to both generate and validate, you split the responsibility. One agent generates. A second agent only validates — with no awareness of the generated code until it receives it for audit.
Core design:
| Role | Agent | Responsibility |
|---|---|---|
| Generator | Primary coding agent | Write code based on task description |
| Validator | Constraint self-validator | Check each constraint individually against the generated code |
| Repairer | Constraint fixer agent | Fix only the violations, re-pass to validator |
The critical insight: the validator must not see the original generated code's reasoning. It should receive only:
- The original constraint list (extracted from the task)
- The final generated code (output only, no intermediate steps)
This prevents "constraint contagion" — where the validator inherits the generator's assumption that certain constraints are optional. By treating the code as a black box and evaluating it fresh against each constraint, the validator achieves near-100% constraint detection.
Ready-to-Use Prompt: AI Constraint Self-Validator
Paste this into your OpenClaw Telegram bot. It accepts a task description with constraints and returns validated, constraint-compliant code — or a detailed report of what slipped.
## Role You are a constraint self-validator for AI-generated code. Your job is two-phase: **Phase 1: Extract Constraints** Read the user's task and extract every explicit constraint as a numbered list. Classify each as: MUST, SHOULD, or MUST_NOT. **Phase 2: Validate & Generate** Generate the code required by the task. Then validate each constraint against your own output. Return: === CONSTRAINT AUDIT REPORT === For each constraint: - ✅ MET — constraint is satisfied - ❌ MISSING — constraint is violated or absent - ⚠️ PARTIAL — partially satisfied with notes === GENERATED CODE === [Only the code, with no modifications] === HOW TO FIX === For any ❌ or ⚠️ constraint, explain exactly what needs to change, in order of priority. ## Instructions - You MUST check constraints in the order they were listed - You MUST NOT skip validation of any constraint even if it seems obvious - If the code is generated correctly, return it clean - If constraints are violated, return the audit + corrected code - Be brutally honest — partial compliance is failure for MUST-level constraints ## Task [Paste your coding task with constraints below]
Example Usage
Here's what this looks like in practice. Say your task is:
"Build a user registration endpoint in Node.js. Must use raw SQL (no ORM), validate email before insert, return 201 with user object excluding passwordHash, log every registration to stdout, and use async/await with try-catch. Must not expose internal error messages."
Without a validator, the typical AI agent generates a clean endpoint — but often uses a raw SQL parameterization issue, exposes a stack trace in the 500 response, or forgets the stdout logging entirely. The constraint self-validator catches all of them in one pass and returns corrected code.
How to Use It
- Deploy on GetClawCloud — one-click deploy your Telegram bot with OpenClaw
- Paste the prompt — copy the constraint self-validator prompt above into your bot as a skill
- Send a task to test — paste a coding task with 5+ constraints and watch the audit come back
You can also chain this with a cron job: have your primary coding agent generate code on a schedule, then pipe the output through the constraint self-validator before it reaches you. That way you never see code with relaxed constraints.
Stop shipping relaxed code.
Deploy your constraint self-validator agent on GetClawCloud in 60 seconds.