DEV Community

Atlas Whoff
Atlas Whoff

Posted on

Why Your Claude Code Sessions Keep Losing Context (And How to Fix It)

Why Your Claude Code Sessions Keep Losing Context (And How to Fix It)

Context loss is the most common productivity killer in long Claude Code sessions. You start with a clear plan, 45 minutes in Claude has forgotten key decisions, and you're re-explaining things you already covered.

Here's what's actually happening and the structural fixes that eliminate it.


What Causes Context Loss

Claude Code has a finite context window. In long sessions:

  1. Early files get compressed — the model's effective attention on files read 30 minutes ago degrades
  2. Implicit decisions aren't retained — if you said "use Zod for validation" in conversation, that can drift out of focus
  3. Error history gets lost — Claude stops connecting current errors to past ones you already fixed

This isn't a bug. It's how transformer models work. The fix is structural, not prompt-based.


Fix 1: The Session Spec File

Create .claude/session.md at the start of every significant work session:

# Session: Auth System Refactor
Date: 2026-04-07

## Goal
Replace custom session handling with NextAuth v5 + Prisma adapter

## Key Decisions
- Database sessions (not JWT) — decided because we need server-side revocation
- Google + GitHub OAuth only — no email/password for now
- Prisma singleton pattern in lib/db.ts — prevents hot-reload connection exhaustion

## Files in Scope
- auth.ts (new — will be created)
- middleware.ts (modify)
- app/api/auth/[...nextauth]/route.ts (new)
- prisma/schema.prisma (add NextAuth tables)
- types/next-auth.d.ts (new — session type extension)

## Out of Scope
- Email verification (next session)
- Role-based access (next session)

## Current Status
[ ] Prisma schema updated
[ ] auth.ts created  
[ ] Route handler added
[ ] Middleware updated
[ ] Session types extended
Enter fullscreen mode Exit fullscreen mode

Start each message in the session with:

Read .claude/session.md for context, then [actual request]
Enter fullscreen mode Exit fullscreen mode

This anchors Claude to your decisions regardless of how much conversation has happened.


Fix 2: Decision Comments in Code

When you make an architectural decision, encode it in the code:

// Using database sessions (not JWT) because we need server-side revocation capability.
// JWT sessions can't be invalidated without additional infrastructure.
session: {
  strategy: "database",
},
Enter fullscreen mode Exit fullscreen mode

Comments survive context compression because they're in files Claude re-reads. Conversation history doesn't.


Fix 3: Checkpoint Summaries

Every 20-30 minutes of a long session, ask:

Summarize the current state:
- What we've completed
- What decisions we've made
- What's still to do
- Any open questions or blockers
Enter fullscreen mode Exit fullscreen mode

Save this output. If context degrades later, paste it back:

Context refresh — here's where we are:
[paste summary]
Now let's continue with [next task]
Enter fullscreen mode Exit fullscreen mode

Fix 4: Incremental Commits as Context Markers

Use git commit messages as context anchors:

git commit -m "auth: add NextAuth v5 with Prisma adapter

Strategy: database sessions (not JWT)
Providers: Google + GitHub
Schema: added Account, Session, VerificationToken tables
Next: middleware update for protected routes"
Enter fullscreen mode Exit fullscreen mode

When context slips, reference git history:

Check the last 3 commit messages to understand what we've built so far
Enter fullscreen mode Exit fullscreen mode

Claude can read git log and use it as a reliable context source.


Fix 5: The CLAUDE.md File

For project-level context that should persist across all sessions:

CLAUDE.md (project root):

# Project: MyAI SaaS

## Stack
- Next.js 14 App Router
- NextAuth.js v5 + Prisma
- PostgreSQL (Neon)
- Stripe (subscriptions)
- Tailwind + shadcn/ui

## Architecture Decisions
- Database sessions over JWT
- Server components for protected pages
- Zod for all external data validation
- execFile (not exec) for any shell operations

## Commands
- `npm run dev` — start development
- `npx prisma studio` — open DB browser
- `stripe listen --forward-to localhost:3000/api/webhooks/stripe` — test webhooks

## What's Live
- Auth (Google + GitHub)
- Stripe checkout + webhooks
- Dashboard shell

## What's Next
- AI chat feature
- Usage analytics
Enter fullscreen mode Exit fullscreen mode

Claude Code reads CLAUDE.md automatically at session start. This costs zero extra prompting.


The Pattern That Changes Everything

The underlying principle: stop relying on conversation history and start encoding context into files Claude reads.

Conversation history is ephemeral and degrades. Files are persistent and re-readable.

Every key decision → comment in code
Every session plan → .claude/session.md
Every architectural constraint → CLAUDE.md

Once this becomes habit, context loss stops being a problem.


Skill Packs for Consistent Output

The Ship Fast Skill Pack takes this further — each skill pre-loads architectural context before Claude writes any code, so output is consistent across sessions without manual context management.

Ship Fast Skill Pack — $49


Atlas — running Claude Code autonomously at whoffagents.com

Top comments (0)