Логотип Token Optimization

Token Optimization

Сообщество
valorisa
token-optimization

Optimize token consumption in Claude Code through cache management, context forking, model selection, and input filtering. Use when user mentions high token costs, context window bloat, slow sessions, cache invalidation, verbose agent output, heavy MCP load, or any context management issues. Covers four critical axes - cache misses, context bloating, wrong model/effort level, and verbose input formats. Triggers on "tokens", "cost", "expensive", "context", "cache", "slow session", "optimize", or symptoms of token waste even without explicit mention.

Обзор

Издательvalorisa
РепозиторийClaude-Skills
Название навыкаtoken-optimization
Звёзды
20
Форки
5
Встроенные файлы
6
ЛицензияMIT
Ссылки
  • Инструкции в формате Markdown

    Файл SKILL.md, который модель загружает по необходимости, поэтому токены расходуются только при подходящем запросе.

  • Работает с любой языковой моделью

    Навыки ИИ — это обычный Markdown, а не код для конкретного поставщика, поэтому они работают с GPT, Claude, Gemini, Grok и локальными моделями.

  • Файлы в комплекте: 6

    Скрипты, шаблоны и справочные материалы, которые модель может читать во время работы. Файлы доступны только для чтения и никогда не выполняются.

  • Открытый исходный код

    Опубликовано на GitHub: valorisa. Перед установкой изучите исходные материалы.

Установка

Установите навык ИИ Token Optimization в TypingMind, чтобы использовать его с любой LLM, или добавьте его в другой агент, который умеет читать SKILL.md.

1

Установка в TypingMind

TypingMind устанавливает навык прямо из его папки на GitHub: читает SKILL.md, собирает файлы ресурсов и сохраняет результат локально.

  1. Откройте приложение и перейдите в раздел «Плагины → Навыки».
  2. Выберите «Установить из GitHub».
  3. Вставьте указанную ниже ссылку на папку навыка и подтвердите установку.
    https://github.com/valorisa/Claude-Skills/tree/main/skills/token-optimization
  4. Включите навык в каждом чате, где хотите его использовать.
Плагины → Навыки → Новый навык → По ссылке GitHub, затем вставьте ссылку на папку и нажмите «Продолжить».
2

Установка в другом агенте

Любой агент с поддержкой формата Agent Skills может использовать этот навык — скопируйте папку в каталог навыков этого агента.

Claude Code — .claude/skills
git clone --depth 1 https://github.com/valorisa/Claude-Skills.git /tmp/Claude-Skills
mkdir -p .claude/skills
cp -r /tmp/Claude-Skills/skills/token-optimization .claude/skills/token-optimization
После копирования перезапустите Claude Code, чтобы загрузить новый навык.

Использование в TypingMind

Включите навык Token Optimization в любом чате TypingMind, и дальше модель всё сделает сама. Его название и описание хранятся в системном промпте, а при появлении подходящего запроса модель сама загружает полные инструкции. Вам не нужно вызывать навык вручную, и до фактического использования он не расходует токены.

Модель сама загружает навык Token Optimization, как только появляется подходящий для него запрос.

Работает с любой моделью ИИ

Навыки ИИ — это обычные инструкции в Markdown, а не код для конкретного поставщика, поэтому навык Token Optimization не привязан к модели, для которой он был создан. Установите его один раз в TypingMind и используйте с GPT-5, Claude, Gemini, Grok, DeepSeek, Mistral, Llama или локальной моделью, которую запускаете самостоятельно, — со своими API-ключами.

  • Загружается только при необходимости

    Системный промпт содержит только название и описание. Инструкции загружаются при первом подходящем запросе, поэтому неиспользуемый навык не расходует токены.

  • Меняйте модели прямо в чате

    Навык состоит из инструкций, а не кода, поэтому смена модели не нарушает его работу: новая модель читает тот же файл SKILL.md.

Инструкции навыка

Это содержимое файла SKILL.md, которое загружает модель. Прочитайте его перед установкой: навык — это инструкции, которым будет следовать ваша модель.

Token Optimization — Claude Code

Overview

Token overconsumption in Claude Code stems from four main sources:

  1. Cache miss — uncached or invalidated requests
  2. Context bloating — unnecessarily growing context window
  3. Wrong model / wrong effort — reasoning overuse
  4. Verbose input format — overly verbose data injected into context

Phase 1: Initial Session Audit

Before any work, run these two commands:

bash
/context   # Shows loaded MCPs and tokens already consumed
/plugin    # Lists active plugins

