Skill-Details

token-optimizer

Directly targets reduced token usage through context, summarization, and tool-call optimization.

ÜbereinstimmungDirektGeprüft für weniger tokenverbrauch
Quellekhilghard/meeting-program-devExterne Quelle
Gemeldete Installationen1Nur Popularitätssignal

Vor Nutzung prüfen

Die automatische Prüfung bewertet Relevanz, nicht Sicherheit oder Empfehlung. Lies vor der Nutzung die Quellanweisungen.

Gespeicherte Quellvorschau

SKILL.md

Dieser Auszug wurde bei der Prüfung gespeichert. Die externe Quelle enthält die vollständige und aktuelle Version.

---
name: token-optimizer
description: Monitors and reduces token usage in AI conversations. Provides tools for context window management, conversation summarization, and tool call optimization. Use when approaching token limits, optimizing prompts, or reducing AI costs.
license: MIT
metadata:
  audience: developers
  workflow: token-optimization
---

# Token Usage Optimizer

## What I Do

- Monitor context window usage in real-time
- Identify token-heavy patterns in conversations
- Suggest optimization strategies
- Implement conversation summarization
- Optimize tool call patterns
- Track token usage trends

## When to Use Me

Use this skill when:

- Context window is approaching limits
- AI responses are being truncated
- Tool definitions are too large
- Multi-step workflows are inefficient
- Need to reduce API costs
- Planning long-running conversations

## Token Usage Monitoring

### Context Window Breakdown

```javascript
// Typical context window allocation
const contextAllocation = {
  systemPrompt: 500, // System instructions
  toolDefinitions: 7000, // All tool schemas (TO REDUCE)
  conversationHistory: 50000, // Past messages
  currentPrompt: 2000, // Current request
  buffer: 10000, // Safety margin
  total: 128000 // Total context window
};

// Target allocation after optimization
const optimizedAllocation = {
  systemPrompt: 500,
  toolDefinitions: 500, // Dynamic loading (93% reduction)
  conversationHistory: 30000, // Summarized old turns
  currentPrompt: 5000, // More detailed prompt
  buffer: 20000, // Larger buffer
  total: 128000
};
```

### Token Estimation

```javascript
// Rough token estimation (1 token ≈ 4 characters)
function estimateTokens(text) {
  return Math.ceil(text.length / 4);
}

// More accurate estimation using tiktoken
function estimateTokensAccurate(text, model = "gpt-4") {
  // Use tiktoken library for accurate counts
  const encoder = getEncoding(model);
  const tokens = encoder.encode(text);
  return tokens.length;
}
```

## Optimization Strategies

### 1. Dynamic Tool Loading (93% Token Reduction)

#### Before (Static Injection)

```json
// All 14 tools sent every request
{
  "tools": [
    {"name": "bash", "description": "...", "parameters": {...}},
    {"name": "edit", "description": "...", "parameters": {...}},
    // ... 12 more tools (~7000 tokens)
  ]
}
```

#### After (Dynamic Loading)

```json
// Compact catalog with search
{
  "tools": {
    "catalog": [
      { "name": "bash", "description": "Execute shell commands" },
      { "name": "edit", "description": "Modify files" }
      // ... compact descriptions (~500 tokens)
    ],
    "search": "tool_search(query: string)"
  }
}
```

**Implementation:**

```javascript
// Tool registry
const toolRegistry = {
  catalog: [
    { name: "bash", description: "Execute shell commands", category: "file" },
    { name: "edit", description: "Modify files", category: "file" },
    { name: "read", description: "Read files", category: "file" },
    { name: "glob", description: "Find files by pattern", category: "file" },
    { name: "grep", description: "Search file contents", category: "file" },
    { name: "list", description: "List directory contents", category: "file" },
    { name: "write", description: "Create/overwrite files", category: "file" },
    { name: "skill", description: "Load agent skills", category: "agent" },
    { name: "webfetch", description: "Fetch web content", category: "web" },
    { name: "websearch", description: "Search the web", category: "web" }
  ],

  // Search tools by keyword
  search(query) {
    return this.catalog.filter(
      (tool) =>
        tool.name.includes(query) ||
        tool.description.includes(query) ||
        tool.category.includes(query)
    );
  },

  // Load full schema for specific tools
  loadTools(toolNames) {
    return toolNames.map((name) => this.getFullSchema(name));
  }
};
```

### 2. Conversation Summarization

#### When to Summarize

```javascript
// Summarize when conversation
Vollständige Quelle auf GitHub lesen (öffnet externe Seite)
Kontext

Verwandte Arbeit