Context Management
How to maintain context across AI coding sessions
Context Management
AI coding assistants have no memory between sessions. This guide explains how to maintain context effectively.
The Problem
- AI loses context between sessions
- AI forgets what was already built
- AI suggests different patterns each time
- AI doesn't know what NOT to touch
Solution: Live State Files
CRITICAL
These files live in the CODE REPOSITORY ROOT (not docs) and must be kept current.
PROJECT_STATE.md
Purpose: Track current progress at step-level granularity.
Update Frequency: After EVERY step (~30 seconds)
Contents:
- Current phase and step
- Last completed step with timestamp
- Session context and handoff notes
- Completed phases with locked files
- Session log for history
- "NEVER DO" rules
See PROJECT_STATE.md Template for full template.
WHAT_EXISTS.md
Purpose: Inventory of all built components.
Update Frequency: After each phase completion
Contents:
- Database models and tables
- API endpoints by module
- Frontend routes
- Established patterns (copy-from examples)
- Guards and decorators
- Environment variables
- NPM packages
See WHAT_EXISTS.md Template for full template.
Update Procedure
After EVERY Step:
1. Open PROJECT_STATE.md
2. Mark completed step: - [x] Step XX: Description ✅ (HH:MM)
3. Update current step section
4. Update progress: XX of YY steps (ZZ%)
5. Save fileAfter Phase Completion:
1. Update PROJECT_STATE.md - move phase to Completed
2. Update WHAT_EXISTS.md - add all new components
3. Create git tag: git tag phase-XX-name
4. Commit both filesAdditional Context Files
Implementation Plan
Single source of truth for current system state:
- Current building block and status
- Completed and locked building blocks
- Recent decisions and changes
- What AI can modify vs what is locked
See Implementation Plan for current status.
Patterns Documentation
Exact code patterns to follow:
- Service pattern structure
- Controller pattern structure
- Repository pattern structure
- Response format patterns
See Patterns for exact patterns.
Constraints Documentation
Hard rules that must never be violated:
- Version requirements (Prisma 5.x, NOT 6.x)
- Technology restrictions (no GraphQL, no microservices)
- Architecture boundaries
- Complexity limits
See Constraints for hard rules.
Session Workflow
Before Starting Work
- Read Implementation Plan to understand current state
- Read current building block specification
- Read Patterns for code structure
- Read Constraints for hard rules
- Check what files are locked (cannot modify)
During Work
- Follow patterns exactly (no variations)
- Respect constraints (no exceptions)
- Don't touch locked files
- Update context after significant changes
After Completing Work
- Update Implementation Plan with what was built
- Document any new patterns in Patterns
- Update building block status
- Lock completed files
Context Preservation Strategies
Pattern Locking
Once a pattern is established, lock it:
- Document in Patterns
- Reference in building block
- Reject any AI suggestions to change it
Decision Locking
Once a decision is made, lock it:
- Document in Constraints
- Create ADR if significant
- Reject any AI suggestions to change it
File Locking
Once a building block is complete, lock its files:
- Tag in git
- Document in Implementation Plan
- Reject any AI suggestions to modify
Common Context Loss Scenarios
Scenario 1: AI Forgets Completed Work
Problem: AI suggests rebuilding something already done
Solution:
- Check Implementation Plan for completed blocks
- Reference locked files
- Reject suggestion to rebuild
Scenario 2: AI Suggests Wrong Pattern
Problem: AI suggests different pattern than established
Solution:
- Reference Patterns for correct pattern
- Reject suggestion
- Use established pattern exactly
Scenario 3: AI Wants to Add Technology
Problem: AI suggests adding new technology
Solution:
- Check Constraints for technology restrictions
- Check Boilerplate Definition for locked technologies
- Reject if not approved (requires ADR)
Best Practices
- Always check context files first - Don't assume AI remembers
- Reference specific patterns - Point to exact pattern in Patterns
- Enforce constraints strictly - No exceptions without ADR
- Update context immediately - After any significant change
- Lock completed work - Prevent AI from "improving" working code