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 阅读完整来源 (打开外部页面)
相关上下文

相关工作