Research published on arXiv and discussed heavily on Hacker News (257 points, 150 comments) has identified a fundamental vulnerability in how large language models handle code generation tasks: constraint decay. As models generate code step-by-step, they gradually abandon safety constraints that were present at inference start. The phenomenon appears particularly acute in backend code scenarios where security constraints—like parameterized SQL queries to prevent injection attacks—are critical. This finding challenges assumptions underlying many self-hosted and local LLM deployments, where organizations assume model behavior remains consistent throughout a single generation session.

The degradation follows a predictable pattern. A model instructed to generate SQL with parameterized queries using placeholders will initially comply, producing correct syntax like `SELECT * FROM users WHERE id = ?`. By step five or six of continued generation, the model begins mixing approaches. By step ten, it produces concatenated string queries—`SELECT * FROM users WHERE id = '` + userid + `'`—directly vulnerable to SQL injection. This isn't a one-off failure case; researchers demonstrated the pattern across multiple constraint types and model architectures. The mechanism appears to stem from inference-time degradation rather than fine-tuning artifacts. As the context window fills with previously generated tokens, the model's attention to the original constraint instruction diminishes, particularly when those constraints compete with the statistical likelihood of common code patterns in training data.

This mechanism differs from simple context-window exhaustion. Models with sufficient remaining context still exhibit constraint decay, suggesting the issue involves how transformer architectures weight earlier instructions against accumulated generation history. The findings directly impact organizations running open-source models locally—whether via Ollama, llama.cpp, or container deployments—for code generation workflows. High-risk use cases like financial transaction code, authentication systems, and healthcare data queries remain unsafe without additional verification layers. However, lower-stakes scenarios—documentation generation, boilerplate creation, and refactoring suggestions where human review is standard—remain viable. The research underscores why specialized models (aligning with recent findings that specialization beats scale for procurement decisions) may outperform general-purpose LLMs for constrained tasks. Teams deploying local models for code generation should implement mandatory static analysis verification rather than assuming model constraints persist across generation.