Skip to content

How to Manage Context in AI Coding Sessions: Break Work into Focused Tasks

My AI coding session was a mess. I’d been working with Claude for three hours straight, building a payment processing system. At hour one, everything was crisp. The AI understood my project structure, followed my naming conventions, and wrote clean code.

By hour three? Total chaos.

The code it produced ignored the authentication middleware I’d specified at the start. It created duplicate utility functions that already existed elsewhere. When I asked it to modify a function from hour two, it hallucinated parameters that never existed. I spent more time fixing its mistakes than I would have writing the code myself.

Then I found a Reddit thread that changed everything. One comment cut straight to the heart of the problem:

“the people struggling are usually trying to do everything in one session and expecting the AI to hold 3 hours of context perfectly. it wont. break things up.”

That was it. I was treating my AI assistant like it had infinite memory. It doesn’t.

The Problem: Your AI Has Limited RAM

Large language models have a fixed context window—think of it like RAM in a computer. It’s finite, and as it fills up, earlier information degrades or gets pushed out entirely.

Here’s what context overload looks like in practice:

Hallucinated connections: The AI starts linking unrelated code because earlier context has become muddled. In my payment system, it tried to import Stripe utilities into a logging module that had nothing to do with payments.

Forgotten constraints: Style guidelines and patterns from the beginning of the session get ignored. My first-hour code used async/await. My third-hour code mysteriously switched to .then() chains.

Inconsistent output: Different parts of the same feature follow different patterns because the AI “forgot” what it did an hour ago.

Quality degradation: Code written after the context window fills tends to be less accurate, more error-prone, and requires more fixes.

The official Claude documentation explicitly warns about this:

“Avoid last 20% of context window for:

  • Large-scale refactoring
  • Feature implementation spanning multiple files
  • Debugging complex interactions”

I was living this warning without knowing it existed.

The Solution: Three Pillars of Context Management

After that Reddit thread, I restructured my entire AI coding workflow around three principles.

Pillar 1: One Task, One Session

The breakthrough insight from the Reddit discussion:

“biggest thing that made it click for me was treating each session as a focused task, not an open-ended conversation. one agent per feature, one agent per bug, one agent per test suite.”

Instead of one marathon session, I now break work into focused chunks:

BEFORE:
Session 1 (3 hours): "Build the entire payment processing system
with Stripe integration, webhook handling, invoice generation,
and refund logic."
AFTER:
Session 1: "Design the Payment model and database schema."
Session 2: "Implement Stripe payment intent creation."
Session 3: "Add webhook handler for payment events."
Session 4: "Build invoice generation service."
Session 5: "Write integration tests for payment flow."

Each session starts fresh. The AI focuses entirely on one piece of work without the noise of earlier, unrelated tasks.

I follow these scoping rules:

Task TypeSession Scope
FeatureOne feature = one session
Bug fixOne bug = one session
Test suiteOne test file/group = one session
RefactoringOne module/file = one session

Pillar 2: CLAUDE.md for Persistent Context

The Reddit thread also recommended something I’d never tried:

“also claude.md files. if you haven’t set one up yet do it today — saves you from re-explaining your project every session.”

A CLAUDE.md file lives in your project root. The AI reads it automatically at session start, giving it immediate context about your stack, conventions, and architecture.

Here’s what mine looks like for my e-commerce API:

# E-commerce Backend API
REST API using Express.js and PostgreSQL.
## Commands
| Command | Description |
|---------|-------------|
| `npm run dev` | Start dev server with hot reload |
| `npm test` | Run all tests |
| `npm run test:watch` | Run tests in watch mode |
| `npm run migrate` | Run pending migrations |
## Architecture

src/ ├── controllers/ # HTTP request handlers ├── services/ # Business logic layer ├── models/ # Sequelize models ├── middleware/ # Auth, validation, error handling └── utils/ # Shared utilities

## Code Style
- Use async/await (no .then chains)
- Services contain business logic; controllers are thin
- All errors use AppError class from utils/errors
- Use Zod for input validation
## Key Files
- `src/middleware/auth.ts` - JWT authentication
- `src/services/orderService.ts` - Order processing
## Gotchas
- Never modify deployed migrations
- Test database resets on each test run
- Rate limiting uses Redis at redis://localhost:6379

This simple file saves me from re-explaining the project every single session. The AI starts with context about my stack, conventions, and quirks.

