Context Lifecycle: Document, Clear, Resume
Scenario
Context: You're working on a complex task and notice the AI's responses are degrading in quality
Goal: Learn the Document & Clear pattern and phased workflow for maintaining high-quality AI output
Anti-pattern: Letting context accumulate until quality degrades, trusting auto-compaction
Conversation
The Context Lifecycle Problem
AI context windows have limits. As conversation grows:
- Older context gets compressed or dropped
- Quality degrades as relevant info is lost
- Auto-compaction is unpredictable
Solution: The Document & Clear Pattern
- Document: Save important state to external files
- Clear: Explicitly reset context with
/clear - Resume: Start fresh by reading the saved files
Tool Support:
- Claude Code:
/clearcommand - GitHub Copilot:
/clearcommand (same!) - Cursor: New Composer session
Both Claude Code and Copilot CLI now share the same /clear command. The tools have converged.
When to Clear Context
Rule of thumb: If less than 50% of prior context is relevant to current work, clear it.
Symptoms of context bloat:
- AI forgets earlier decisions
- Responses become inconsistent
- AI asks questions you already answered
- Quality noticeably drops
Before I clear, let me document the current state.
Please write a progress summary to docs/refactor-progress.md including:
- What we've completed
- Key decisions we made
- Current blockers
- Next steps
I'll create a progress summary:
# Refactor Progress - Auth System
## Completed
- [x] JWT service implementation (`src/services/jwt.ts`)
- [x] Refresh token rotation logic
- [x] Database migration for refresh_tokens table
- [x] Type definitions (`src/types/auth.ts`)
## Key Decisions
- Access token expiry: 15 minutes
- Refresh token expiry: 7 days
- Refresh rotation: Single-use (invalidate on use)
- Backward compatibility: Dual-mode auth during migration
## Current Blockers
- Need to decide on token storage (httpOnly cookie vs localStorage)
- Rate limiting strategy for refresh endpoint
## Next Steps
1. Create hybrid auth middleware (accepts session OR JWT)
2. Update `/auth/login` to return tokens
3. Add `/auth/refresh` endpoint
4. Update frontend auth hook
Written to docs/refactor-progress.md.
/clear
Context cleared. Ready for a fresh start.
Read docs/refactor-progress.md and continue with the next steps. Start with the hybrid auth middleware.
Reading docs/refactor-progress.md...
Got it. You're migrating from session-based auth to JWT. The JWT service is complete. Now I'll create the hybrid auth middleware that accepts both session AND JWT during the migration period.
// src/middleware/hybridAuth.ts
import { Request, Response, NextFunction } from 'express';
import { verifyAccessToken } from '../services/jwt';
import { getSession } from '../services/session';
export async function hybridAuth(req: Request, res: Response, next: NextFunction) {
// Try JWT first (preferred path)
const authHeader = req.headers.authorization;
if (authHeader?.startsWith('Bearer ')) {
const token = authHeader.slice(7);
try {
req.user = await verifyAccessToken(token);
req.authMethod = 'jwt';
return next();
} catch (e) {
// Fall through to session check
}
}
// Fallback to session (legacy path)
const session = await getSession(req);
if (session?.userId) {
req.user = { id: session.userId };
req.authMethod = 'session';
console.warn('DEPRECATION: Session auth used. Migrate to JWT.');
return next();
}
return res.status(401).json({ error: 'Authentication required' });
}
Created hybrid middleware. What's next?
The Research → Plan → Implement Workflow
For complex tasks, use a phased approach with context clearing between phases:
- Research Phase: Explore codebase, understand patterns
/clear- Reset context- Plan Phase: Create detailed implementation plan
/clear- Reset context- Implement Phase: Execute the plan with focused context
Key insight: Reviewing the plan is higher leverage than reviewing the code.
RESEARCH PHASE
Explore how authentication is handled across the codebase. Find:
- All auth-related files
- Existing patterns and conventions
- Dependencies and integrations
Don't write any code yet - just research and write findings to docs/auth-research.md.
I'll research the auth landscape and document findings...
# Auth System Research
## Files Found
- `src/middleware/auth.ts` - Current session-based middleware
- `src/services/session.ts` - Redis session management
- `src/routes/auth.ts` - Login/logout/register endpoints
- `src/hooks/useAuth.ts` - Frontend auth hook (React Query)
- `tests/auth/**` - 45 auth-related tests
## Patterns Observed
- All protected routes use `authMiddleware`
- Session stored in Redis with 24h TTL
- Frontend uses `useAuth().user` for current user
- API errors use `{ error: string, code?: string }` format
## Dependencies
- express-session (v1.18.0)
- connect-redis (v7.1.0)
- Frontend: @tanstack/react-query for auth state
## Integrations
- User service fetches profile on auth
- Websocket auth uses same session cookie
- Admin dashboard has separate auth check
Written to docs/auth-research.md. Ready for planning phase when you are.
/clear
Context cleared.
PLAN PHASE
Read docs/auth-research.md and create a detailed implementation plan for JWT migration that:
- Maintains backward compatibility
- Handles all integrations found
- Includes test strategy
Write the plan to docs/jwt-migration-plan.md.
Reading docs/auth-research.md... Creating implementation plan:
# JWT Migration Plan
## Phase 1: Infrastructure (No Breaking Changes)
1. Add JWT service (sign, verify, refresh)
2. Add refresh_tokens table
3. Create hybrid auth middleware
4. All existing code continues to work
## Phase 2: Dual-Mode Operation
1. Update `/auth/login` to return JWT + session
2. Add `/auth/refresh` endpoint
3. Update websocket to accept JWT auth
4. Frontend: Add JWT support to useAuth (opt-in flag)
## Phase 3: Migration Period (2 weeks)
1. Log usage: track session vs JWT auth
2. Notify clients via deprecation header
3. Update admin dashboard
4. All new features use JWT-first
## Phase 4: Session Removal
1. Remove session fallback from hybrid middleware
2. Remove Redis session store
3. Remove express-session dependency
4. Update documentation
## Test Strategy
- Unit: JWT service (sign, verify, rotate)
- Integration: Hybrid middleware (both paths)
- E2E: Full login → refresh → logout flow
- Regression: All 45 existing auth tests must pass
## Risks & Mitigations
- Risk: Client not migrating → Mitigation: 2-week deprecation period with warnings
- Risk: Websocket auth breaks → Mitigation: Test in staging first
Plan written to docs/jwt-migration-plan.md.
Review this plan before implementation. Catching issues here is much cheaper than catching them in code.
Context Utilization Target
Keep context utilization at 40-60%:
- Below 40%: You might be clearing too often
- Above 60%: Quality may start degrading
- Above 80%: Clear immediately
Most AI tools show context usage. Monitor it and clear proactively.
Key Takeaways
- Use `/clear` to reset context when <50% remains relevant (works in both Claude Code and Copilot)
- Document state to external files BEFORE clearing - create durable memory
- Research → Plan → Implement: Clear context between phases for focused work
- Review the plan before implementation - highest leverage review point
- Keep context utilization at 40-60% for optimal quality
Try It Yourself
Prompt Template
Before clearing, summarize current progress to a markdown file:
Please write a progress summary to `docs/progress.md` with:
1. What's completed
2. Key decisions made
3. Next steps
Then I'll use /clear to reset context. Variations to Try
- Simple clear: Just use `/clear` for a fresh start
- Research phase: "Explore the codebase for [topic]. Don't write code yet - just document findings."
- Plan phase: "Read [research file] and create an implementation plan in [plan file]."
- Resume: "Read [progress file] and continue with the next steps."
Related Content
Sources
How I Use Every Claude Code Feature
Introduces the Document & Clear pattern for managing AI context. Key insight: rather than trusting automatic context compaction, explicitly clear context and persist important state to external files. This produces more reliable outputs and gives you control over what the AI 'remembers'.
- Document & Clear method: dump plan to .md file, /clear, restart with file reference
- Auto-compaction is opaque, error-prone, and not well-optimized - avoid /compact
- Use /clear + /catchup for simple reboots (custom command reads uncommitted changes)
Advanced Context Engineering for Coding Agents
Introduces the Research → Plan → Implement workflow for AI-assisted development. Key insight: reviewing plans is higher leverage than reviewing code. The workflow explicitly clears context between phases to maintain focus and quality.
- Research → Plan → Implement workflow produces better results than direct coding
- Keep context utilization in the 40-60% range depending on complexity
- A bad line of a plan could lead to hundreds of bad lines of code - review plans first