agentleFS
VersoXBTVersoXBT/claude-initial-setup/skills/memory-management/context-optimization/SKILL.md

context-optimization

Skill4 starsChanged 9 months agoMITView on GitHub

Strategies for managing Claude Code's context window efficiently: token budgeting, progressive disclosure, reference files, and using subagents to protect context. Use when conversations are getting long, the user mentions context limits, or tasks involve reading many files.

14 other projects use a version of this.

Adopted by

Most recently taken up by

Used with

Projects with it also use bdi-mental-states, filesystem-context and 10 more.

Other files people use alongside this one.

Files found in the same public projects as a version of this one, more often than they turn up anywhere else. Each project counts once, large collections count for less, and this file's own repository is left out.
  • Downey6689Skill

    bdi-mental-states

    Transforms external RDF context into BDI mental states. Invoke when modeling beliefs, desires, and intentions.

    Also in 4 projects that use this

  • Downey6689Skill

    filesystem-context

    Uses filesystem for dynamic context discovery. Invoke when organizing docs or loading information just-in-time.

    Also in 4 projects that use this

  • AbdullahMalik17Skill

    configuring-dapr-pubsub

    Configures Dapr pub/sub components for event-driven microservices with Kafka or Redis. Use when wiring agent-to-agent communication, setting up event subscriptions, or integrating Dapr sidecars. Covers component configuration, subscription patterns, publishing events, and Kubernetes deployment. NOT when using direct Kafka clients or non-Dapr messaging patterns.

    Also in 3 projects that use this

  • AbdullahMalik17Skill

    containerizing-applications

    Containerizes applications with Docker, docker-compose, and Helm charts. Use when creating Dockerfiles, docker-compose configurations, or Helm charts for Kubernetes. Includes Docker Hardened Images (95% fewer CVEs), multi-stage builds, and 15+ battle-tested gotchas.

    Also in 3 projects that use this

  • xfstudioSkill

    conversation-memory

    Persistent memory systems for LLM conversations including short-term, long-term, and entity-based memory Use when: conversation memory, remember, memory persistence, long-term memory, chat history.

    Also in 3 projects that use this

  • muratcankoylanSkill

    long-horizon-prompting

    This skill should be used when writing, enhancing, or evaluating the launch prompt for a long-running autonomous agent or a parallel multi-agent orchestration attacking a hard problem: pseudo-formal task briefs that define terms and an exact success predicate linguistically, enumerate non-counting outcomes, set persistence rules with explicit stop and return conditions and effort floors, manage a diverse portfolio of parallel approaches with an approach registry and blocked-route bookkeeping, and gate the return on adversarial audit. Route agent topology and coordination protocols to multi-agent-patterns, runtime control surfaces and loop governance to harness-engineering, evaluator and quality-gate construction to evaluation, judge design to advanced-evaluation, and compaction or memory mechanics to context-compression and memory-systems.

    Also in 3 projects that use this

  • +6 more from AbdullahMalik17

The file

We split this skill into the rules it gives an agent

8 rules. 0 are common, 8 are distinctive, and 0 are very specific to this project.

  • 0

    Common

    Most of the 13 edited versions we read keep these.

  • 8

    Distinctive

    This author's own choices. Worth a look.

  • 0

    Very specific

    Tied to their project's names, files or tools. Probably skip.

  • Common
  • Distinctive
  • Very specific
  1. Distinctive:

    Start with structure, then drill into details:

    line 44
  2. Distinctive:

    Do not read all source files upfront.

    line 60
  3. Distinctive:

    When a file's contents have been read and discussed, refer to it by path and line number instead of quoting large blocks:

    line 64
  4. Distinctive:

    Use subagents when exploration would consume too much main context:

    line 77
  5. Distinctive:

    Use Grep to locate exactly what you need before reading entire files:

    line 99
  6. Distinctive:

    Reading everything upfront: Do not read all project files at the start of a session.

    line 153
  7. Distinctive:

    Repeating tool calls: If you already read a file earlier in the session, do not read it again.

    line 156
  8. Distinctive:

    Rule: Read only what you need, when you need it.

    line 170

“Common” is measured: kept by at least 6 in 10 of the 13 edited versions of this file we could read. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.

Keep what fits and drop the rest. Your version, ready for your agents.
Files
SKILL.md

Context Optimization

Claude Code's context window is finite. Efficient use of context enables longer sessions, larger refactors, and deeper analysis without hitting limits or losing important earlier information.

When to Use

  • Conversation is approaching context limits (system may compress earlier messages)
  • Task requires reading many large files
  • User mentions running out of context or losing earlier information
  • Planning a large refactoring or multi-file implementation
  • Deciding whether to use subagents vs direct tool calls