Quality criteria I follow for CLAUDE.md:

  1. Commands documented: Build, test, dev commands—copy-pasteable
  2. Architecture explained: Directory structure with purpose
  3. Non-obvious patterns: Gotchas and quirks documented
  4. Concise: Every line adds value, no filler
  5. Current: Reflects the actual codebase state
  6. Actionable: Commands work without modification

Pillar 3: External Memory via Files

The third piece: stop asking the AI to remember things from earlier in the session. Use files instead.

Think of it this way:

Context Window = RAM (volatile, limited)
Filesystem = Disk (persistent, unlimited)

Before complex tasks, I create planning files that externalize memory:

task_plan.md
## Goal
Implement user search with pagination
## Phases
1. [ ] Design search query interface
2. [ ] Implement searchService with pagination
3. [ ] Add search controller endpoint
4. [ ] Write tests
## Decisions
- Use offset-based pagination (not cursor) for simplicity
- Max 100 results per page

I also keep a findings file during the session:

findings.md
- Existing search in productSearch.ts uses Elasticsearch
- User data is in PostgreSQL
- Need to join with user_settings table for preferences
- Missing index on email column - add migration

And a progress log:

progress.md
- 10:00 Started session
- 10:15 Completed phase 1 - interface designed
- 10:45 Phase 2 complete, found missing index
- 11:00 Added migration for email index
- 11:30 All phases complete, tests passing

When context degrades or I need to start a fresh session, the AI reads these files to catch up. No more “remember that function from two hours ago?” It can read the actual files.

Session Handoff Protocol

When I hit the context limit (or after about 2 hours), I follow a handoff protocol:

1. Update planning files:

  • Mark completed phases
  • Document discoveries
  • Log any errors and resolutions

2. Create a session summary:

session-summary-2026-03-19.md
## Completed
- Payment model with Stripe integration
- Webhook handler for payment_intent.succeeded
- Webhook handler for payment_intent.failed
## In Progress
- Invoice generation (60% done)
- File: src/services/invoiceService.ts
- Blocked on: PDF template rendering issue
## Next Session
- Finish invoice generation
- Then: write integration tests for payment flow

3. Start fresh:

"Read session-summary-2026-03-19.md and continue
from where we left off."

The new session starts clean with the summary file providing immediate context.

Common Mistakes I Made (So You Don’t Have To)

Mistake 1: The Never-Ending Session

“We’re on a roll, let’s just keep going!”

After 2-3 hours, context degrades. The AI is working with partial information and making more mistakes. I now set a timer and start fresh sessions rather than pushing through.

Mistake 2: No Persistent Context

I used to explain my project from scratch every session. This wasted tokens and provided inconsistent context each time. The CLAUDE.md file eliminated this entirely.

Mistake 3: Asking AI to Remember

“Remember that function from earlier? Modify it.”

The AI often couldn’t accurately recall code from earlier in a long session. Now I reference specific files: “Modify validateUser in src/services/authService.ts to add email verification.”

Mistake 4: One Giant Prompt

“Build a complete REST API with auth, rate limiting, logging, and tests.”

This overloaded context immediately and produced unreliable results. I now write a spec file first, then implement one piece per session.

Mistake 5: Ignoring Context Warnings

Most AI coding tools show context usage. I used to ignore this. Now I monitor the percentage—when approaching 80%, I start planning the session handoff.

What Changed for Me

After adopting this approach:

  • Fewer hallucinations: The AI stops making false connections between unrelated code
  • Consistent output: Code follows the same patterns throughout the project
  • Faster sessions: Less time re-explaining context, more time building
  • Better quality: Code produced in focused sessions needs fewer fixes
  • Easier debugging: When something goes wrong, I know exactly which session produced it

The key insight that transformed my workflow:

“treat each session as a focused task, not an open-ended conversation”

My AI assistant isn’t a single omniscient entity—it’s a series of focused, well-contextualized workers. Each one starts fresh, reads the relevant context files, does one job well, and hands off cleanly to the next.

That three-hour payment system? I broke it into five focused sessions. Total time was similar, but the code was cleaner, I made fewer fixes, and the AI never hallucinated parameters or forgot constraints.

Context management is the difference between AI coding success and frustration. Treat each session as a focused task, use CLAUDE.md files for persistent project memory, and externalize session state to planning files. Your AI assistant will stay sharp, consistent, and reliable throughout your development workflow.

Final Words + More Resources

My intention with this article was to help others share my knowledge and experience. If you want to contact me, you can contact by email: Email me

Here are also the most important links from this article along with some further resources that will help you in this scope:

Oh, and if you found these resources useful, don’t forget to support me by starring the repo on GitHub!

Comments