Files
kv-ai/sample-data/Default/My AI Learning/Claude Code Context Optimization.md

2.9 KiB

I Stopped Hitting Product/Claude Code Usage Limits (Person/Brad | Concept/AI & Automation)

Ref: https://youtu.be/9ToOfgZ4qqQ?si=zCfA4u4oFWPQ5neU

Core Concept: Context Hygiene

The issue isn't just "limits," but context bloat. Every message in a session compounds in cost because the entire history is reread. Bloated context leads to higher costs, faster limit hits, and potentially worse output (LLM loses focus on the start of the window).

Strategies to Reduce Token Burn

1. Pruning Invisible Bloat

  • MCP Servers: Every connected MCP server loads all tool definitions into every message.
    • Fix: Use /mcp to list and disconnect servers not actively needed for the current session.
    • Optimization: Replace MCPs with CLIs where possible; CLIs only cost tokens when actually called, whereas MCPs cost tokens just by existing.
  • claude.md Optimization:
    • Remove Contradictions: Ensure instructions don't clash (e.g., "be concise" vs "explain in detail").
    • Ruthless Pruning: Remove rules that are default behaviors, redundant, or vague "band-aids."
    • Progressive Disclosure: Keep the core claude.md small (universal rules only). Move detailed patterns (API standards, testing guidelines) to separate reference files and use one-line pointers in the core file.
  • Skill Bloat:
    • Metadata for every installed skill is loaded into context.
    • Prefer concise, short skills over verbose ones.

2. Settings.json Tweaks

  • Auto-compact: Set auto_compact_percentage_override to ~75% (default is ~83%) to trigger compaction before quality degrades.
  • Bash Output: Set bash_max_output_length environment variable to 150,000 to avoid silent truncation and costly reruns.
  • Permissions: Add deny rules for directories like node_modules, .git, and build artifacts to prevent Product/Claude from wasting context on irrelevant files.

3. Daily Habits

  • Fresh Sessions: Use /clear between unrelated tasks to stop paying the "tax" of previous conversation history.
  • Plan Mode: Use plan mode for non-trivial tasks to align on the approach before code is written, avoiding expensive "wrong path" loops.
  • Replace, Don't Correct: When Product/Claude makes a mistake, replace the bad exchange rather than sending a follow-up correction (which keeps the error in the history).
  • Right Model for the Job: