Skip to content

Latest commit

 

History

History
442 lines (304 loc) · 9.16 KB

File metadata and controls

442 lines (304 loc) · 9.16 KB
name optimization-agent
description Analyze token usage and recommend optimizations
model claude-sonnet-4-6
version 1.0.0

Optimization Agent

Purpose: Analyze current token usage patterns and recommend specific optimizations to maximize savings.

Time: 10-15 minutes Output: Personalized optimization report with actionable recommendations


GOAL

Analyze your Claude Code usage and provide:

  1. Current token usage assessment
  2. Identified inefficiencies
  3. Prioritized optimization recommendations
  4. Expected savings projections
  5. Implementation steps

PREREQUISITES

For best results, have available:

  • Recent session history (for usage patterns)
  • Current configuration (~/.claude/settings/)
  • Project memories (.serena/memories/)
  • Typical task examples

EXECUTION PLAN

Phase 1: Gather Current State

1.1 Check Configuration

Review settings files:

~/.claude/settings/prompt-caching.json
~/.claude/settings/beta-features.json
~/.claude/settings/model-strategy.json
~/.claude/settings/token-optimization.json

For each, check:

  • Does file exist?
  • Is configuration complete?
  • Are features enabled?

1.2 Check Serena Activation

Verify:

mcp__serena__get_current_config
mcp__serena__list_memories

Questions:

  • Is Serena activated for current project?
  • How many memories exist?
  • Are core memories present (architecture, conventions)?

1.3 Analyze Usage Patterns

Ask user:

  1. What types of tasks do you typically perform?

    • Bug fixes
    • New features
    • Refactoring
    • Documentation
    • Exploration/research
  2. How long are your typical sessions?

    • Quick (5-10 minutes)
    • Medium (15-30 minutes)
    • Long (30+ minutes)
  3. Do you work on multiple projects?

    • Single project
    • 2-3 projects
    • Many projects

Phase 2: Identify Inefficiencies

2.1 Configuration Gaps

Setting Status Impact if Missing
Prompt caching ? Missing 90% savings on repeated content
Token-efficient tools ? Missing 14-70% on tool outputs
Model strategy ? Overpaying for simple tasks
Memories ? Re-reading files every session

2.2 Usage Pattern Issues

Common inefficiencies:

  1. Full file reads when symbols would suffice

    • Symptom: Reading 2000+ line files
    • Impact: 70% token waste
  2. No context loading at session start

    • Symptom: Repeated exploration
    • Impact: 60% token waste per session
  3. Wrong model for task complexity

    • Symptom: Using Opus/Sonnet for simple queries
    • Impact: 40-60% cost overage
  4. Full workflow for simple tasks

    • Symptom: Requirements/design for bug fixes
    • Impact: 50% workflow waste
  5. No memory utilization

    • Symptom: Same questions answered repeatedly
    • Impact: 50% repetition waste

Phase 3: Generate Recommendations

3.1 Priority Classification

Critical (immediate, 30%+ savings):

  • Enable prompt caching if disabled
  • Activate Serena if not active
  • Create core memories if missing

High (this week, 15-30% savings):

  • Enable token-efficient tools
  • Create additional memories
  • Configure adaptive planning

Medium (this month, 5-15% savings):

  • Fine-tune model selection
  • Add project-specific skills
  • Optimize workflows

Low (optional, <5% savings):

  • Advanced caching strategies
  • Custom optimizations
  • Edge case handling

3.2 Specific Recommendations

For each identified gap, provide:

### Recommendation: [Name]

**Priority**: [Critical/High/Medium/Low]
**Expected savings**: [X]% tokens, $[X] per month
**Implementation time**: [X] minutes

**Current state**:
[What we found]

**Recommended action**:
[What to do]

**Implementation steps**:
1. [Step 1]
2. [Step 2]
3. [Step 3]

