Advanced Claude Code Techniques: Master Pro-Level Patterns
By Learnia AI Research Team
Advanced Claude Code Techniques
You've mastered the basics. Now it's time to unlock Claude Code's full potential with techniques used by the most productive developers. This guide covers context engineering, advanced prompting patterns, cost optimization, and the strategic patterns that separate power users from beginners.
1. Context Engineering: The Meta-Skill
Context is everything in Claude Code. The quality of your output is directly proportional to the quality of your context. This section teaches you to engineer context like a pro.
Understanding the Context Window
Claude Code has a 200K token context window. Think of it as RAM for your AI assistant:
| Usage | Zone | Visual | Action |
|---|---|---|---|
| 0-50% | Green | ๐ข | Work normally |
| 50-75% | Yellow | ๐ก | Consider /compact soon |
| 75-90% | Red | ๐ด | Use /compact now |
| 90%+ | Critical | โซ | Session may crashโact immediately |
What Consumes Context?
Not all actions are equal. Here's the real cost breakdown:
Interactive Context Visualizer
Use this interactive tool to visualize how different activities consume your 200K token context window:
Context Depletion Symptoms
Learn to recognize when context is running low:
The Compact Command Deep Dive
When you run /compact, Claude:
- โSummarizes the conversation so far
- โPreserves essential info (project structure, current task, recent decisions)
- โDiscards verbose details (old diffs, lengthy outputs, resolved discussions)
- โFrees typically 40-60% of context
2. Session vs. Persistent Memory
Claude Code has two distinct memory systems. Understanding the difference is crucial:
The CLAUDE.md Power Pattern
CLAUDE.md is your persistent project memory. Claude reads it automatically on every launch.
# CLAUDE.md
## Project: E-Commerce Platform
Tech stack: Next.js 14, TypeScript, Prisma, PostgreSQL
## Decisions Made
- JWT auth (not sessions) for horizontal scaling
- Server Components by default, Client only when needed
- All API routes return Result<T, AppError> pattern
## Coding Standards
- No `any` types - use `unknown` with type guards
- All functions must have JSDoc comments
- Tests required for all business logic
## Current Sprint Focus
- User authentication system (70% complete)
- Next: Implement refresh token rotation
Build Your Perfect CLAUDE.md
Use this interactive editor to create a customized CLAUDE.md for your project. Choose from templates and customize to your needs:
Session Handoff Pattern
When ending a session, create a handoff document to maintain continuity:
# Session Handoff - 2025-01-15 18:00
## Accomplished
- Implemented JWT access token generation
- Added /auth/login endpoint
- Created User model with Prisma
## Current State
- Login works for valid credentials
- Refresh tokens: NOT YET IMPLEMENTED
- Tests: 80% coverage on auth module
## Next Session
1. Implement refresh token generation
2. Add token rotation on refresh
3. Write integration tests for full flow
## Key Files
- src/auth/tokens.ts (main logic)
- src/routes/auth.ts (endpoints)
- prisma/schema.prisma (User model)
3. XML-Structured Prompts
For complex requests, XML tags provide semantic organization that helps Claude distinguish between instructions, context, and constraints.
Basic Structure
<instruction>
Your main task description here
</instruction>
<context>
Background information, project details
</context>
<constraints>
- Limitation 1
- Requirement 2
</constraints>
<output>
Expected format or structure
</output>
When to Use XML Tags
Real-World Example
Here's a production-quality prompt for security review:
<instruction>
Review this authentication middleware for security vulnerabilities
</instruction>
<context>
Financial application handling sensitive user data.
Must follow OWASP Top 10 and PCI DSS compliance.
</context>
<code>
async function authenticate(req, res, next) {
const token = req.headers.authorization?.split(' ')[1];
if (!token) return res.status(401).json({ error: 'No token' });
const decoded = jwt.verify(token, process.env.JWT_SECRET);
req.user = decoded;
next();
}
</code>
<constraints>
- Point out security risks with severity ratings
- Consider timing attacks and token leakage
- Suggest PCI DSS compliant alternatives
</constraints>
<output>
1. Security issues found (Critical/High/Medium/Low)
2. Specific code fixes with examples
3. Additional hardening recommendations
</output>
4. Semantic Anchors
Semantic anchors are precise technical terms that activate specific patterns in Claude's training data. Vague terms produce generic code; precise anchors produce expert-level implementations.
The Precision Difference
How to Apply Semantic Anchors
Add them to your CLAUDE.md:
# Architecture Principles
Follow these patterns explicitly:
- **Architecture**: Hexagonal Architecture (Ports & Adapters)
- **Error handling**: Railway Oriented Programming - return Result<T, E>
- **Testing**: TDD London School - mock collaborators, test behaviors
- **Documentation**: ADR (Architecture Decision Records) for choices
Or use them inline:
Refactor the user service following Domain-Driven Design (Evans).
Apply Repository pattern for persistence and ensure
Screaming Architecture where package structure reveals intent.
5. Plan Mode & OpusPlan
Plan Mode is Claude Code's "look but don't touch" modeโessential for safe exploration and strategic thinking.
Entering Plan Mode
/plan
Or simply ask Claude:
Let's plan this feature before implementing
What Plan Mode Allows vs. Prevents
OpusPlan Mode: Best of Both Worlds
OpusPlan uses Opus for planning (superior reasoning) and Sonnet for implementation (cost-efficient).
/model opusplan
The Workflow:
6. Rev the Engine
For critical decisions, run multiple rounds of planning before executing.
The Pattern
Standard workflow: think โ plan โ execute
Rev the Engine: think โ plan โ think harder โ refine โ think hardest โ finalize โ execute
When to Use
- โCritical architectural decisions (irreversible, high-impact)
- โComplex migrations affecting 10+ files
- โUnfamiliar domains where first instincts are often wrong
Three-Round Example
## Round 1: Initial Analysis
User: /plan
User: Analyze the current auth system. What are the key components,
dependencies, and risks of migrating to OAuth2?
Claude: [Initial analysis]
## Round 2: Deep Challenge
User: Now challenge your own analysis:
- What assumptions did you make?
- What failure modes did you miss?
- What would a senior security engineer flag?
Claude: [Deeper analysis with self-correction]
## Round 3: Final Plan
User: Based on both rounds, write the definitive migration plan.
Include rollback strategy and risk mitigation.
Claude: [Refined plan incorporating both rounds]
## Execute
User: /execute
User: Implement the plan from round 3.
7. Cost Optimization
Claude Code uses API credits. Understanding costs helps optimize without sacrificing productivity.
Pricing Reality Check
| Model | Input (per 1M tokens) | Output (per 1M tokens) |
|---|---|---|
| Sonnet 3.5 (default) | $3.00 | $15.00 |
| Opus 4 | $15.00 | $75.00 |
| Haiku 3.5 | $0.80 | $4.00 |
Typical session costs:
- โQuick task (5-10 min): $0.05-$0.10
- โFeature work (1-2 hours): $0.20-$0.50
- โDeep refactor (half day): $1.00-$2.00
The Expensive Operations
Cost Optimization Strategies
Track Your Costs
Check current session cost anytime:
/status
Output:
Session Status
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
Context: 45% used (90K / 200K tokens)
Cost: $0.23 this session
Duration: 1h 23m
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
8. Prompting as Provocation
The Claude Code team internally treats prompts as challenges to a peer, not instructions to an assistant. This subtle shift produces higher-quality outputs.
Three Provocation Patterns
9. Fresh Context Pattern
For long autonomous runs, consider the Fresh Context Pattern (also called the Ralph Loop).
The Problem: Context Rot
Research shows LLM performance degrades with accumulated context:
- โ20-30% performance gap between focused and polluted prompts
- โDegradation starts around 16K tokens
- โFailed attempts and error traces dilute attention
The Solution
Instead of managing context within a session, restart with a fresh session per task:
# The Ralph Loop
while :; do cat TASK.md PROGRESS.md | claude -p ; done
State persists via:
- โ
TASK.mdโ Current task definition - โ
PROGRESS.mdโ Learnings, completed work, blockers - โGit commits โ Each iteration commits atomically
When to Use Fresh Context
| Situation | Approach |
|---|---|
| Interactive exploration | Traditional (stay in session) |
| Context 70-90% | /compact |
| Autonomous run > 1 hour | Fresh Context Pattern |
| Clear success criteria (tests pass) | Fresh Context Pattern |
| Overnight/AFK execution | Fresh Context Pattern |
10. Advanced Workflows Summary
The Expert's Decision Tree
Is this a simple, reversible change?
โโโ YES โ Just execute (no planning overhead)
โโโ NO โ How impactful is this decision?
โโโ Medium โ Plan Mode + Execute
โโโ High โ OpusPlan (Opus thinks, Sonnet implements)
โโโ Critical โ Rev the Engine (3 rounds) + OpusPlan
Quick Reference
| Technique | When to Use | Command/Pattern |
|---|---|---|
/compact | Context > 50% | /compact |
| Plan Mode | Safe exploration | /plan |
| OpusPlan | Best reasoning + cost efficiency | /model opusplan |
| Rev the Engine | Critical decisions | 3 rounds in Plan Mode |
| Fresh Context | Long autonomous runs | `while :; do cat TASK.md |
| XML Prompts | Complex multi-part requests | <instruction>, <context>, <constraints> |
| Semantic Anchors | Expert-level output | Use precise technical terms |
What's Next?
You now have the advanced techniques that separate power users from beginners. Practice these patterns and they'll become second nature.
Continue to Module 1 for hands-on exercises applying these techniques to real projects.
This article is part of the Claude Code Ultimate Guide series. Continue your journey with Module 1: Core Techniques for deeper practice.
โRelated Articles
FAQ
What is context engineering in Claude Code?+
Context engineering is the practice of optimizing what information Claude sees. This includes CLAUDE.md structure, file references, context compaction, and strategic use of the 200K token window.
What are XML-structured prompts?+
XML tags like <requirements>, <constraints>, <examples> help Claude parse complex instructions. They provide clear semantic boundaries and improve response accuracy for multi-part requests.
What is OpusPlan mode?+
OpusPlan triggers Claude to create a detailed execution plan before acting. Use it for complex tasks: 'OpusPlan: Refactor the authentication system to use JWT tokens.'
How can I reduce Claude Code costs?+
Use /compact to summarize context, be specific in requests to reduce iterations, use cheaper models (Haiku) for simple tasks, and structure CLAUDE.md efficiently.