Loading...
Loading...
Token Optimization is the systematic reduction of token expenditure across agent operations without sacrificing output quality.
npx skill4agent add itallstartedwithaidea/agent-skills token-optimizationgraph 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| Iclass 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]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.5import { 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 |