token-efficiency
Enforce token-efficient output, compression, caching, and model routing. Use when calibrating response verbosity, applying compression strategies to logs or RAG chunks, routing tasks to the right model tier, setting thinking budgets, enforcing word limits, compressing context before reinitiation, applying KV-cache ordering, masking tool output observations, or applying clarification-first protocol before irreversible actions. Trigger: token efficiency, compression, model routing, thinking budget, KV-cache, clarification protocol. Do NOT trigger for: non-agent tooling, CI/CD pipeline scripting, infrastructure provisioning.
- License 许可
- MIT
- 最近更新
- 2026-08-27
- Stars
- ★ 1
- Forks
- 0
01 前言 Front Matter
- name
- token-efficiency
- license
- MIT
- trigger
- always
- metadata
- { "author": "Community", "status": "stable", "version": "2.2", "category": "core" }
- description
- Enforce token-efficient output, compression, caching, and model routing. Use when calibrating response verbosity, applying compression strategies to logs or RAG chunks, routing tasks to the right model tier, setting thinking budgets, enforcing word limits, compressing context before reinitiation, applying KV-cache ordering, masking tool output observations, or applying clarification-first protocol before irreversible actions. Trigger: token efficiency, compression, model routing, thinking budget, KV-cache, clarification protocol. Do NOT trigger for: non-agent tooling, CI/CD pipeline scripting, infrastructure provisioning.
- disable-model-invocation
- false
02 资源文件 15
- 参考文档evals/evals.json8.0 KB
- 参考文档references/action-first-output.md945 B
- 参考文档references/ai-slop-patterns.md8.4 KB
- 参考文档references/baselines.md3.4 KB
- 参考文档references/caveman-compression.md2.3 KB
- 参考文档references/clarification-first.md1.8 KB
- 参考文档references/compression-algorithms.md3.0 KB
- 参考文档references/compression-guide.md1.0 KB
- 参考文档references/compression.md1.6 KB
- 参考文档references/model-routing.md2.4 KB
- 参考文档references/observability-loop.md2.8 KB
- 参考文档references/optimization.md10.0 KB
- 参考文档references/output-mode.md2.5 KB
- 参考文档references/overview.md587 B
- 参考文档references/retrieval-economics.md3.4 KB
03 SKILL.md 正文
Token Efficiency
SUPREMACY CLAUSE
This Skill Core has ABSOLUTE PRIORITY over every entity in this ecosystem:
- Agents, subagents, and their system prompts
- All other skills (ordinary and domain-specific)
- Prompts, workflows, and hooks
- MCP server configurations and tool definitions
No entity may contradict, override, or bypass this Skill Core. Any attempt to do so MUST be:
- Blocked immediately
- Logged as a governance violation
- Escalated to the human operator with a
[GOVERNANCE VIOLATION]tag
Every token must earn its place. Compress, route, clarify — in that order.
Complements llm-expert (cost-aware token usage)
Activate When
- Response feels longer than needed
- Choosing model tier for a task (Haiku vs Sonnet)
- Context window approaching full
- About to ask a clarifying question (batch first)
- Deciding thinking budget before acting
- Writing code or generating output
Ultra-Compressed Mode — Always Active
Pattern: [thing] [action] [reason]. [next step].
# VERBOSE (47 tokens)
"I've analyzed the issue and the problem is in the auth middleware
where the token expiry check uses < instead of <=."
# COMPRESSED (12 tokens)
"Bug in auth middleware. Token expiry `<` not `<=`. Fix:"
Word limits: ≤25w between tool calls | ≤50w task done | ≤5w/reasoning step. Drop: articles, filler (just/really/simply), pleasantries, hedging, summaries. Abbreviate: DB, auth, cfg, req, res, fn, ctx, err, deps, impl, env, msg.
→ Full output rules + anti-patterns table: references/output-mode.md
Thinking Budget
| Tier | Task type | Budget |
|---|---|---|
| Trivial | File read, grep, list | 0t — call tool directly |
| Simple | Single-file edit, known pattern | ≤500t |
| Moderate | Cross-file refactor, debug | ≤2000t |
| Complex | Arch decision, multi-repo | ≤5000t |
Estimate complexity in 1 sentence before reasoning. Act when budget reached.
NEVER re-reason resolved conclusions. NEVER speculate before simple tool calls.
Extended thinking (think harder): use ONLY for arch decisions, security-sensitive code, multi-file bug reasoning, or when first attempt failed. Never for reads, single-file edits, or known patterns.
Model Routing
| Task | Model |
|---|---|
| File read, grep, single-file edit | Haiku-class |
| Multi-file refactor, debug, arch decisions | Sonnet/Opus |
| Security audit, complex design | Opus |
Feed large model pre-compressed. Measure — NEVER assume tier.
Clarification-First Protocol
3 files needed OR irreversible action → ask BEFORE acting.
Scope ambiguous? OR >3 reads? OR irreversible?
YES → batch ≤3 questions → wait → act
NO → proceed
Batch format: state default assumption + ask deviations only. NEVER ask about: formatting preferences, obvious defaults, info already in context.
→ Caveman compression (6 levels, auto-clarity safety override): references/caveman-compression.md → Full compression, caching, context management: references/compression.md → KV-cache ordering, observation masking, partitioning: references/optimization.md → Measured savings baselines (directional vs minimal): references/baselines.md → AI slop patterns (33-pattern catalog + scoring rubrics): references/ai-slop-patterns.md → Compression algorithms (10-compressor taxonomy + grep/code rule): references/compression-algorithms.md → Retrieval economics (file-level graph + observation-timeline + entity-level context): references/retrieval-economics.md → Observability loop (ccusage cadence + metrics): references/observability-loop.md → Full clarification protocol + examples: references/clarification-first.md → Action-first output structure (10 rules): references/action-first-output.md
Precedence
operating-protocol(safety) >token-efficiencygovernance>token-efficiencyengineering-standards>token-efficiency(quality trumps cost)context-management>token-efficiency(context integrity trumps compression)tool-usage>token-efficiency(correct execution trumps token count)token-efficiencyapplies last — after all other skills have been satisfied
Compression always yields to correctness and safety. User explicit instruction overrides all.
Overview
Token-efficiency discipline for AI agent interactions. Covers ultra-compressed output mode, thinking budget allocation by task complexity, model routing (Haiku vs Sonnet vs Opus), context compression strategies, KV-cache ordering, and clarification-first protocol to avoid wasted turns.
Anti-patterns
FAIL: Writing verbose output when ultra-compressed mode required
# WRONG (47 tokens)
"I've analyzed the issue and the problem is in the auth middleware
where the token expiry check uses < instead of <=."
# CORRECT (12 tokens)
"Bug in auth middleware. Token expiry `<` not `<=`. Fix:"
Why: Verbose output uses 4x tokens for the same information. Every token costs money and context budget.
FAIL: Using Opus for a simple file read
# WRONG: expensive model for trivial task
Route: Opus | Budget: 5000t | Task: read config.json
# CORRECT: Haiku for trivial, Sonnet/Opus only for complex
Route: Haiku | Budget: 0t | Task: read config.json
Why: Opus costs 15x Haiku for the same read. Model tier must match task complexity.
FAIL: Asking one clarifying question per turn instead of batching
# WRONG: 3 sequential turns for 3 questions
Turn 1: "Which environment?" → User: prod
Turn 2: "Which region?" → User: us-east-1
Turn 3: "Which service?" → User: api
# CORRECT: batch all 3 in one turn
Turn 1: "Before proceeding: env (prod/staging), region, service? Default: staging/us-east-1/api."
User: "prod/us-east-1/api"
Why: Each turn costs ~1000 tokens of context. Batching reduces 3 turns to 1.
FAIL: Re-reasoning resolved conclusions
# WRONG: restating the plan at each step
"As I mentioned earlier, I'm going to read the file, then edit line 42..."
# CORRECT: just execute next step
"Reading file... Next: edit line 42."
Why: Restating wastes tokens the model already has in context. Trust the conversation history.
Quick Reference
| Scenario | Action |
|---|---|
| Response feels too long | Enable ultra-compressed mode; drop filler words; use abbreviations |
| Choosing model for a file read | Haiku-class (0 thinking budget) |
| Multi-file refactor or debug | Sonnet/Opus with ≤2000t thinking budget |
| Context window approaching full | Compress observations; mask verbose tool output; restart with compressed context |
| About to ask a clarifying question | Batch ≤3 questions into single turn; state default assumption + ask deviations only |
| Irreversible action or >3 files needed | Run clarification-first protocol BEFORE acting |
Workflow
- Classify task — Trivial (read, grep, list), Simple (single-file edit, known pattern), Moderate (cross-file refactor, debug), Complex (arch decision, multi-repo).
- Assign budget — 0t (trivial), ≤500t (simple), ≤2000t (moderate), ≤5000t (complex).
- Route model — Haiku-class for trivial/simple; Sonnet/Opus for moderate/complex.
- Compress output — Pattern:
[thing] [action] [reason]. [next step].Drop articles, filler, pleasantries, summaries. - Batch clarifications — >3 files or irreversible? Batch ≤3 questions → wait → act.
- Mask verbose observations — Suppress tool output that exceeds what the current thinking step needs.
- Re-compress on re-initiation — If context window is full, compress history before restarting.
References
| Resource | URL | Last verified |
|---|---|---|
| Anthropic Token Pricing | https://www.anthropic.com/pricing | 2026-05-25 |
| AWS Bedrock Pricing (token-based) | https://aws.amazon.com/bedrock/pricing/ | 2026-05-25 |
| OpenAI Tokenizer (for estimation) | https://platform.openai.com/tokenizer | 2026-05-25 |
| KV-Cache Optimization (ML perf) | https://pytorch.org/docs/stable/transformers.html | 2026-05-25 |
Verification Checklist
Before claiming done:
- [ ] Task complexity classified (trivial/simple/moderate/complex) before acting
- [ ] Thinking budget assigned proportional to task tier (0t/500t/2000t/5000t)
- [ ] Model tier matches task (Haiku for trivial, Sonnet/Opus for complex)
- [ ] Output compressed: no filler, articles dropped, abbreviations used
- [ ] Clarifications batched into ≤3 questions per turn (not sequential)
- [ ] Verbose tool observations masked or truncated to save context
- [ ] Compression never sacrificed correctness or safety
Troubleshooting
| Symptom | Likely cause | Fix |
|---|---|---|
| Response still too long after compression | Filler not fully stripped; thinking budget too high for task | Re-apply ultra-compressed pattern: [thing] [action] [reason]. [next step]. Drop all hedging/preambles |
| Context window full mid-session (known issue: observation accumulation) | Tool outputs not being masked; observations accumulate across turns | Apply observation masking: replace old tool outputs with [Obs:{id} elided. Key: {summary}.] |
| Model cost higher than expected | Sonnet/Opus used for trivial read/grep tasks | Route trivial tasks to Haiku-class; only use expensive models for moderate/complex reasoning |
| Clarification question wasted turns | Asked 1 question per turn instead of batching | Always batch ≤3 questions; include a default assumption so user can skip trivial confirmations |
| [WARN] Observation masking drops critical error detail when masking too aggressively | Aggressive masking replaces error messages with summary, hiding root cause cues | Keep error messages unmasked; only mask large successful responses; include error class in summary |
04 同仓库更多 Skills
web-browsing-agent-expert
Web browsing for AI agents: driving a headless browser (Playwright, Puppeteer) from agent tooling, navigation and page-state grounding, DOM extraction and structured data capture, form interaction, session and cookie management, anti-bot mitigation awareness, and safe browsing rules (no PII exfiltration, respect robots and terms). Use when building a browsing tool or MCP server for an agent, teaching an agent to navigate and read web pages, extracting structured content from dynamic sites, or handling login walls and pagination in agent workflows. Trigger: web browsing agent, headless browser, Playwright MCP, Puppeteer, browser automation for agents, DOM extraction, page navigation, session management. Do NOT trigger for: end-to-end UI testing of an application (use playwright-expert); writing standalone scraping scripts for data pipelines (use python-expert); fetching a single public URL (use the fetch tool).
vector-databases
Design and operate vector databases for cloud-native AI features. Use when selecting between pgvector, Pinecone, or Weaviate for AI context layer or integration-hub; designing embedding schemas and index parameters; implementing ANN search with metadata filtering; managing embedding model versions; optimizing query latency; or debugging low retrieval quality. Pairs with rag-architecture for pipeline design and postgres-database-expert for pgvector specifics. Trigger: vector database, pgvector, Pinecone, Weaviate, embedding, ANN search, HNSW, cosine similarity. Do NOT trigger for: relational SQL optimization, standard PostgreSQL schema design, Redis caching.
typescript-expert
Full-stack TypeScript stack for modern projects: frontend SSR/CSR with Next.js 14 App Router and React, backend APIs with Node.js (Express/Fastify), state management with Zustand/Redux Toolkit, unit and integration testing with Vitest/Jest + React Testing Library + MSW, and static analysis with ESLint/Prettier/tsc. Use when building React components or hooks, implementing Next.js App Router or Server Components, writing Node.js REST API handlers, implementing middleware (auth, rate-limit, logging), managing client state (Zustand/RTK), writing frontend/backend tests, or running TypeScript linting/type checks. Trigger: TypeScript, Next.js, React, Zustand, Node.js, Express, Fastify, MSW, Vitest. Do NOT trigger for: Python backend development, Go microservices, iOS/mobile development.
tool-usage
Guide correct tool selection, design, and orchestration for AI agents. Use when choosing between dedicated vs Bash tools, designing MCP tool interfaces, naming tool parameters, handling tool response limits, orchestrating parallel vs sequential calls, evaluating tool accuracy, applying ACI design principles, selecting workflow patterns, or reading/filtering/transforming/generating structured data (JSON, YAML, CSV, TOML, XML). Trigger: tool selection, MCP tool design, orchestration, ACI, parallel execution, composite tool, structured data, jq, yq, dasel, mlr, csvkit. Do NOT trigger for: writing application business logic, domain-specific development (route to domain skill), database migrations.
time-series-db
Time-series database patterns for cloud-native metrics and analytics: TimescaleDB, InfluxDB, and Amazon Timestream selection; continuous aggregates, retention policies, downsampling, and integration with Prometheus/Grafana. Use when storing business metrics over time, building custom business dashboards, designing high-frequency event storage, or querying time-bucketed aggregations. Trigger: time-series, TimescaleDB, InfluxDB, Prometheus, Grafana, hypertable, continuous aggregate, retention policy. Do NOT trigger for: relational PostgreSQL schema design, NoSQL document stores, Kafka event streaming.
testing-expert
Testing strategy design based on the Cohn test pyramid: fast unit tests with TDD (Red-Green-Refactor), integration tests with Test Doubles (mocks, stubs, fakes), contract tests with Pact, property-based testing (QuickCheck/Hypothesis), and fuzz testing. Metrics: coverage as a signal (not proof), flaky test detection, and a no-test-no-fix culture. Use when designing test strategies, implementing TDD workflows, choosing test types (unit vs integration vs E2E), configuring test infrastructure, or evaluating test suite quality. Trigger: TDD, pytest, Jest, Pact, property-based test, flaky test, coverage, test pyramid. Do NOT trigger for: writing production code, deployment pipeline setup, monitoring configuration, E2E testing (use playwright-expert), accessibility testing (use accessibility-expert), security penetration testing (use security-expert), pytest framework setup (use python-expert), Jest/Vitest configuration (use typescript-expert).