スキル一覧に戻る
hgeldenhuys

context-optimization

by hgeldenhuys

1🍴 0📅 2026年1月24日
GitHubで見るManusで実行

SKILL.md


name: context-optimization description: Guide for managing and optimizing context in Claude Code. Use when experiencing slow responses, context warnings, or planning large tasks. Covers /compact, /clear, context budgeting, subagent delegation, and efficient session workflows. allowed-tools: ["Read"]

Context Optimization

Manage Claude Code's context window efficiently for faster responses, better memory, and productive long sessions.

Quick Reference

CommandEffectWhen to Use
/compactCompress context, preserve key infoApproaching limit, slow responses
/clearReset to empty contextFresh start, topic change
/costShow token usageMonitor consumption
/resumeContinue previous sessionMulti-session workflows
/renameName current sessionSession organization

Understanding Context

What Is Context?

Context is everything Claude "remembers" in your conversation:

  • Your messages and Claude's responses
  • Files read during the session
  • Tool outputs (bash results, search results)
  • System prompts and loaded skills

Context Window Limits

ModelContext WindowPractical Limit
Claude Opus 4.5200K tokens~150K usable
Claude Sonnet 4200K tokens~150K usable
Claude Haiku200K tokens~150K usable

Note: Reserve 20-30% for Claude's responses. Hitting the ceiling causes degraded performance before hard failures.

Token Estimation

Content TypeApproximate Tokens
1 line of code10-15 tokens
100 lines of code1,000-1,500 tokens
Typical source file500-3,000 tokens
Large file (1000+ lines)5,000-15,000 tokens
Your message~1 token per word

Context Pressure Indicators

Visual Indicators

  1. Token counter - Watch the usage bar in the UI header
  2. Response time - Noticeably slower responses
  3. Cost increase - Higher per-turn costs (use /cost)

Behavioral Signs

SignWhat's Happening
Claude "forgets" earlier discussionContext truncation
Repeated questions about contextInformation pushed out
Slower, choppier responsesProcessing large context
Incomplete tool outputsContext conservation
Asking to re-read filesFile content evicted

When to Act

Usage LevelRecommendation
Under 50%Continue normally
50-70%Plan for compaction soon
70-85%Compact now, consider task splitting
85%+Compact immediately or clear

/compact - Context Compression

What /compact Does

  1. Summarizes conversation history
  2. Preserves key decisions and context
  3. Maintains file awareness
  4. Keeps recent changes tracked
  5. Reduces token count by 50-80%

When to Use /compact

  • Token usage exceeds 70%
  • Responses becoming slow
  • Before starting a new sub-task
  • Transitioning between phases
  • After completing a major task

Compact Workflow

> /compact

Claude compresses the conversation, keeping:

  • Current task objectives
  • Key decisions made
  • Files modified/created
  • Important constraints
  • Recent context (last few turns)

Best Practices

  1. Compact at natural breaks - Between tasks, not mid-implementation
  2. State important context first - Mention critical info before compacting
  3. Verify understanding after - Ask "What are we working on?" post-compact
  4. Don't over-compact - Once per major phase, not every few turns

/clear - Fresh Start

What /clear Does

  • Completely resets context to zero
  • No memory of previous conversation
  • Like starting a new session

When to Use /clear

ScenarioWhy Clear
Switching projectsDifferent codebase, different context
Major topic changeUnrelated to previous work
Context too pollutedToo much irrelevant history
Starting fresh approachPrevious direction was wrong
Testing from scratchNeed clean state

/clear vs /compact

Aspect/compact/clear
Memory retainedSummarizedNone
Token usage after~20-50%~0%
Context continuityPreservedLost
Use caseContinue workFresh start

Clear Workflow

> /clear

Now starting fresh on [task description]...

Always restate your objective after clearing.

Context Budgeting

Planning Sessions

Before starting work, estimate context needs:

Task: Implement user authentication
Estimated reads: 10 files (~15,000 tokens)
Expected tool use: High (~20,000 tokens)
Conversation: Medium (~10,000 tokens)
Buffer: 20% (~10,000 tokens)
Total estimate: ~55,000 tokens (28% of window)

Budget Allocation Strategy

