Sonnet 5's Flutter Code Keeps Going Rogue — Here's the Constraint System That Stops It
As coding agents grow more autonomous, the bottleneck shifts from "can it write code" to "can it write only the code you asked for." Without explicit boundaries, Sonnet 5's helpfulness becomes a liability that burns tokens, breaks builds, and forces developers into multi-round rework cycles.
Claude Sonnet 5's agentic tendencies make it prone to scope drift, paradigm wobble, and self-check hallucinations in Flutter projects. The root cause is not model capability but a mismatch between growing autonomy and weak constraints. A two-layer anchoring system — a short, non-negotiable AGENTS.md plus glob-loaded .cursor/rules files under 500 characters — ensures core red lines survive every round of context.
Constraint-based prompts that specify where to change, what to change, and what NOT to change prevent the model from "incidentally" refactoring a LoginPage when asked to tweak a button. For complex tasks, a 200-token plan phase saves 3–5 rounds of rework. Session hygiene — one task per chat, compacting before the context ring turns yellow — stops early constraints from being diluted by round 10.
Two Flutter-specific hooks plug the highest-frequency drift points: WidgetKeys constants prevent the model from oscillating between find.text and find.byKey, while excluding *.g.dart files from the model's view and forbidding hand-written generated code stops a common source of compilation failures. Across production projects, the same Cursor Pro quota lasted a full month instead of two weeks after adopting the system.
The core tension in AI-assisted coding has inverted: it's no longer about coaxing useful output from weak models, but about restraining over-eager strong models from doing too much.
Sonnet 5's helpfulness is a double-edged sword — its tendency to "incidentally optimize" is a feature for greenfield projects and a bug for maintaining existing codebases with established conventions.
The 500-character alwaysApply limit is a practical discovery, not a theoretical one; it reflects how Sonnet 5's new tokenizer silently inflates token counts and causes models to skip long rule files.
Constraint-based prompting treats the model like a junior engineer who needs explicit boundaries, not a senior one who can infer intent — a mental model shift that many developers haven't made yet.
Flutter's specific pain points (deep widget trees, multiple state management options, generated files) make it a canary for AI coding drift; the same patterns will surface in other frameworks as models grow more agentic.