Skill 詳細

token-optimizer

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

一致度直接一致トークン使用量の削減 向けにレビュー済み
出典khilghard/meeting-program-dev外部ソース
報告インストール数1人気度の参考値

使用前に確認

自動レビューは関連性のみを確認し、安全性や推奨を保証しません。使用前に出典の説明を読んでください。

保存された出典プレビュー

SKILL.md

これはレビュー時に保存された抜粋です。完全で最新の内容は外部ソースを確認してください。

---
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
GitHub で全文を読む (外部ページ)
関連情報

関連する仕事