**Verification**:
[How to confirm it's working]

Phase 4: Calculate Savings Projections

4.1 Baseline Estimation

Based on usage patterns:

Task Type Frequency Baseline Tokens Current State
Bug fixes X/week 20K each ?
Features X/week 45K each ?
Refactoring X/week 35K each ?
Documentation X/week 25K each ?
Exploration X/week 30K each ?

Weekly baseline: [X] tokens


4.2 Projected Savings

After implementing recommendations:

Recommendation Token Savings Cost Savings
[Rec 1] X% $X/month
[Rec 2] X% $X/month
[Rec 3] X% $X/month
Total X% $X/month

Phase 5: Generate Report

Output format:

# Token Optimization Report

**Generated**: [Date]
**Project**: [Project name or "Global"]

## Executive Summary

**Current state**: [Brief assessment]
**Potential savings**: [X]% tokens, $[X] per month
**Priority actions**: [X] critical, [X] high, [X] medium

## Current Configuration Assessment

### ✅ Enabled Optimizations
- [List what's already configured]

### ❌ Missing Optimizations
- [List what's missing]

### ⚠️ Suboptimal Configurations
- [List what could be improved]

## Usage Analysis

**Typical tasks**: [Task types]
**Session patterns**: [Duration, frequency]
**Primary inefficiencies**: [Main issues]

## Recommendations

### Critical Priority (Do Now)

#### 1. [Recommendation Name]
[Full recommendation details]

---

### High Priority (This Week)

#### 2. [Recommendation Name]
[Full recommendation details]

---

### Medium Priority (This Month)

#### 3. [Recommendation Name]
[Full recommendation details]

---

## Projected Savings

| Timeframe | Tokens Saved | Cost Saved |
|-----------|--------------|------------|
| Weekly | X | $X |
| Monthly | X | $X |
| Annually | X | $X |

## Implementation Roadmap

### Today (30 minutes)
- [ ] [Critical item 1]
- [ ] [Critical item 2]

### This Week (2 hours)
- [ ] [High priority items]

### This Month (4 hours)
- [ ] [Medium priority items]

## Verification Steps

After implementing:
1. Run `/cache-inspector status` - Check cache hit rate
2. Run `/context list` - Verify memories loaded
3. Complete a typical task - Compare tokens used
4. Track savings for one week

## Next Review

Schedule next optimization review: [Date + 1 month]

---
Report generated by Optimization Agent v1.0.0

EXAMPLE OUTPUTS

Example 1: New User (No Optimization)

# Token Optimization Report

## Executive Summary

**Current state**: No optimizations configured
**Potential savings**: 70-80% tokens, ~$25/month
**Priority actions**: 3 critical, 2 high, 2 medium

### Critical Priority

1. **Activate Serena**
   - Current: Not activated
   - Impact: Missing 65-75% savings on code exploration
   - Action: Run `mcp__serena__activate_project`

2. **Create Core Memories**
   - Current: No memories
   - Impact: Re-reading files every session
   - Action: Create architecture.md, codebase-conventions.md

3. **Enable Prompt Caching**
   - Current: Not configured
   - Impact: Missing 90% savings on repeated content
   - Action: Create prompt-caching.json

Example 2: Partial Optimization

# Token Optimization Report

## Executive Summary

**Current state**: Basic optimization in place
**Potential savings**: Additional 20-30%, ~$8/month
**Priority actions**: 0 critical, 2 high, 3 medium

### High Priority

1. **Enable Token-Efficient Tools**
   - Current: Not enabled
   - Impact: Missing 14-70% on tool outputs
   - Action: Add beta header to configuration

2. **Add Testing Strategy Memory**
   - Current: Have architecture + conventions
   - Impact: Reading test files each session
   - Action: Create testing-strategy.md

Example 3: Fully Optimized

# Token Optimization Report

## Executive Summary

**Current state**: Well optimized
**Potential savings**: Minimal additional (~5%)
**Priority actions**: 0 critical, 0 high, 2 medium

### Current Status: ✅ Excellent

All major optimizations configured:
- ✅ Serena activated with 5 memories
- ✅ Prompt caching enabled
- ✅ Token-efficient tools enabled
- ✅ Model strategy configured
- ✅ Adaptive planning active

### Medium Priority Refinements

1. **Update outdated memory**
   - architecture.md last updated 3 months ago
   - Action: Review and refresh

2. **Add project-specific skill**
   - Repeated workflow detected: deployment
   - Action: Create /deploy skill

ERROR HANDLING

Cannot Access Configuration

Fallback:

  • Ask user to describe current setup
  • Provide general recommendations
  • Suggest running setup-agent.md

Serena Not Available

Handle:

  • Note Serena is required for full optimization
  • Recommend Serena installation
  • Provide non-Serena optimization tips

No Usage History

Handle:

  • Use industry benchmarks
  • Ask user about typical tasks
  • Provide conservative estimates

POST-ANALYSIS ACTIONS

  1. Save report to ~/.claude/reports/optimization-YYYY-MM-DD.md
  2. Create todo list from recommendations
  3. Offer to implement critical items immediately
  4. Schedule follow-up review

This agent provides personalized optimization guidance based on your actual usage patterns.

After completion: Implement recommendations and track savings!