Intermediate15 min1 prerequisite

Understand how Claude Code reads, indexes, and works with your project's files and structure.

Project Context

Claude Code's effectiveness depends on understanding your project. This lesson covers how it builds context and how you can optimize it.

Automatic Context Discovery

Initial Scan

When you start Claude Code, it automatically:

  1. Scans directory structure: Understands project layout
  2. Identifies project type: Detects frameworks and languages
  3. Reads config files: package.json, tsconfig.json, etc.
  4. Notes patterns: Coding style, file organization

What Gets Indexed

Terminal
Indexed automatically:
├── Source code files (.ts, .js, .py, etc.)
├── Configuration files (package.json, etc.)
├── Documentation (README, docs/)
├── Test files
└── Type definitions

Not indexed (by default):
├── node_modules/
├── .git/
├── Build outputs (dist/, build/)
├── Binary files
└── Large data files

Controlling Context

The .claudeignore File

Like .gitignore, create .claudeignore to exclude files:

Terminal
# Dependencies
node_modules/
vendor/
.venv/

# Build outputs
dist/
build/
.next/
out/

# Large files
*.csv
*.sql
data/

# Sensitive files
.env*
secrets/
*.pem

# Generated code
*.generated.ts
coverage/

Why Exclude Files?

  1. Performance: Smaller context = faster responses
  2. Relevance: Fewer distractions for the AI
  3. Cost: Less token usage
  4. Security: Keep secrets out of context

Project Instructions

Creating Instructions

The .claude/instructions.md file guides Claude Code:

Terminal
# Project: E-Commerce Platform

## Overview
This is a Next.js 14 e-commerce application with Stripe payments.

## Tech Stack
- Next.js 14 (App Router)
- TypeScript (strict mode)
- Tailwind CSS + shadcn/ui
- Prisma + PostgreSQL
- NextAuth.js for auth
- Stripe for payments

## Project Structure
- `/app` - Next.js pages and API routes
- `/components` - React components
- `/lib` - Shared utilities
- `/prisma` - Database schema

## Coding Conventions

### Components
- Use Server Components by default
- Add 'use client' only when needed
- Follow the component structure in /components/ui

### API Routes
- Validate input with Zod
- Use try/catch for error handling
- Return consistent response shapes

### Database
- Use Prisma for all database access
- Never write raw SQL
- Include relations in queries when needed

## Common Tasks

### Adding a new page
1. Create file in /app/[route]/page.tsx
2. Add server-side data fetching
3. Use components from /components

### Adding an API endpoint
1. Create route.ts in /app/api/[route]
2. Add Zod schema for validation
3. Include auth check if needed

## Important Notes
- All prices are in cents
- User IDs are UUIDs, not integers
- Use UTC for all timestamps

What to Include

Good instructions cover:

  • Tech stack: Frameworks, libraries, versions
  • Structure: Where things live
  • Conventions: Coding patterns
  • Domain knowledge: Business rules
  • Common tasks: How-to guides

Context Commands

View Current Context

Terminal
/context

Shows what files Claude Code has read.

Read Specific Files

Terminal
/read src/lib/auth.ts
/read package.json

Explicitly add files to context.

Search the Codebase

Terminal
/search authentication

Find relevant files by content.

Show Project Structure

Terminal
/tree

Display directory tree.

Terminal
/tree src/components

Show specific directory.

Context During Conversations

Automatic Context Building

As you work, Claude Code adds relevant context:

Terminal
You: Fix the bug in the checkout flow

Claude Code: [Automatically reads]
- src/app/checkout/page.tsx
- src/components/CheckoutForm.tsx
- src/lib/stripe.ts
- src/app/api/checkout/route.ts

Analyzing the checkout flow...

Referencing Files

Use @ for explicit references:

Terminal
Look at @src/lib/utils.ts and @src/types/index.ts

Context Limits

Claude Code has a context window limit. When you hit it:

Terminal
Claude Code: Context is getting full. Would you like me to:
1. Summarize and compact the conversation
2. Focus on specific files only
3. Start a fresh conversation

[1/2/3]?

Optimizing Context

Keep Instructions Focused

Too long = less effective:

Terminal
#  Too verbose
Here is every detail about the project...
[2000 lines of documentation]

#  Focused
## Tech Stack
[Key technologies]

## Conventions
[Important patterns]

## Notes
[Critical information]

Structure for Scanning

Claude Code scans quickly. Use clear organization:

Terminal
#  Walls of text
The project uses Next.js which is a React framework and we also use TypeScript for type safety and Tailwind CSS for styling and Prisma for the database and...

#  Scannable structure
## Tech Stack
- **Frontend**: Next.js 14, React 18
- **Styling**: Tailwind CSS
- **Database**: Prisma + PostgreSQL
- **Auth**: NextAuth.js

Include Examples

Show, don't just tell:

Terminal
## API Response Format

All API routes return:
```json
{
  "success": true,
  "data": { ... }
}

Or on error:

Terminal
{
  "success": false,
  "error": "Error message"
}
Terminal

## Multiple Projects

### Workspace Configuration

For monorepos or multi-project setups:

my-workspace/ ├── .claude/ │ └── instructions.md # Workspace-level instructions ├── frontend/ │ └── .claude/ │ └── instructions.md # Frontend-specific ├── backend/ │ └── .claude/ │ └── instructions.md # Backend-specific └── shared/

Terminal

### Switching Context

You: Now let's work on the backend

Claude Code: Switching context to /backend [Reads backend-specific instructions]

Terminal

Or start in a specific directory:

```bash
cd backend
claude

Context Best Practices

1. Start with Overview

Begin with the big picture:

Terminal
# E-Commerce Platform

A Next.js application selling digital products with subscription plans.

## Key Flows
1. User browses products
2. User adds to cart
3. User checks out (Stripe)
4. User accesses purchased content

2. Document the Non-Obvious

Focus on what Claude Code can't infer:

Terminal
## Business Rules

- Free users limited to 3 downloads/month
- Pro users get unlimited downloads
- Enterprise users can add team members
- Refunds allowed within 7 days

## Database Notes

- `users.plan` is denormalized for query speed
- Soft delete: check `deleted_at` is null
- All timestamps are UTC

3. Keep It Updated

Instructions should evolve with your project:

Terminal
# Add to your workflow
git commit -am "Add subscription feature"
# Also update .claude/instructions.md

Troubleshooting Context

"Claude Code doesn't understand my project"

  1. Check .claudeignore isn't excluding important files
  2. Add explicit instructions in .claude/instructions.md
  3. Use /read to explicitly add key files
  4. Start with "Let me explain this project..."

"Context is too full"

  1. Add more patterns to .claudeignore
  2. Use /compact to summarize conversation
  3. Start fresh sessions for new topics
  4. Be more selective with file references

"Responses miss project conventions"

  1. Add conventions to instructions file
  2. Reference example files that show patterns
  3. Explicitly mention "Follow our patterns in..."

Summary

  • Automatic scanning: Claude Code indexes your project
  • .claudeignore: Exclude unnecessary files
  • .claude/instructions.md: Project-specific guidance
  • Context commands: /read, /tree, /search
  • @ references: Explicitly include files
  • Keep focused: Shorter, clearer instructions work better

Next Steps

With project context understood, let's explore agentic workflows—how Claude Code autonomously completes complex tasks.

Mark this lesson as complete to track your progress