token-optimization
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseToken Optimization
Token Optimization
Part of Agent Skills™ by googleadsagent.ai™
属于 Agent Skills™ 的一部分,由 googleadsagent.ai™ 提供
Description
描述
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.
Token Optimization 是在不牺牲输出质量的前提下,系统性地减少 Agent 操作过程中的 Token 消耗。在生产级 AI 系统中,Token 同时是成本和延迟的基本计量单位——每一个不必要的 Token 都会增加 API 账单,并拖慢响应时间。本 Skill 归纳了 Everything Claude Code 生态(150k+ stars)和 googleadsagent.ai™ 生产平台中使用的优化技术;在该平台上,Buddy™ 每天在严格的成本预算内处理数千次 Google Ads 分析。
优化涵盖四个维度:模型选择(使任务复杂度与模型能力和成本相匹配)、Prompt 压缩(在保持指令完整性的同时移除冗余 Token)、后台处理(将高开销操作卸载到异步工作流),以及缓存(避免对相同或相似输入进行重复计算)。同时实施这四个维度的生产系统,与直接实现相比,通常可降低 60-80% 的 Token 成本。
Token 优化的重点不是省钱,而是提高效率。如果 Agent 在冗长的系统 Prompt 或重复的工具输出上浪费 Token,它不仅成本高昂,还会更快填满 Context Window,为真正的推理留下更少空间。优化可以同时改善经济性和质量。
Use When
何时使用
- Monthly API costs exceed budget targets for AI agent operations
- Response latency is above acceptable thresholds for user-facing agents
- Context windows are filling up before complex tasks can complete
- Multiple model tiers are available and you need intelligent routing
- Batch processing workloads generate high token volumes
- You need to scale agent usage without proportional cost increases
- AI Agent 运行的月度 API 成本超出预算目标
- 面向用户的 Agent 响应延迟高于可接受阈值
- 复杂任务尚未完成,Context Window 就已被填满
- 有多个模型层级可用,需要进行智能路由
- 批处理工作负载产生大量 Token
- 需要在不按比例增加成本的情况下扩展 Agent 使用规模
How It Works
工作原理
mermaid
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| ITasks 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.
mermaid
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任务首先进入复杂度分类器,再路由到适当的模型层级。Prompt 压缩器会移除冗余内容、缩短冗长指令,并将叙述性描述替换为结构化格式。缓存层会拦截重复或近似重复的查询。适合后台处理的任务(非交互式分析、批量操作)会在非高峰时段排队进行异步处理。每个阶段都会强制执行 Token 预算,硬性限制每次操作的消耗。
Implementation
实现
Task Complexity Classifier:
python
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:
python
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.5Semantic Cache Layer:
typescript
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 };
}
}任务复杂度分类器:
python
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 压缩引擎:
python
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语义缓存层:
typescript
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 };
}
}Best Practices
最佳实践
- Measure before optimizing — instrument token usage per operation type (system prompt, user message, tool results, generation) to identify the highest-impact optimization targets.
- Right-size the model — use fast/cheap models for simple tasks (formatting, summarization) and reserve expensive models for complex reasoning; most tasks don't need the most capable model.
- Compress system prompts aggressively — system prompts are sent with every request; a 30% compression here pays dividends across every invocation.
- Cache at the semantic level — exact-match caching catches duplicates; semantic similarity caching catches near-duplicates that would produce identical results.
- Set per-operation token budgets — hard limits prevent runaway costs from unexpectedly verbose model outputs or circular tool-calling loops.
- Prefer structured formats over prose — JSON and YAML carry more information per token than natural language descriptions.
- Truncate tool outputs intelligently — return only the fields the model needs, not the full API response; a Google Ads API response can be reduced from 50k tokens to 2k tokens with selective field extraction.
- Monitor cost per successful task — track not just raw token usage but tokens-per-completed-task to capture the impact of retries and failures.
- 先测量,再优化——按操作类型(系统 Prompt、用户消息、工具结果、生成结果)记录 Token 用量,以确定影响最大的优化目标。
- 选择合适规模的模型——对简单任务(格式化、摘要)使用快速/低成本模型,将高成本模型留给复杂推理;大多数任务并不需要能力最强的模型。
- 大力压缩系统 Prompt——系统 Prompt 会随每次请求一并发送;在此处压缩 30% 的收益会在每次调用中不断累积。
- 在语义层面缓存——精确匹配缓存可捕获完全重复的查询;语义相似性缓存可捕获会产生相同结果的近似重复查询。
- 设置单次操作的 Token 预算——硬性限制可以防止模型输出异常冗长或工具调用陷入循环所导致的成本失控。
- 优先使用结构化格式而非自然语言文本——JSON 和 YAML 相比自然语言描述,每 Token 能承载更多信息。
- 智能截断工具输出——只返回模型需要的字段,而不是完整的 API 响应;通过选择性字段提取,可将 Google Ads API 响应从 50k Token 缩减到 2k Token。
- 监控每个成功任务的成本——不仅要跟踪原始 Token 用量,还要跟踪每个已完成任务的 Token 数,以反映重试和失败的影响。
Platform Compatibility
平台兼容性
| 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 |
| 功能 | Claude Code | Cursor | Codex | Gemini CLI |
|---|---|---|---|---|
| 模型路由 | ✅ --model 参数 | ✅ 模型选择器 | ✅ 模型配置 | ✅ 模型参数 |
| Prompt 压缩 | ✅ 完整支持 | ✅ 完整支持 | ✅ 完整支持 | ✅ 完整支持 |
| 响应缓存 | ✅ 自定义 | ✅ 自定义 | ✅ 自定义 | ✅ Context 缓存 |
| 后台任务 | ✅ Subagents | ✅ Subagents | ✅ Async | ✅ Async |
| Token 预算 | ✅ max_tokens | ✅ max_tokens | ✅ max_tokens | ✅ max_tokens |
Related Skills
相关 Skill
- Multi-Model Routing - Model routing is the highest-leverage technique for token cost reduction
- Knowledge Base Injection - Top-K pattern selection prevents context window bloat from excessive injection
- Memory Persistence - Memory injection budgets must be balanced against current-task context needs
- Budget Optimization - Applies analogous cost-efficiency optimization principles to ad spend allocation
- Multi-Model Routing - 模型路由是降低 Token 成本收益最高的技术手段
- Knowledge Base Injection - Top-K 模式选择可避免注入过多内容导致 Context Window 膨胀
- Memory Persistence - Memory 注入预算必须与当前任务的 Context 需求相平衡
- Budget Optimization - 将类似的成本效率优化原则应用于广告支出分配
Keywords
关键词
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
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