- Learn
- AI Coding Agents
- Claude Code
- Project Context
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:
- Scans directory structure: Understands project layout
- Identifies project type: Detects frameworks and languages
- Reads config files: package.json, tsconfig.json, etc.
- Notes patterns: Coding style, file organization
What Gets Indexed
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:
# 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?
- Performance: Smaller context = faster responses
- Relevance: Fewer distractions for the AI
- Cost: Less token usage
- Security: Keep secrets out of context
Project Instructions
Creating Instructions
The .claude/instructions.md file guides Claude Code:
# 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
/context
Shows what files Claude Code has read.
Read Specific Files
/read src/lib/auth.ts
/read package.json
Explicitly add files to context.
Search the Codebase
/search authentication
Find relevant files by content.
Show Project Structure
/tree
Display directory tree.
/tree src/components
Show specific directory.
Context During Conversations
Automatic Context Building
As you work, Claude Code adds relevant context:
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:
Look at @src/lib/utils.ts and @src/types/index.ts
Context Limits
Claude Code has a context window limit. When you hit it:
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:
# ❌ 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:
# ❌ 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:
## API Response Format
All API routes return:
```json
{
"success": true,
"data": { ... }
}
Or on error:
{
"success": false,
"error": "Error message"
}
## 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/
### Switching Context
You: Now let's work on the backend
Claude Code: Switching context to /backend [Reads backend-specific instructions]
Or start in a specific directory:
```bash
cd backend
claude
Context Best Practices
1. Start with Overview
Begin with the big picture:
# 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:
## 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:
# Add to your workflow
git commit -am "Add subscription feature"
# Also update .claude/instructions.md
Troubleshooting Context
"Claude Code doesn't understand my project"
- Check
.claudeignoreisn't excluding important files - Add explicit instructions in
.claude/instructions.md - Use
/readto explicitly add key files - Start with "Let me explain this project..."
"Context is too full"
- Add more patterns to
.claudeignore - Use
/compactto summarize conversation - Start fresh sessions for new topics
- Be more selective with file references
"Responses miss project conventions"
- Add conventions to instructions file
- Reference example files that show patterns
- 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.