Core Patterns

Token Budgeting

Estimate context costs before reading files:

Small file (< 100 lines):   ~500-1000 tokens   -- Read freely
Medium file (100-500 lines): ~1000-5000 tokens  -- Read if needed
Large file (500+ lines):     ~5000+ tokens      -- Use offset/limit or subagent

For large files, read only the relevant section:

Read:
  file_path: "src/services/auth.ts"
  offset: 150    # Start at line 150
  limit: 50      # Read only 50 lines

Progressive Disclosure

Start with structure, then drill into details:

# Step 1: Understand the project structure (cheap)
Glob: "src/**/*.ts"

# Step 2: Find the relevant area (cheap)
Grep: pattern "export function authenticate"

# Step 3: Read only the relevant file (targeted)
Read: "src/middleware/auth.ts"

# Step 4: Read related files only if needed
Read: "src/types/auth.ts"

Do not read all source files upfront. Discover what you need, then read it.

Reference Files Instead of Inline Content

When a file's contents have been read and discussed, refer to it by path and line number instead of quoting large blocks:

# EXPENSIVE: Quoting 30 lines of code in your response
"The function at lines 45-75 does X, here is the full code: ..."

# EFFICIENT: Reference by path and line
"The authenticate function at src/middleware/auth.ts:45 handles
token validation. The error branch at line 62 needs updating."

Subagents as Context Firewalls

Use subagents when exploration would consume too much main context:

# BAD: Reading 15 files directly into main context
Read: "src/db/users.ts"
Read: "src/db/posts.ts"
Read: "src/db/comments.ts"
... (12 more files)

# GOOD: Delegate to subagent, receive summary only
Agent (Explore):
  prompt: "Read all files in src/db/. For each file, report:
           1. Table/model name
           2. Key query functions
           3. Whether it uses parameterized queries
           Return a concise table with these columns."

The subagent reads all 15 files in its own context. The main context receives only the summary table.

Targeted Grep Before Read

Use Grep to locate exactly what you need before reading entire files:

# Step 1: Find where the function is defined
Grep:
  pattern: "export function processPayment"
  output_mode: "content"
  -C: 3

# Step 2: Read only if you need more context around it
Read:
  file_path: "src/services/billing.ts"
  offset: 120
  limit: 40

Context-Sensitive Task Sizing

Match task complexity to remaining context:

High context remaining (early in session):

  • Large refactors across multiple files
  • Feature implementation spanning many components
  • Deep codebase exploration

Low context remaining (late in session):

  • Single-file edits
  • Independent utility creation
  • Simple bug fixes with known location
  • Documentation updates

Avoiding Context Waste

Common context wasters to avoid:

# WASTE: Reading a file you will not modify
Read: "package.json"  # Just to "check" — only read if you need specific info

# WASTE: Reading the same file twice
Read: "src/app.ts"  # Read earlier in session
Read: "src/app.ts"  # Reading again because you forgot — check conversation first

# WASTE: Large verbose tool output
Bash: "npm list --all"  # Produces hundreds of lines
# Better: "npm list --depth=0"  # Top-level only

# WASTE: Reading generated files
Read: "dist/bundle.js"  # Generated, not useful
Read: "node_modules/..."  # Never read node_modules

Anti-Patterns

  • Reading everything upfront: Do not read all project files at the start of a session. Use progressive disclosure — start with structure, drill into specifics.
  • Ignoring offset/limit: For large files, always use offset and limit to read only the relevant section. Reading a 2000-line file when you need lines 500-520 wastes context.
  • Quoting code back to the user: The user can see the file. Reference by path:line instead of copying 30 lines into your response.
  • Repeating tool calls: If you already read a file earlier in the session, do not read it again. Use your conversation history.
  • Using verbose bash output: Commands like npm list --all, find / -name "*.ts", or git log without limits dump large amounts of text. Always constrain output with flags or pipes.

Quick Reference

StrategyWhenHow
Progressive disclosureStarting explorationGlob -> Grep -> Read (targeted)
Offset/limitLarge filesoffset: N, limit: M on Read
Subagent firewallMany files to analyzeAgent (Explore) with summary prompt
Reference by pathDiscussing codefile.ts:line instead of quoting
Targeted grepFinding specific codeGrep with context before Read
Output constraintsBash commandsUse --depth, head, tail, flags

Rule: Read only what you need, when you need it. Summarize, do not quote.

Download now (ZIP)

Does it work?

Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.

No reviews yet

People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.

  • It worked
  • Partly
  • It didn't
What did you use it for? e.g. Reviewing pull requests in a Rails app

Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.

Your trail

Files you open will collect here, so you can find them again.

Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.

Now do this for your own.

Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.