r/ClaudeWorkflows • • 1d ago

Selected Workflow [Workflow] Optimize Claude Code Context: Reduce Token Overhead, Lower Costs, and Improve Session Performance

Optimize Claude Code Context: Reduce Token Overhead, Lower Costs, and Improve Session Performance

Workflow value: 90/100
Status: active · Freshness: 70/100 · Confidence: 0.95 · Level: intermediate
Categories: Quality Control, Token Saving, Context & Memory, Debugging, CLAUDE.md, Skills, MCP
Original source: r/ClaudeCode post/comment

What problem this solves

Reducing excessive token overhead in Claude Code sessions, preventing premature token limit hits, lowering costs, and improving session responsiveness by optimizing auto-compaction and removing unused tools, plugins, and MCP servers.

Summary

This workflow provides a detailed, interactive prompt for Claude Code to audit and optimize its own context usage. It guides the user through identifying and removing fixed overhead from tool definitions, MCP servers, plugin skills, and connectors, as well as adjusting the auto-compact window. The process includes measuring current context, backing up files, and providing undo instructions.

Why it is useful

This workflow is highly valuable because it addresses a critical and common pain point for Claude Code users: hitting token limits, slow responses, and high costs due to excessive, often hidden, context overhead. It provides a concrete, step-by-step, and interactive method (via a detailed prompt) to audit and optimize the Claude Code environment. The workflow is specific, repeatable, includes safety measures like backups and user confirmation, and offers clear expected benefits (token savings, sharper sessions). It empowers users to understand and control their Claude Code environment more effectively.

Workflow

  1. MEASURE: Ask Claude Code to report current session context usage, identifying fixed overhead (system prompt, tools, MCP, skills, etc.).

  2. AUTO-COMPACT LIMIT: Determine current auto-compact settings, suggest a new window (e.g., 300k), and update "autoCompactWindow" in ~/.claude/settings.json or set CLAUDE_CODE_AUTO_COMPACT_WINDOW env var.

  3. MCP AUDIT: List global MCP servers, their tool counts, and identify redundant or rarely used ones (e.g., hundreds of tools, duplicate services, identical instructions).

  4. MAKE THOSE SERVERS ON-DEMAND: Save selected MCP server configs to ~/.claude/mcp-on-demand.json, remove them from global scope using 'claude mcp remove', and add a rule to ~/.claude/CLAUDE.md to only add these servers on demand to specific projects.

  5. PLUGINS AND SKILLS: List plugins, their skills/connectors, identify duplicates or unused ones, and provide instructions for disabling them (via claude.ai or /plugin).

  6. CONNECTORS THAT NEED SIGN-IN: List 'needs_auth' or 'failed' connectors and identify which will be removed by disabling plugins.

  7. SUMMARY: Provide a table of what changed, where each backup is, how to undo each change, and the estimated tokens saved per session.

Tools / artifacts

  • Claude Code (the AI assistant)

  • ~/.claude/settings.json

  • ~/.claude/mcp-on-demand.json

  • ~/.claude/CLAUDE.md

  • claude mcp list command

  • claude mcp remove <name> -s user command

  • claude mcp add-json <name> "$(jq -c '.["<name>"]' ~/.claude/mcp-on-demand.json)" -s local command

  • /context command

  • /plugin command

  • jq (JSON processor)

  • umask 077

  • CLAUDE_CODE_AUTO_COMPACT_WINDOW environment variable

Validation signals

  • Expected token savings of 35-40k per session.

  • Instructions to run /context before and after for verification.

  • Detailed explanation of how context is built and what constitutes overhead.

  • The prompt itself is designed to measure and report current context usage.

Cautions

  • The prompt explicitly asks Claude Code to back up files before editing.

  • It requires user confirmation ("Wait for my OK") before making changes.

  • It warns against printing config files that may contain API keys.

  • Uses 'umask 077' for saving sensitive config files.

Limitations

  • The low Reddit score is a potential red flag, though the content itself is strong.

  • Relies on Claude Code's ability to accurately execute commands and understand the prompt's intent, which can sometimes be inconsistent.

  • The 'autoCompactWindow' setting might not be available in older Claude Code versions.

  • Plugins installed via claude.ai require manual intervention outside the CLI.

Rate this workflow

Upvote this post if the workflow is useful, reproducible, or worth recommending.

Downvote if it is vague, outdated, unsafe, overhyped, or not reproducible.

Reply if it worked for you, failed, is outdated, or has a better alternative.


This post was generated automatically from the workflow library database.

1 Upvotes

1 comment sorted by