Context Management Basics
10 examples to get you started with Claude Code context management - 7 basic and 3 intermediate.
Search across all documentation pages
10 examples to get you started with Claude Code context management - 7 basic and 3 intermediate.
/init, @mentions, /compact, and /clear are all built into Claude Code itself.Run /init inside a fresh or existing project so Claude Code drafts a starting CLAUDE.md for you.
cd my-project
claude
# inside the Claude Code session:
/initCLAUDE.md./init again on a project that already has a CLAUDE.md will update it rather than duplicate work from scratch.Related: Bootstrapping a New Project with /init - a full walkthrough of this command.
A CLAUDE.md file needs no special syntax - it is markdown Claude Code reads automatically at the start of each session.
# Project: Order Service
- Language: TypeScript, strict mode.
- Tests live in `__tests__/`, mirroring the source structure.
- Never edit files under `generated/` - they are rebuilt by `npm run codegen`.CLAUDE.md.Related: What CLAUDE.md Actually Does - why this file matters.
Use @ followed by a path to reference an exact file instead of describing it in prose.
# in a Claude Code prompt:
Fix the bug in @src/utils/formatDate.ts where timezones aren't applied.@mentions skip the guesswork of Claude Code searching for the right file - you name it directly.@mentions for large files can bloat the context window fast, since the full file content gets pulled in.Related: Referencing Specific Files with @mentions - deeper patterns for this.
When a long conversation starts to feel sluggish or the context window is filling up, condense it instead of losing everything.
# inside a Claude Code session:
/compact/compact summarizes the conversation so far, keeping the relevant parts and dropping the rest.Related: Why /compact and /clear Exist - when to reach for each one.
When a conversation has drifted or context has gone stale, wipe it and start fresh.
# inside a Claude Code session:
/clear/clear discards the conversation history entirely - CLAUDE.md is still reloaded fresh at the start of the new session./compact, nothing from the old conversation carries forward.Related: Why /compact and /clear Exist - the full comparison.
Concrete, non-negotiable rules belong in CLAUDE.md as explicit statements, not implied conventions.
## Hard Rules
- Never commit directly to `main` - always open a pull request.
- Never modify `.env.production` - ask the user first.
- Always run `npm test` before declaring a task finished.Related: Writing a CLAUDE.md File That Actually Gets Followed - the full pattern.
@mentions can be chained when a task genuinely spans several files.
# in a Claude Code prompt:
Compare @src/api/userController.ts with @src/api/orderController.ts
and make the error handling consistent between them.@mentions is far cheaper on context than asking Claude Code to search the whole codebase.Related: Referencing Specific Files with @mentions - more on scoping mentions.
A subfolder can carry its own CLAUDE.md that adds rules on top of the root file, scoped to just that part of the codebase.
<!-- backend/CLAUDE.md -->
# Backend-specific rules
- All database queries go through the `repositories/` layer - never query directly from a route handler.
- Use `zod` schemas to validate every external input before it reaches business logic.CLAUDE.md still applies everywhere - this file only adds detail relevant to backend/.Related: Nested CLAUDE.md Files and How Scope Inheritance Works - the full mechanics.
Before reaching for /compact or /clear, figure out what is actually consuming context.
# a few questions to answer before deciding what to do:
# - Have I @mentioned any very large files repeatedly?
# - Has the conversation run many exploratory searches with verbose output?
# - Is CLAUDE.md itself unusually long?@mentions, and verbose tool output are the usual culprits behind context bloat./compact is the right move when the task is still in progress and you want to keep going./clear is the right move when you are switching tasks entirely and the old history is no longer relevant.Related: Auditing What's Eating Your Claude Code Context Window - a full audit workflow.
A realistic workflow chains all three tools together rather than relying on just one.
# CLAUDE.md already documents the project's conventions, so:
Refactor @src/services/payment.ts to use the retry pattern
described in CLAUDE.md under "Error Handling".
# ...later, once that task is done and merged:
/clear
# start the next, unrelated task with a clean session@mentions narrow the request to the exact file, and /clear resets once the task is complete.Related: Context Management Best Practices - patterns that tie all of this together.
Stack versions: Written against the Claude model lineup current as of ~June 2026 - Claude Fable 5, Claude Opus 4.8, Claude Sonnet 5 (the default), and Claude Haiku 4.5. Model names, pricing, and product features move quickly - verify current specifics at platform.claude.com/docs before relying on them.
Reviewed by Chris St. John·Last updated Jul 18, 2026