CategoryAllocationNotes
System prompt + skills5-10%Fixed overhead
File reads20-40%Be selective
Tool outputs15-25%Limit verbose output
Conversation20-30%Your messages + responses
Response buffer20%Room for Claude's output

Reducing Context Usage

  1. Read selectively - Request specific functions, not entire files
  2. Use targeted searches - grep patterns instead of reading files
  3. Limit tool verbosity - Ask for summaries, not full outputs
  4. Break into sessions - Split large tasks across multiple sessions
  5. Use subagents - Delegate to separate context windows

Subagent Delegation

Why Subagents Help

Subagents have their own context window. Delegating tasks:

  • Preserves main conversation context
  • Allows parallel exploration
  • Isolates experimental changes
  • Returns only relevant results

Delegation Pattern

> Use a subagent to analyze the database schema and report the key tables

[Subagent runs in separate context]
[Returns summary to main conversation]

Good Candidates for Delegation

Task TypeWhy Delegate
File explorationMany reads, minimal output needed
Code analysisDeep dive, summary sufficient
Test executionVerbose output, pass/fail matters
Documentation readingLarge content, key points needed
Search tasksMany grep/glob operations

Agent Definition for Context Efficiency

---
name: explorer
description: Explore codebase and return concise summaries
tools: Read, Glob, Grep
model: haiku
---

You are a code explorer. Investigate thoroughly but report concisely.
Return only essential findings. Summarize, don't quote entire files.

See creating-subagents for full agent creation guide.

Session Management

Naming Sessions

> /rename authentication-feature

Benefits:

  • Easy to identify in history
  • Clear purpose documentation
  • Resumable with meaningful names

Resuming Sessions

> /resume

Shows recent sessions. Select one to continue where you left off.

Or directly:

> /resume authentication-feature

Multi-Session Workflows

For large features, plan multiple sessions:

SessionFocusCompact At
Session 1Architecture designEnd
Session 2Backend implementation70%
Session 3Frontend implementation70%
Session 4Integration & testingEnd

Session Checkpoints

Create natural checkpoints:

  1. Before major changes - Compact to preserve state
  2. After milestones - Document progress in conversation
  3. At decision points - State decisions clearly for post-compact memory

Efficient Workflows

Task Batching

Instead of interleaving tasks:

Inefficient:

> Read file A
> Make change to A
> Read file B
> Make change to B
> Read file A again

Efficient:

> Read files A and B
> Make all changes to A
> Make all changes to B

Explicit Context Statements

Help Claude remember across compactions:

> We're building a REST API for user management.
> Key constraint: Must use existing auth middleware.
> Files involved: src/api/users.ts, src/middleware/auth.ts

Pre-Compact Checklist

Before running /compact:

  • Stated current objective clearly
  • Mentioned key constraints
  • Listed important files/decisions
  • Completed current sub-task

Monitoring Usage

/cost Command

> /cost

Shows:

  • Total tokens used
  • Input vs output breakdown
  • Estimated cost

When to Check

  • After reading large files
  • After verbose tool operations
  • Every 10-15 exchanges
  • Before starting new task

Reference Files

FileContents
STRATEGIES.mdDetailed optimization strategies
INDICATORS.mdContext pressure indicators and monitoring
WORKFLOWS.mdEfficient workflow patterns

Quick Decisions

SituationAction
Slow responses/compact
Topic change/clear
Large task aheadPlan sessions, use subagents
Forgot earlier discussionRestate context, consider clearing
Mid-implementationAvoid compacting, finish first
Starting new featureName session, budget context

スコア

総合スコア

50/100

リポジトリの品質指標に基づく評価

SKILL.md

SKILL.mdファイルが含まれている

+20
LICENSE

ライセンスが設定されている

0/10
説明文

100文字以上の説明がある

0/10
人気

GitHub Stars 100以上

0/15
最近の活動

3ヶ月以内に更新がある

0/10
フォーク

10回以上フォークされている

0/5
Issue管理

オープンIssueが50未満

+5
言語

プログラミング言語が設定されている

+5
タグ

1つ以上のタグが設定されている

0/5

レビュー

💬

レビュー機能は近日公開予定です