Context Management for AI Coding: Stay Under the Limit, Stay Coherent
Long AI coding sessions lose coherence as context windows fill. Learn to manage context strategically — what to include, what to summarize, and when to start a fresh session.
The Context Window Problem
Every AI model has a context window — the amount of text it can "see" in one conversation. For coding sessions, this window fills quickly: your codebase files, conversation history, code generations, and corrections. Once near the limit, models start forgetting earlier context, making mistakes that would have been obvious earlier in the conversation.
Good context management is the difference between a productive 2-hour coding session and one that falls apart after 30 minutes.
What to Include in Context (Prioritized)
- The immediate task — what you're building right now
- Directly relevant code — files being modified or that the new code depends on
- Types and interfaces — TypeScript types the generated code must satisfy
- Constraints and conventions — rules the output must follow
- Examples of similar existing code — one example, not your entire codebase
What to Exclude
- Files unrelated to the current task (don't dump your whole codebase)
- Test files unless you're writing tests
- Long conversation history that's no longer relevant
- Documentation you can reference by description instead of pasting
The Context Refresh Pattern
For long sessions, periodically summarize and start fresh:
Before we continue, let me summarize what we've built so far:
## Session Summary
Completed:
- User authentication with Auth.js (app/lib/auth.ts)
- Article CRUD API routes (app/api/articles/route.ts)
- Article list page with pagination (app/articles/page.tsx)
In progress:
- Article editor component — have the basic form, need to add image upload
Key decisions made:
- Using draft-js for rich text (not TipTap — we hit compatibility issues)
- Images go to Cloudinary, URLs stored in article.featuredImage field
- Editor is a Client Component; article save is via API route
Current task:
Add image upload to the article editor.
The upload should go to POST /api/upload which already exists and returns { url: string }.
Relevant code: [PASTE JUST THE EDITOR COMPONENT]
Context Compression Technique
Instead of pasting full files, compress them to interfaces and signatures:
// Instead of pasting the full 200-line ArticleService class:
// ArticleService API (paste this summary):
class ArticleService {
static async getAll(filters: ArticleFilters): Promise<Article[]>
static async getById(id: string): Promise<Article | null>
static async create(data: CreateArticleInput, userId: string): Promise<Article>
static async update(id: string, data: UpdateArticleInput): Promise<Article>
static async delete(id: string): Promise<void>
static async publish(id: string): Promise<Article>
}
// type definitions:
type Article = { id, title, slug, content, status, authorId, publishedAt, ... }
type CreateArticleInput = Omit<Article, 'id' | 'createdAt' | 'updatedAt' | 'authorId'>
Multi-Session Projects
For projects spanning multiple sessions, maintain a session state file:
# session-state.md (don't commit this)
## What We're Building
[Current feature description]
## Done This Session
- [x] Added Zod validation to article API routes
- [x] Fixed the pagination bug on article list
## TODO
- [ ] Image upload component
- [ ] Article preview mode
## Decisions Made
- Using react-dropzone for upload UX (already installed)
- Max image size: 10MB enforced server-side
## Open Questions
- Should drafts be auto-saved? (ask the team)
Start each session by reading this file first — it's 20 lines that replace a full session of re-establishing context. Update it at the end of every session.
Admin
Cal.com
Open source scheduling — tự host booking system, thay thế Calendly. Free & privacy-first.
Bình luận (0)
Đăng nhập để bình luận
Chưa có bình luận nào. Hãy là người đầu tiên!