一键导入
token-optimization
Token Optimization is the systematic reduction of token expenditure across agent operations without sacrificing output quality.
用 Codex 或 Claude 帮你安装 复制这段 Prompt,粘贴到 Codex、Claude 或其他助手里,让它检查 Skill 页面并帮你完成安装。
菜单
Token Optimization is the systematic reduction of token expenditure across agent operations without sacrificing output quality.
用 Codex 或 Claude 帮你安装 复制这段 Prompt,粘贴到 Codex、Claude 或其他助手里,让它检查 Skill 页面并帮你完成安装。
基于 SOC 职业分类
Parallel Agent Orchestration is the discipline of dispatching, coordinating, and aggregating results from multiple concurrent subagents to dramatically accelerate complex tasks.
Proactive Intelligence enables agents to autonomously seek out external information — web searches, API re-pulls, data freshness checks — during analysis without waiting for explicit user requests
Context Engineering is the discipline of maximizing agent output quality while minimizing token expenditure.
Prompt Architecture is the structural engineering of agent instructions.
Verification Loops are systematic evaluation pipelines that validate agent outputs at every stage of execution.
Continuous Learning enables agents to automatically extract successful patterns from completed sessions and codify them into reusable skills, rules, and prompt refinements.
| name | token-optimization |
| description | Token Optimization is the systematic reduction of token expenditure across agent operations without sacrificing output quality. |
Part of Agent Skills™ by googleadsagent.ai™
Token Optimization is the systematic reduction of token expenditure across agent operations without sacrificing output quality. In production AI systems, tokens are the fundamental unit of both cost and latency — every unnecessary token increases API bills and slows response times. This skill codifies the optimization techniques used in the Everything Claude Code ecosystem (150k+ stars) and the googleadsagent.ai™ production platform, where Buddy™ processes thousands of Google Ads analyses daily within strict cost budgets.
The optimization surface spans four dimensions: model selection (matching task complexity to model capability and cost), prompt compression (removing redundant tokens while preserving instruction fidelity), background processing (offloading expensive operations to async workflows), and caching (avoiding redundant computation for identical or similar inputs). Production systems that implement all four dimensions typically achieve 60-80% token cost reduction compared to naive implementations.
Token optimization is not about being cheap — it is about being efficient. An agent that wastes tokens on verbose system prompts or redundant tool outputs is not only expensive; it fills its context window faster, leaving less room for actual reasoning. Optimization improves both economics and quality simultaneously.
graph TD
A[Incoming Task] --> B[Complexity Classifier]
B -->|Simple| C[Fast Model<br/>Haiku/Flash]
B -->|Medium| D[Balanced Model<br/>Sonnet/GPT-4o]
B -->|Complex| E[Premium Model<br/>Opus/o1]
C --> F[Prompt Compressor]
D --> F
E --> F
F --> G{Cache Hit?}
G -->|Yes| H[Return Cached Result]
G -->|No| I[Execute with Budget]
I --> J[Cache Result]
J --> K[Response]
H --> K
I --> L{Background Eligible?}
L -->|Yes| M[Async Queue]
M --> I
L -->|No| I
Tasks enter through a complexity classifier that routes to the appropriate model tier. The prompt compressor strips redundant content, shortens verbose instructions, and replaces narrative descriptions with structured formats. A cache layer intercepts repeated or near-duplicate queries. Background-eligible tasks (non-interactive analysis, batch operations) are queued for async processing outside peak hours. Every stage enforces a token budget that hard-limits expenditure per operation.
Task Complexity Classifier:
class ComplexityClassifier:
THRESHOLDS = {
"simple": {"max_tokens": 500, "patterns": ["summarize", "format", "list", "count"]},
"medium": {"max_tokens": 2000, "patterns": ["analyze", "compare", "explain", "review"]},
"complex": {"max_tokens": 8000, "patterns": ["architect", "refactor", "debug", "optimize"]},
}
def classify(self, task: str) -> str:
task_lower = task.lower()
scores = {}
for level, config in self.THRESHOLDS.items():
score = sum(1 for p in config["patterns"] if p in task_lower)
scores[level] = score
if scores["complex"] > 0:
return "complex"
if scores["medium"] > 0:
return "medium"
return "simple"
def select_model(self, complexity: str) -> dict:
models = {
"simple": {"name": "claude-haiku", "cost_per_1k": 0.00025, "max_tokens": 1024},
"medium": {"name": "claude-sonnet", "cost_per_1k": 0.003, "max_tokens": 4096},
"complex": {"name": "claude-opus", "cost_per_1k": 0.015, "max_tokens": 8192},
}
return models[complexity]
Prompt Compression Engine:
class PromptCompressor:
REPLACEMENTS = [
(r"\s+", " "),
(r"Please note that ", ""),
(r"It is important to ", ""),
(r"Make sure to ", ""),
(r"You should ", ""),
(r"In order to ", "To "),
(r"At this point in time", "Now"),
]
def compress(self, prompt: str, target_reduction: float = 0.3) -> str:
compressed = prompt
for pattern, replacement in self.REPLACEMENTS:
compressed = re.sub(pattern, replacement, compressed)
original_tokens = count_tokens(prompt)
compressed_tokens = count_tokens(compressed)
reduction = 1 - (compressed_tokens / original_tokens)
if reduction < target_reduction:
compressed = self.structural_compress(compressed, target_reduction)
return compressed.strip()
def structural_compress(self, text: str, target: float) -> str:
lines = text.split("\n")
scored = [(line, self.line_importance(line)) for line in lines]
scored.sort(key=lambda x: x[1], reverse=True)
result = []
tokens = 0
budget = int(count_tokens(text) * (1 - target))
for line, score in scored:
line_tokens = count_tokens(line)
if tokens + line_tokens <= budget:
result.append(line)
tokens += line_tokens
return "\n".join(result)
def line_importance(self, line: str) -> float:
if line.strip().startswith("#"):
return 1.0
if any(kw in line.lower() for kw in ["must", "required", "never", "always"]):
return 0.9
if line.strip().startswith("-") or line.strip().startswith("*"):
return 0.7
return 0.5
Semantic Cache Layer:
import { createHash } from "crypto";
interface CacheEntry {
result: string;
timestamp: number;
tokens_saved: number;
model: string;
}
class SemanticCache {
private cache: Map<string, CacheEntry> = new Map();
private ttlMs: number;
private totalSaved = 0;
constructor(ttlMinutes = 60) {
this.ttlMs = ttlMinutes * 60 * 1000;
}
private keyFor(prompt: string, model: string): string {
const normalized = prompt.toLowerCase().replace(/\s+/g, " ").trim();
return createHash("sha256").update(`${model}:${normalized}`).digest("hex").slice(0, 32);
}
get(prompt: string, model: string): CacheEntry | null {
const key = this.keyFor(prompt, model);
const entry = this.cache.get(key);
if (!entry) return null;
if (Date.now() - entry.timestamp > this.ttlMs) {
this.cache.delete(key);
return null;
}
this.totalSaved += entry.tokens_saved;
return entry;
}
set(prompt: string, model: string, result: string, tokensUsed: number): void {
const key = this.keyFor(prompt, model);
this.cache.set(key, {
result,
timestamp: Date.now(),
tokens_saved: tokensUsed,
model,
});
}
stats(): { entries: number; totalTokensSaved: number } {
return { entries: this.cache.size, totalTokensSaved: this.totalSaved };
}
}
| Feature | Claude Code | Cursor | Codex | Gemini CLI |
|---|---|---|---|---|
| Model routing | ✅ --model flag | ✅ Model selector | ✅ Model config | ✅ Model flag |
| Prompt compression | ✅ Full | ✅ Full | ✅ Full | ✅ Full |
| Response caching | ✅ Custom | ✅ Custom | ✅ Custom | ✅ Context caching |
| Background tasks | ✅ Subagents | ✅ Subagents | ✅ Async | ✅ Async |
| Token budgets | ✅ max_tokens | ✅ max_tokens | ✅ max_tokens | ✅ max_tokens |
token-optimization, cost-reduction, model-routing, prompt-compression, caching, batch-processing, token-budget, model-selection, latency-optimization, agent-skills
© 2026 googleadsagent.ai™ | Agent Skills™ | MIT License