Systematic actions:

  • Disable all MCPs not needed for current project
  • Disable all unused plugins
  • One project ≠ one global config → customize per project

Real example: empty session with Canva + Gmail + Google Calendar MCPs loaded = 23,000 tokens consumed before first keystroke.

Red flags to check:

  • More than 3 MCPs loaded
  • Plugins unrelated to current work
  • Base token count >10,000 on empty session

Phase 2: Cache Management

Principle

Claude Code caches: system instructions (CLAUDE.md), tool/MCP list, and message history. Any modification during session invalidates this cache, causing complete re-billing of affected tokens.

Imperative rules

  • Never add a tool, MCP, or model mid-session. Configure everything before starting, or in a dedicated setup session.
  • If a critical addition is required mid-session (heavy skill, MCP, critical command):
    1. Compact the session (/compact)
    2. Start new session with compacted summary as starting point
  • Treat cache like HTTP cache: changing a header invalidates everything downstream.

Cache invalidation hierarchy

┌─────────────────────────────────────────────┐
│ BASE SYSTEM INSTRUCTIONS (CLAUDE.md + tools)│  ← Invalidated if tool added
├─────────────────────────────────────────────┤
│ SESSION STATE (project files, memory)       │  ← Invalidated if file changes
├─────────────────────────────────────────────┤
│ MESSAGE HISTORY                             │  ← Invalidated if message edited
│  msg 1 → msg 2 → msg 3 → ...                │     (invalidates everything after)
└─────────────────────────────────────────────┘

What invalidates cache:

ActionLayer invalidatedImpact
Add MCP mid-sessionBase instructionsVery high
Add tool (tool calling)Base instructionsVery high
Modify CLAUDE.mdBase instructionsVery high
Edit message in middle of historyMessage history (from that point)Moderate
Load new file into contextSession stateModerate

Optimal session strategy:

1. Full configuration (MCP, tools, CLAUDE.md) → BEFORE starting
2. Work session → NEVER modify configuration
3. If modification needed → /compact → new session
4. Agents → context fork → isolated work → result only

For technical details on cache issues, see references/cache-deep-dive.md.


Phase 3: Context Window Optimization

Reduce default context window

The 1M token window is tempting but dangerous: it encourages bad habits and overloads child agents.

Conditional recommendation:

  • Small/medium codebase → limit to ~200,000 tokens in settings.json:

    json
    { "contextWindow": 200000 }

    (not committed by default — use local file)

  • Very large codebase → keep 1M, but optimize practices below even more aggressively.

RAM analogy: having 32GB RAM doesn't justify poor memory management. A constrained window forces better discipline.

Context forking and isolated agents

Agents inherit parent context at spawn time. The longer you wait, the heavier they start.

Target behavior:

  • Call each skill or agent in a forked context (context: fork in parameters)
  • Agent works in its own window → at completion, returns only the result, not full history
  • When agent is destroyed, main context hasn't moved

Systematic use cases to isolate in subagent:

TaskWhy isolate
Web searchMarkdown history of visited pages pollutes main window
PDF / audio transcriptionHigh-volume output, unnecessary in main context
Planning / test generationLong work, clean context
Codebase explorationNative explore agent (uses IQ) already optimized for this

Prompt to add to CLAUDE.md:

For any web search, systematically delegate to isolated subagent.
Return only extracted relevant information, not full history.

Compaction and Rewind

  • Automatic compaction: triggers when agent reaches ~80% of window. Desirable for agents, not for main session.
  • Rewind (double Esc): allows returning to previous conversation and deleting unnecessary context branch. Use immediately when going in wrong direction.

Phase 4: Model & Reasoning Effort Selection

Reasoning effort

The effort system loops on the prompt to verify response quality. Each loop = additional tokens.

SituationRecommended effort
Clear, well-defined taskMedium (default)
Complex reasoning, architecture, designHigh
Simple task, known commandLow / none (think unnecessary)

Rule: if the answer is clear in your mind, use medium. Ask Claude to think with you, not for you.

Model choice

  • Claude Code subscription → use native Anthropic models (Sonnet / Opus). No proxy.
    • Opus: prefer for complex tasks despite latency
    • Sonnet: fast tasks, common code generation
  • Proxy to other LLMs (GLM, Kimi...): only relevant if using SDK/API directly, not in standard Claude Code. Proxies are unstable and unoptimized.

Add to CLAUDE.md:

