Context Optimization
This skill should be used when the user asks about "context window optimization", "reduce context usage", "context is filling up", "running out of context", "context bloat", "optimize my sessions", "why is context so large", or when providing advice on how to keep Claude Code sessions efficient. Provides knowledge about what consumes context and strategies to minimize waste.
What this skill does
# Context Optimization for Claude Code
## Overview
Claude Code sessions have a finite context window. Understanding what consumes context and how to minimize waste keeps sessions productive longer. This skill provides patterns and strategies for context-efficient workflows.
## What Consumes Context
Every interaction in a Claude Code session adds to context. The main consumers, ranked by typical impact:
### 1. Message Metadata Overhead (30-40%)
Every message carries a JSON envelope with sessionId, uuid, cwd, gitBranch, version, and other fields. This is not directly controllable but is the largest single category.
### 2. Progress/Streaming Messages (20-30%)
Hook progress events and streaming data accumulate significantly in long sessions. Not directly controllable.
### 3. User-Pasted Images (10-20%)
Screenshots pasted into chat are encoded as base64 PNG, inflating size ~33% over raw bytes. A single full-screen PNG screenshot can consume 500 KB to 2 MB of context.
### 4. File Read Results (5-10%)
Every `Read` tool invocation returns the full file content into context. Re-reading the same file multiple times multiplies its impact.
### 5. Browser Screenshots (3-5%)
Claude-in-Chrome screenshots are JPEG (~50-80 KB each) but accumulate in browser-heavy sessions.
### 6. Edit/Write Payloads (2-3%)
File content included in Edit and Write operations.
### 7. Bash Output (1-3%)
Command output returned to context. Usually small per-call but adds up with heavy Bash usage.
## Optimization Strategies
### Compress Screenshots Before Pasting
- Resize to ~800px wide before pasting
- Use JPEG format instead of PNG where possible
- Consider using Claude-in-Chrome's screenshot tool (produces ~50-70 KB JPEG) instead of pasting full-screen PNGs (500 KB-2 MB)
- On macOS, use Preview or a keyboard shortcut to downscale clipboard images
### Reduce File Re-Reads
- Add structural summaries of frequently-referenced files to the project's `CLAUDE.md`
- For infrastructure files (CloudFormation, Terraform, etc.), document resource names and relationships in `CLAUDE.md`
- Keep documentation files (DEVLOG.md, CHANGELOG.md) concise; archive old content
### Use /compact Proactively
- Run `/compact` after browser automation sequences with many screenshots
- Run `/compact` after completing a distinct phase of work
- Run `/compact` when switching between different parts of the codebase
### Optimize Bash Usage
- Pipe verbose output through `head` or `tail` when full output is unnecessary
- Redirect large outputs to files and read selectively
- Use `--quiet` or `--silent` flags where available
### Use .claudeignore
- Add build artifacts, node_modules, and generated files
- Add large data files that should not be searched
- Add log files and temporary outputs
### Prefer Edit Over Write
- `Edit` replaces specific strings (small payload)
- `Write` sends the entire file content (large payload)
- For modifications to existing files, Edit is more context-efficient
### Consolidate Subagent Usage
- Each Task/subagent invocation adds its result to context
- Combine related queries into a single subagent call when possible
- Use targeted prompts that return concise results
## Diagnosing Context Issues
To analyze what's consuming context in a specific project, run:
```
/context-analyzer:analyze-context
```
This parses the project's JSONL chat history files in `~/.claude/projects/` and produces a detailed breakdown with per-category sizes, most re-read files, and targeted recommendations.
For programmatic access, the analysis script supports JSON output:
```bash
python3 ${CLAUDE_PLUGIN_ROOT}/scripts/analyze_context.py --json
```
## Common Anti-Patterns
### Re-Reading the Same File Every Turn
**Problem**: Claude reads `types.ts` or `api.ts` every time it needs to reference types.
**Fix**: Add key type definitions or API surface to `CLAUDE.md`.
### Pasting Full-Screen Screenshots
**Problem**: Each PNG screenshot is 500 KB-2 MB in base64.
**Fix**: Crop to the relevant area and resize before pasting.
### Long Browser Automation Sequences Without Compacting
**Problem**: 20+ screenshots from scroll-screenshot-click sequences fill context.
**Fix**: Run `/compact` after browser testing. Use `read_page` (accessibility tree) instead of screenshots when visual verification isn't needed.
### Verbose Build/Test Output
**Problem**: Full test suite output or build logs dumped to context.
**Fix**: Pipe through `tail -20` or grep for failures only.
### Large Documentation Files Read on Every Session
**Problem**: DEVLOG.md or similar grows over time and is read at session start.
**Fix**: Keep it under 5 KB or move to Claude Code memory (`~/.claude/projects/.../memory/`).
## Additional Resources
For detailed analysis methodology and report interpretation, see:
- **`references/analysis-methodology.md`** — How the analysis script categorizes context consumption
Related in AI Agents
skill-development
IncludedComprehensive meta-skill for creating, managing, validating, auditing, and distributing Claude Code skills and slash commands (unified in v2.1.3+). Provides skill templates, creation workflows, validation patterns, audit checklists, naming conventions, YAML frontmatter guidance, progressive disclosure examples, and best practices lookup. Use when creating new skills, validating existing skills, auditing skill quality, understanding skill architecture, needing skill templates, learning about YAML frontmatter requirements, progressive disclosure patterns, tool restrictions (allowed-tools), skill composition, skill naming conventions, troubleshooting skill activation issues, creating custom slash commands, configuring command frontmatter, using command arguments ($ARGUMENTS, $1, $2), bash execution in commands, file references in commands, command namespacing, plugin commands, MCP slash commands, Skill tool configuration, or deciding between skills vs slash commands. Delegates to docs-management skill for official documentation.
reprompter
IncludedTransform messy prompts into well-structured, effective prompts — single or multi-agent. Use when: "reprompt", "reprompt this", "clean up this prompt", "structure my prompt", rough text needing XML tags and best practices, "reprompter teams", "repromptception", "run with quality", "smart run", "smart agents", multi-agent tasks, audits, parallel work, anything going to agent teams. Don't use when: simple Q&A, pure chat, immediate execution-only tasks. See "Don't Use When" section for details. Outputs: Structured XML/Markdown prompt, quality score (before/after), optional team brief + per-agent sub-prompts, agent team output files. Success criteria: Single mode quality score ≥ 7/10; Repromptception per-agent prompt quality score 8+/10; all required sections present, actionable and specific.
adaptive-compaction
IncludedAdaptive add-on policy and recovery layer that decides WHEN to compact, prune, snapshot, or fork -- replacing fixed-percent auto-compaction across Claude Code, Codex, and MCP-capable hosts. Trigger on auto-compact timing or damage: "when should I compact", "is it safe to compact now or start a fresh session", "auto-compact fires too early/mid-task", "switching to an unrelated task but the window still has space", "context rot", "answers get worse the longer the session runs", "the agent forgot the plan or my decisions after it summarized", "add a layer on top that manages context without changing the agent", raising autoCompactWindow to give the policy room, or installing/tuning a cross-tool compaction policy or PreCompact hook -- even when "compaction" is never said but the problem is context-window pressure or post-summarization memory loss. Do NOT use to summarize a conversation, build RAG, write a summarization prompt (decides WHEN not HOW), or answer max-context-length trivia.
agent-skill-creator
IncludedCreate cross-platform agent skills from workflow descriptions. Activates when users ask to create an agent, automate a repetitive workflow, create a custom skill, or need advanced agent creation. Triggers on phrases like create agent for, automate workflow, create skill for, every day I have to, daily I need to, turn process into agent, need to automate, create a cross-platform skill, validate this skill, export this skill, migrate this skill. Supports single skills, multi-agent suites, transcript processing, template-based creation, interactive configuration, cross-platform export, and spec validation.
llm-wiki
IncludedUse when building or maintaining a persistent personal knowledge base (second brain) in Obsidian where an LLM incrementally ingests sources, updates entity/concept pages, maintains cross-references, and keeps a synthesis current. Triggers include "second brain", "Obsidian wiki", "personal knowledge management", "ingest this paper/article/book", "build a research wiki", "compound knowledge", "Memex", or whenever the user wants knowledge to accumulate across sessions instead of being re-derived by RAG on every query.
skill-master
IncludedAgent Skills authoring, evaluation, and optimization. Create, edit, validate, benchmark, and improve skills following the agentskills.io specification. Use when designing SKILL.md files, structuring skill folders (references, scripts, assets), ingesting external documentation into skills, running trigger evals, benchmarking skill quality, optimizing descriptions, or performing blind A/B comparisons. Keywords: agentskills.io, SKILL.md, skill authoring, eval, benchmark, trigger optimization.