Loading lesson…
Loading lesson…
While using Claude Code, you may encounter various issues. This section summarizes common problems and solutions.
Fast token consumption is one of the most common issues. Below is a complete optimization strategy.
Diagnosis:
First run /context to inspect current token usage:
/context
Focus on:
Optimization strategy:
1. Improve .claudeignore
Make sure .claudeignore includes unnecessary files:
# must ignore
node_modules/
dist/
build/
*.log
.env
# project-specific
# React
.next/
out/
# Vue
.nuxt/
.output/
# generic
.vscode/
.idea/
coverage/
*.min.js
*.bundle.js
2. Compress context regularly
Long conversations accumulate many tokens. It is recommended to run /compact every 5-6 rounds:
# after long conversation
/compact
# continue
Now let's implement order module...
3. Reference files precisely
Avoid referencing entire directory if not needed:
# not recommended
@src/ Explain this code
# recommended
@src/utils/auth.ts @src/components/Login.tsx Explain login flow
4. Avoid reading huge files
If /context shows one file consuming many tokens, consider:
If Claude answers inaccurately or repeatedly asks basic project info, it lacks project context.
Solutions:
1. Generate CLAUDE.md
Run /init to generate project config:
/init
After generation, validate:
2. Manually edit CLAUDE.md
If auto-generated config is not detailed enough, add:
## Project-Specific Information
### Architecture Decisions
- Why choose X over Y?
- What are core design patterns?
### Common Pitfalls
- When using useEffect, watch out for...
- DB queries must...
### Third-Party Integrations
- Payments via Stripe
- Email via SendGrid
- File storage via AWS S3
3. Use Rules directory
For large projects, organize conventions in Rules:
.claude/rules/
├── 00-architecture.md # architecture overview
├── 01-coding-style.md # coding style
├── 10-frontend.md # frontend rules
├── 11-backend.md # backend rules
└── 20-testing.md # testing rules
4. Add context in prompt when needed
For specific tasks, append relevant background:
We use a custom useAuth Hook for authentication.
It returns { user, login, logout, isLoading }.
Please build a user-menu component based on this Hook.
Claude Code provides multiple rollback mechanisms for different scenarios.
Scenario 1: rollback conversation state
If you only mistyped or dislike response:
Double Esc -> rollback previous turn
Triple Esc -> clear all conversation history
Note: this only rolls back conversation state, not file edits.
Scenario 2: undo file edits
If Claude already modified files, undo manually:
# check changes
!git status
!git diff
# revert one file
git checkout -- src/utils/helpers.ts
# revert all working tree changes
git checkout -- .
# if already committed
# soft rollback (keep changes)
git reset --soft HEAD~1
# hard rollback (discard changes)
git reset --hard HEAD~1
Scenario 3: preventively use Git workflow
Best practice: save current work before Claude session:
# save current state before starting
git add .
git commit -m "WIP: before Claude Code session"
# or use stash
git stash push -m "before claude"
# develop with Claude Code...
# if result is unsatisfactory, full rollback
git reset --hard HEAD~1
# or
git stash pop
Frequent permission confirmations hurt efficiency. Proper permission config can make workflow smoother.
Permission model:
Claude Code permissions are three levels:
Optimization config:
Edit .claude/settings.json:
{
"permissions": {
"allow": [
// Git read operations
"Bash(git status)",
"Bash(git log:*)",
"Bash(git diff:*)",
"Bash(git branch)",
// test and checks
"Bash(npm test:*)",
"Bash(npm run lint:*)",
"Bash(npm run typecheck)",
// dev server
"Bash(npm run dev:*)",
// source edits
"Edit(src/**/*.{ts,tsx})",
"Edit(tests/**/*.test.ts)",
"Write(src/**/*.ts)"
],
"ask": [
// Git write operations
"Bash(git commit:*)",
"Bash(git push:*)",
"Bash(git pull:*)",
// package management
"Bash(npm install:*)",
"Bash(npm uninstall:*)",
// build and deployment
"Bash(npm run build)",
"Bash(npm run deploy:*)",
// config file edits
"Edit(package.json)",
"Edit(tsconfig.json)",
// sensitive file reads
"Read(.env)",
"Read(config/secrets.*)"
],
"deny": [
// dangerous commands
"Bash(rm -rf:*)",
"Bash(sudo:*)",
"Bash(curl * | sh)",
"Bash(wget * | sh)",
// system files
"Edit(/etc/*)",
"Write(/usr/*)",
// Git internals
"Edit(.git/*)"
]
}
}
Progressive permission strategy: