ai notes
Concept

Coding Patterns Stop AI Forgetfulness

Using a strict coding pattern helps an AI write better code by giving its limited short-term memory a clear blueprint to follow.

Created 3 min read

Using a strict coding pattern helps an AI write better code by giving its limited short-term memory a clear blueprint to follow. When code is messy, the AI wastes its “attention budget” trying to figure out how everything connects, causing important details to fade into background noise. Standard patterns (like Clean Architecture) let the AI use shortcuts it already learned during training, while custom patterns work like a temporary template you paste into the chat that the AI constantly copies on the fly. Either way, keeping your code structured forces the AI to focus its math on exactly what matters, preventing it from getting confused or forgetting your rules.


How it actually looks to the model:

Imagine your custom pattern is a rule where you always put an underscore before a variable name, like _myVariable. The AI has never seen this exact convention on GitHub, so it has to pick it up from your chat on the spot.

Here’s the thing to know first: the model is always doing a kind of backward glance as it writes. It’s not a special mode that only switches on for your custom rules — it runs constantly, on standard code and your code alike. What changes is what it finds when it looks back.

  1. Reading the example. The AI reads your example in the context window: [code block...] _myVariable. It doesn’t just store the literal text — it picks up on the habit: “around here, variable names get an underscore in front.”
  2. Generating new code. Now you ask it to write a new function using your pattern. As it works, part of the model is continuously glancing back through the chat, asking “have I seen something like this before, and what tended to come next?”
  3. The match. When it’s about to name a variable, that backward glance lands on your example. The pattern matches, so the model leans harder on your example than on the generic code it learned in training — it weights your convention more heavily for this moment.
  4. Reapplying, not copying. It writes the underscore, then the new name. Note that it’s not photocopying _myVariable — the variable names differ every time. It inferred the rule from your example and reapplied it to a word it had never seen.

A quick analogy for that last step: literal copying would be reading “milk, eggs, bread” straight off a list you wrote. What the model actually does is more like noticing you always grab the store-brand version of things, then picking store-brand for a new item that was never on your list. It learned your preference, not your exact list.

One honest caveat: “glance back, find the pattern, reapply it” is a real and load-bearing piece of what’s happening — but it’s a simplified picture, not the complete mechanism. It’s the right intuition to carry around, just not the whole story.

That’s why Context compression for better code generation and Software systems integrating with AI still benefits from good software architecture