Claude Code Implementation

Claude Code-specific token optimization strategies including compaction guidance, subagent routing, and session hygiene practices

Token Optimization Guide / Claude Code Implementation

Claude Code Token Optimization

Layer 1 β€” Platform Mechanics

Keep CLAUDE.md content stable across a project so it doesn’t need reprocessing every session; put task-specific instructions in Skills (~/.claude/skills/) instead of inline in CLAUDE.md, so they only enter context when matched. Assign subagents by tier: Haiku for test/log/doc-retrieval work, Sonnet as default, Opus reserved for architecture calls. Cap reasoning spend with /effort or MAX_THINKING_TOKENS.

Key Strategies:

  • Stable Core Content: Keep project-level CLAUDE.md stable to avoid reprocessing overhead
  • Progressive Disclosure: Move task-specific instructions to Skills that load on-demand
  • Tiered Subagent Routing: Route subagents by complexity tier
    • Haiku: Test runs, log triage, doc lookups
    • Sonnet: Default for code generation
    • Opus: Architecture-level decisions only
  • Reasoning Spend Control: Use /effort settings or MAX_THINKING_TOKENS to cap spend

Layer 2 β€” Agent Operation

Write explicit compaction guidance into CLAUDE.md (what to keep, what to drop) so /compact doesn’t guess. Treat /clear as the default when switching to an unrelated task, not just /compact. Audit MCP servers regularly β€” an idle server still costs prefix tokens every turn.

Operational Best Practices:

  • Explicit Compaction Rules: Define what to preserve vs. discard in /compact
    • Preserve: Failing test output, diffs, open TODOs, acceptance criteria
    • Discard: Exploratory file reads that didn’t lead to changes
  • Session Hygiene: Default to /clear when switching tasks, not /compact
  • MCP Server Auditing: Regular audit and removal of unused MCP servers
    • Idle servers still cost prefix tokens every turn
    • Flag servers unused for 2+ weeks for removal

Layer 3 β€” Codebase Architecture

Keep the SRP/coupling/file-size rule in the shared AGENTS.md, which Claude Code now reads alongside CLAUDE.md.

Architecture Rules:

  • Single Responsibility Principle: One responsibility per function/class/file
  • File Size Limits: Split files exceeding ~300 lines before adding more
  • Dependency Management: Keep dependency fan-out low
  • Shared Rules: Maintain architecture rules in shared AGENTS.md

Claude Code-Specific Features

Plan Mode

Claude Code’s Plan mode provides a built-in planning gate that separates exploration from implementation. Use this for non-trivial tasks to prevent uncontrolled editing during exploration.

Skills System

Claude Code’s Skills system (~/.claude/skills/ or .claude/skills/) provides progressive disclosure:

  • Skills load only when matched to the task
  • Reduces always-loaded token overhead
  • Ideal for task-specific procedures and domain knowledge

Subagent Model Routing

Per-subagent model definition allows fine-grained cost control:

  • Define model tier per subagent in subagent configuration
  • Reserve expensive models (Opus) for high-value tasks
  • Use cheaper models (Haiku) for routine operations

Interactive Tools

Practical tools to help you think systematically, build better AI agents, and master prompt engineering.

Software Engineering Playbooks

Practical, End-to-End implementation guides for building Production-ready Software. Each playbook includes working code, architecture diagrams, and step-by-step instructions.

πŸ€–

Research Agent with Gateway

Build a production-ready AI research agent using Agent Gateway for unified traffic management, authentication, and observability across LLM providers.

Agent Gateway A2A Protocol Multi-Agent
πŸ”—

RAG Pipeline with Vector Database

Implement a complete Retrieval-Augmented Generation pipeline with vector embeddings, semantic search, and context injection for accurate AI responses.

RAG Vector DB Embeddings
πŸ”„

Multi-Agent Orchestration

Create a coordinated multi-agent system with specialized agents, task distribution, and result synthesis for complex problem-solving.

Orchestration Task Distribution Synthesis
1 / 2

Future References

Explore these resources for deeper learning on AI agent development, spec-driven development, and prompt engineering tools.

Spec-Driven Development

Comprehensive guide on Spec-Driven Development practices and methodologies.

Awesome Copilot

Curated list of GitHub Copilot resources, extensions, and best practices.

Promptfoo

Tool for testing, evaluating, and improving LLM prompts and applications.

Prompts.chat

Collection of prompt engineering resources and templates.

Agent Skills

Agent skills resources and documentation for building AI agent skills.

Awesome Skills

Curated list of awesome skill repositories and collections.

Agent Skills Topic

GitHub topic for discovering agent-related skills and repositories.

AI Agent Topic

Trendshift topic for discovering AI agents.

AI Skills Topic

Trendshift topic for discovering AI skills.

Agent Governance Toolkit

Agent governance toolkit.

Pattern Sources

Our patterns are curated from industry-leading sources with proper attribution and licensing compliance.

Refactoring.Guru

Classic GoF design patterns, code smells catalog, and refactoring techniques (https://refactoring.guru).

Enterprise Integration Patterns

65 messaging patterns for integrating enterprise applications by Gregor Hohpe and Bobby Woolf (CC BY 4.0).

Microservices.io

Comprehensive patterns for microservice architectures by Chris Richardson.

Agent Catalog Patterns

Patterns for agentic systems from agentpatternscatalog.org (CC BY 4.0).

OWASP Foundation

Security patterns from OWASP Top 10 for Web Applications, LLM Applications, and Agentic Applications (CC BY-SA 4.0).

Industry Research

ML/AI patterns from Microsoft, Google, Anthropic, and academic research.

AI Agent Patterns

Spec-driven development patterns from Claude, Gemini, OpenAI, and GitHub Copilot on github/spec-kit and OpenSpec.

Data Engineering Leaders

Data platform patterns from Martin Fowler (Data Mesh), Kimball Group (Dimensional Modeling), and cloud providers.

MLOps Best Practices

Data science patterns from MLflow, Great Expectations, and MLOps practitioners.

Streaming & Analytics

Real-time patterns from Confluent/Kafka, Apache projects, and serverless analytics platforms.

Academic Papers

Rigorous ML patterns from peer-reviewed research including data leakage prevention and active learning.

5-Day AI Agents Course

Intensive Vibe Coding Course With Google by Brenda Flynn et al. (2026) on Kaggle.

The Agent Loop

Foundational Agent Definition (Perceive + Act):
Russell, S. J., & Norvig, P. (1995). Artificial Intelligence: A Modern Approach. Prentice Hall. (Current edition: 4th Ed., Pearson, 2020)

Modern Iterative LLM Agent Loop:
Yao, S., Zhao, J., Yu, D., et al. (2022). ReAct: Synergizing Reasoning and Acting in Language Models. arXiv:2210.03629.

Historical Context:
Incorporating AIMA's perceive/act model, Classical robotics' Sense-Plan-Act loop (Brooks, 1986), and ReAct's Thought→Action→Observation cycle.