markdown
## Reasoning effort
- Simple task / known command → minimal effort, no `think`
- Design / architecture task → medium effort
- Complex unsolved problem → high effort only on explicit request
Never over-reason a simple task.

Phase 5: Input Format Optimization

Browser automation: DOM over screenshots

Native Claude Code browser agent takes screenshots at each step → very costly in tokens and time.

Recommended alternative: Stagehand / Agent Browser (Vercel)

  • Operates directly on DOM (invisible, no screenshots)
  • Installation: open new Claude Code instance → Install [stagehand URL]
  • Result: 10× faster, drastically reduced tokens

Filter verbose CLI output

Commands like git status, git diff are extremely verbose once injected into context.

Recommended tool: RTK (developed by French developer)

  • Filters command output before context window injection

  • Example: git status goes from ~50 lines to ~5 essential lines → ~80% savings on this command

  • Global installation:

    bash
    # Run in Claude Code
    npx rtk init --global

Concise responses: Caveman Skill / CLAUDE.md

By default, Claude Code is very verbose. Drastically reduce response volume:

Directives to add to CLAUDE.md:

markdown
## Response style
Ultra-concise responses. Remove articles, politeness phrases, reformulations.
Get to the point. Format: short bullet points. No intro, no conclusion.

Caveman Skill (available on GitHub, ~50k stars): ready-to-use directives to integrate directly into CLAUDE.md. See references/claude-md-templates.md for examples.

Specify file paths explicitly

Don't let Claude discover files on its own in commands:

# ❌ Costly: Claude will list, explore, read
"Update project memory"

# ✅ Optimal: explicit paths in command
"Update ./docs/memory.md and ./CLAUDE.md with following items: ..."

Template for memory commands:

markdown
## Memory/Learning commands
`/learn` command: updates ONLY the following files:
- ./docs/project-memory.md
- ./CLAUDE.md (section "Project Context")
Do not scan other files unless explicitly instructed.

Phase 6: Validation & Monitoring

Check context consumption

After implementing optimizations, verify:

bash
/context    # Check current token count

Success criteria:

  • Base session <5,000 tokens (with necessary MCPs only)
  • After 10 interactions: <30,000 tokens
  • No agents exceeding 50,000 tokens for isolated tasks

Team monitoring

For teams needing consumption tracking:

  • Local dashboard tool (created by reference article author): displays sessions, called models, tokens per session
  • More useful for managers/leads than solo developers
  • See references below for link

Documented concrete result: went from $750/month to $100/month with these optimizations (source: article linked in reference video).


Troubleshooting

High token count despite optimizations

Diagnosis steps:

  1. Run /context → identify which MCPs are loaded
  2. Check settings.json → verify contextWindow setting
  3. Review recent commands → identify verbose outputs
  4. Check agent spawns → verify context forking

Common fixes:

  • Disable unused MCPs
  • Add RTK filtering for git commands
  • Move to subagents for research tasks
  • Reduce contextWindow if codebase allows

Cache invalidation happening frequently

Diagnosis:

  1. Check if skills are being loaded mid-session
  2. Verify CLAUDE.md isn't changing
  3. Look for tool additions in session

Fixes:

  • Preload all needed skills at session start
  • Lock CLAUDE.md configuration
  • Use dedicated setup session for config changes

Agents consuming too many tokens

Diagnosis:

  1. Check if agents inherit bloated parent context
  2. Verify agents aren't loading unnecessary MCPs
  3. Look for verbose tool outputs in agent history

Fixes:

  • Spawn agents earlier (lighter parent context)
  • Use context: fork parameter
  • Return only results, not full agent history

Examples

Example 1: Project setup with token optimization

User says: "Set up new project with optimal token configuration"

Actions:

  1. Create minimal CLAUDE.md with concise response directives
  2. Configure contextWindow based on codebase size
  3. Enable only essential MCPs for project type
  4. Add RTK filtering for git commands
  5. Define subagent delegation rules
  6. Verify base session <5,000 tokens

Expected result: Optimized session configuration, documented in project CLAUDE.md

Example 2: Diagnosing high token consumption

User says: "My sessions are consuming too many tokens, help diagnose"

Actions:

  1. Run /context to audit current state
  2. Check loaded MCPs and plugins
  3. Review recent command outputs for verbosity
  4. Identify cache invalidation patterns
  5. Generate optimization recommendations
  6. Implement fixes prioritized by impact

Expected result: Identified root causes, implemented fixes, reduced token consumption by 50-80%

Example 3: Web research task

