Context Management Basics
10 examples to get you started with Claude Code context management - 7 basic and 3 intermediate.
Prerequisites
- Claude Code CLI installed and running inside a project directory (a git repo is not required, but recommended).
- No extra packages to install - CLAUDE.md is just a markdown file, and
/init,@mentions,/compact, and/clearare all built into Claude Code itself.
Basic Examples
1. Generate a starter CLAUDE.md with /init
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:
/init- Claude Code scans the project (package files, folder layout, existing docs) and writes a first-pass
CLAUDE.md. - The generated file is a starting point, not a finished document - always read it and correct anything wrong or missing.
- Running
/initagain 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.
2. Write a minimal CLAUDE.md by hand
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`.- Place this file at the root of your project as
CLAUDE.md. - Every bullet should be a concrete, checkable rule, not a vague preference.
- Commit it to version control so the whole team shares the same project memory.
Related: What CLAUDE.md Actually Does - why this file matters.
3. Point Claude Code at one file with @mentions
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.@mentionsskip the guesswork of Claude Code searching for the right file - you name it directly.- They work for any file in the project, not just source code - config files and docs too.
- Overusing
@mentionsfor 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.
4. Free up space mid-session with /compact
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/compactsummarizes the conversation so far, keeping the relevant parts and dropping the rest.- Use it when you want to keep working on the same task but the session has accumulated a lot of exploratory back-and-forth.
- It is not lossless - very specific earlier details can get smoothed over in the summary.
Related: Why /compact and /clear Exist - when to reach for each one.
5. Start over cleanly with /clear
When a conversation has drifted or context has gone stale, wipe it and start fresh.
# inside a Claude Code session:
/clear/cleardiscards the conversation history entirely - CLAUDE.md is still reloaded fresh at the start of the new session.- Use it when switching to an unrelated task, rather than dragging irrelevant history along.
- Unlike
/compact, nothing from the old conversation carries forward.
Related: Why /compact and /clear Exist - the full comparison.
6. Add a project-wide rule Claude Code should never break
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.- A dedicated "Hard Rules" section makes non-negotiables easy to scan versus general style preferences.
- Pair critical rules with an actual enforced check (CI, a pre-commit hook) - CLAUDE.md is guidance the agent reads, not a hard technical guardrail.
- Keep the list short - a handful of true hard rules are more likely to be followed than fifty of them.
Related: Writing a CLAUDE.md File That Actually Gets Followed - the full pattern.
7. Reference multiple related files at once
@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.- Chaining a few targeted
@mentionsis far cheaper on context than asking Claude Code to search the whole codebase. - Keep the list to the files that actually matter for the task - each mention adds its full content to context.
- For a whole folder's worth of related files, describing the pattern in prose is often cheaper than mentioning every file individually.
Related: Referencing Specific Files with @mentions - more on scoping mentions.
Intermediate Examples
8. Layer a nested CLAUDE.md for one subfolder
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.- The root
CLAUDE.mdstill applies everywhere - this file only adds detail relevant tobackend/. - Nested files are useful once a project has genuinely distinct areas (frontend vs backend, packages in a monorepo).
- Keep nested rules non-contradictory with the root file - conflicting guidance forces the agent to guess which one wins.
Related: Nested CLAUDE.md Files and How Scope Inheritance Works - the full mechanics.
9. Diagnose a session that has gotten sluggish
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?- Large files read in full, excessive
@mentions, and verbose tool output are the usual culprits behind context bloat. /compactis the right move when the task is still in progress and you want to keep going./clearis 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.
10. Combine CLAUDE.md, @mentions, and /clear across a task
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- CLAUDE.md supplies the standing conventions,
@mentionsnarrow the request to the exact file, and/clearresets once the task is complete. - This combination keeps each session focused: broad project memory plus a tight, current task scope.
- Avoid letting one long session accumulate several unrelated tasks - it is what makes context bloat and confused answers more likely.
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.