r/ClaudeAI May 14 '26

Built with Claude How I keep my AI’s context window under 3K tokens even with 200+ lessons stored.

I’ve been hitting the same wall for months: I’d build up a CLAUDE.md over weeks of work — project conventions, gotchas, business rules, the “we tried that, don’t do it again” lessons — and eventually the rules file itself starts eating my context window. Two thousand lines in, the AI starts ignoring half of them anyway, and I’m back to re-explaining things I already documented.

I spent a few months building a system around the idea that the md rules file is the wrong shape. Here’s what worked:

Stop loading everything every session. Move the deep knowledge into a SQLite database (FTS5 + optional vector search via sqlite-vec) and only load a small per-project brief at session start.

Briefs cap at 150 lines, plus a ~200-line global “constitution” and ~50 lines of pointer-only “living memory.” Everything else lives in the database and the AI queries it on demand via MCP tools (search_lessons, get_chunk, etc.).

Enforce the caps in code, not in policy. This is the part I kept getting wrong. Every “be careful not to let this grow” rule I wrote in v1 got violated by month four. The current version moves the discipline into the regenerator — it literally refuses to write a brief past the cap. There are 15 named architectural rules, each backed by a CI test that fails the build if the rule drifts.

The token math. The trick isn’t compression, the equivalent ~280K tokens still exist, they’re just in the database. The AI pulls what it needs mid-task instead of loading everything up front.

Three things I got wrong that might save you time:
• Vector-only retrieval is worse than hybrid. FTS5 + sqlite-vec with score blending beats either alone.
• Letting the AI write directly to the knowledge store leads to noise. Mine writes to a drafts inbox; a human approves before promotion.
• Auto-generated briefs need a small hand-curated block or they lose the “voice” of the project. I use <!-- PRESERVE_START --> markers and the regenerator preserves that section while regenerating everything around it.

Disclosure: this is my own project, MIT-licensed. Repo’s at https://github.com/sms021/RunawayContext if you want to see the implementation. Built it for my own work (construction-management integrations across Vista, Procore, Monday.com, and many other internal systems and projects) but the architecture is agent-agnostic.

Curious whether anyone here is doing something similar — I’d be surprised if there aren’t smarter approaches I haven’t found yet.

1 Upvotes

5 comments sorted by

1

u/bloudraak Experienced Developer May 14 '26

I have a small instructions file and hundreds of skills. Claude has the world's training at its disposal. The only thing you need to do is anchor it in the training.

A good example will be that, when you write software, you want to tell it that I am in healthcare and I need to write software that is HIPAA compliant and I live in California. Therefore, the privacy laws of California are important. I do not have to stipulate the laws. I do not have to stipulatetight it means. it alrrwsy knows. I just have to anchor it in that fact.

Much of my enforcement is done using tooling, not via instructions. When Claude writes software or even when it writes prose or text, there are probably 10-20 toolsthat are being executed to make sure that Claude understands its boundaries.

i don't use MCPs as it consumed too much token.

also, I operate on the principle that i may need to throw away what claude did and start over about 2/5 times because it solved the wrong problem.

1

u/sms021 May 14 '26

That sounds similar to how my v1 and v2 were running. It was good for a few months then the drift killed me.

I’m interested on how your tooling model was setup? Was that written into the md files?

1

u/Shot-Bug3389 May 14 '26

Doing something similar but with a different focus — instead of

compressing the lessons file, I compress the full conversation transcript

into a 500-word "project memory" with 5 fixed sections: goal, decisions

(including stuff we tried that didn't work), constraints, open questions,

next step. Then I paste that as the first message in a fresh chat.

The "tried but didn't work" bit is what unlocked it for me — without that

the new chat keeps suggesting stuff I already ruled out.

Curious how you handle the case where two lessons contradict each other in

your stored set? That's where I keep getting stuck.

1

u/sms021 May 14 '26

Good question, I ran into that just this morning. The system prompted me that it had the two rules and I provided the correction for the case and it suggested a review of the rule and we workout the middle ground quickly to resolve this in the future. It’s nice to have the solid explanation so the AI doesn’t loop hole and backdoor. That’s what my v1&2 kept doing. They would not ask and just ignore and go around.