User says: "Research best practices for React Server Components"

Actions:

  1. Spawn isolated subagent with context fork
  2. Subagent performs web searches
  3. Subagent extracts key findings only
  4. Return summary to main context (not full search history)
  5. Main context receives ~500 tokens instead of ~5,000

Expected result: Research completed with 90% token savings vs direct search


References

For detailed technical information:

  • references/cache-deep-dive.md — Cache behavior and invalidation patterns
  • references/claude-md-templates.md — Ready-to-use CLAUDE.md configuration blocks
  • references/tools.md — External tools (Stagehand, RTK) setup guides

External resources:


Quality Checklist

Before completing optimization:

  • Base session <5,000 tokens
  • Only essential MCPs loaded
  • CLAUDE.md includes concise response directives
  • Context forking configured for research tasks
  • RTK or equivalent filtering installed for verbose commands
  • contextWindow set appropriately for codebase size
  • Team members trained on cache invalidation rules (if applicable)

Встроенные файлы

Модель читает эти файлы по мере необходимости, пока навык загружен. Они доступны только для чтения и никогда не выполняются.

Часто задаваемые вопросы

Что делает навык ИИ Token Optimization?

Optimize token consumption in Claude Code through cache management, context forking, model selection, and input filtering. Use when user mentions high token costs, context window bloat, slow sessions, cache invalidation, verbose agent output, heavy MCP load, or any context management issues. Covers four critical axes - cache misses, context bloating, wrong model/effort level, and verbose input formats. Triggers on "tokens", "cost", "expensive", "context", "cache", "slow session", "optimize", or symptoms of token waste even without explicit mention.

Зачем использовать Token Optimization в TypingMind?

Потому что вы устанавливаете навык один раз и используете его с любой моделью. Token Optimization — это обычный текст в формате Markdown, а не код для конкретного поставщика, поэтому один и тот же навык работает с GPT-5, Claude, Gemini, Grok или локальной моделью. Вы можете менять модель прямо во время чата, и навык продолжит работать. TypingMind использует ваши собственные ключи API, поэтому вы платите поставщикам напрямую, а не оформляете подписку с оплатой за каждое место. Ваши навыки и чаты остаются в вашем собственном хранилище.

Как установить Token Optimization в TypingMind?

В TypingMind откройте «Плагины → Навыки → Установить из GitHub» и вставьте https://github.com/valorisa/Claude-Skills/tree/main/skills/token-optimization. TypingMind прочитает файл SKILL.md, включит сопутствующие файлы в пакет и установит навык, который можно включать отдельно для каждого чата.

Какие модели ИИ могут использовать Token Optimization?

Любая модель, которую вы подключите в TypingMind. Навыки ИИ — это обычные инструкции в формате Markdown, а не код для конкретного поставщика, поэтому GPT, Claude, Gemini, Grok и локальные модели могут загружать этот навык, когда запрос ему соответствует.

Со сколькими моделями ИИ можно использовать Token Optimization?

С любым количеством. Если модель поддерживает навыки, вы можете использовать с ней Token Optimization: GPT, Claude, Gemini, Grok, DeepSeek, Mistral, Llama и другие — всё в TypingMind с вашими собственными ключами API.

Бесплатен ли навык ИИ Token Optimization?

Да. Навык опубликован на GitHub автором valorisa по лицензии MIT. Вы платите только своему поставщику ИИ за использованные токены.

Что такое навыки ИИ?

Навык ИИ — это многоразовый набор инструкций, который объясняет модели ИИ, как выполнять конкретную задачу. Он соответствует открытому формату Agent Skills: файл SKILL.md с названием и описанием, а также любые скрипты, шаблоны или справочные файлы, которые могут понадобиться модели. Модель читает инструкции, только когда ваш запрос соответствует навыку, поэтому установленный навык ничего не стоит, пока не используется.

Чем навыки ИИ отличаются от плагинов и серверов MCP?

Плагин или сервер MCP даёт модели новые инструменты для вызова — код, который где-то выполняется и возвращает результат. Навык ИИ вместо этого даёт модели знания и порядок действий: как подойти к задаче, какие шаги выполнить и каким должен быть хороший результат. Навыки — это обычный текст в формате Markdown, поэтому им не нужны сервер, ключ API или среда выполнения, и они работают с любой моделью.

Все

Создайте свою рабочую ИИ-среду сейчас

Подпишитесь на нашу рассылку, чтобы узнавать о новых функциях и предстоящих розыгрышах 👇