token-optimization

Diagnose and reduce LLM token usage across prompts, context, agent loops, and output.

1|1|Updated May 24, 2026
One-click install
npx skills add https://github.com/bm629/agent-skills --skill token-optimization-bm629
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: token-optimization
Source: https://github.com/bm629/agent-skills/tree/main/skills/token-optimization
Command: npx skills add https://github.com/bm629/agent-skills --skill token-optimization-bm629

SYSTEM DOCUMENTATION & REQUIREMENTS

💡 This Skill includes references (resource) components.

What problem does it solve? Agent sessions burn tokens through bloated system prompts, unbounded tool output, long conversation histories, and oversized models on simple tasks, driving up API cost and hitting context-window limits. This Skill provides a layered triage workflow that measures where tokens go and applies the matching optimization tactic. ## Core Features & Use Cases - Bucketed measurement: Estimate token share across system prompt, tool definitions, history, tool results, and output using heuristic rules (words × 1.3, chars / 4) or exact count_tokens endpoints. - Layered tactic catalog: Prompt caching with stable prefixes, instruction-file slimming, history compression, observation masking, model routing by complexity, parallel tool calls, and output trimming with depth tiers and stop sequences. - Cross-host translation table: Maps each tactic to concrete commands for Claude Code, Codex CLI, Cursor, Gemini CLI, and Copilot CLI. - Use Case: An agent loop crosses 70% of its context window and costs keep climbing. Run the workflow to find that tool results dominate, mask verbose file reads into re-fetchable references, route routine calls to a cheaper model tier, and verify a 40% per-task token reduction. ## Quick Start Use token-optimization to analyze where my agent session's tokens are going and propose the top changes to cut cost.

Frequently Asked Questions about token-optimization

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How do I reduce token usage in an AI agent loop?

Measure which bucket dominates first (system prompt, tool definitions, history, tool results, or output), then apply the matching tactic: cache stable prefixes, slim always-loaded files, compress old turns, mask verbose tool output, and batch independent tool calls into one turn.

How does prompt caching reduce API cost?

Prompt caching hashes a stable prefix (system prompt, tool definitions, reference docs) once and reuses it at a heavy discount on later calls within the TTL. Keep the prefix byte-identical, place changing content at the end, and set the cache breakpoint after the largest stable block.

How can I estimate token counts without a tokenizer?

Use heuristic rules accurate to roughly ±15%: English prose is words × 1.3, code is characters / 4, and CJK text is characters / 2. For exact counts, call the provider's count_tokens endpoint before the real request.

Does this work with Claude Code, Cursor, and other agent CLIs?

Yes, the tactics are provider-agnostic and a translation table maps each one to host-specific commands like /compact, /clear, /model, and MAX_THINKING_TOKENS for Claude Code, Codex CLI, Cursor, Gemini CLI, and Copilot CLI.

Why did my optimization not reduce total cost?

The dominant bucket was likely misidentified, or a per-call saving increased the loop count, raising tokens-per-task. Re-measure all buckets fresh, verify against an end-to-end task total rather than a single call, and retarget the actual dominant bucket.

When should I not compress conversation history?

Never prune system instructions, safety headers, or the primary objective, and avoid compressing mid-task or during active debugging and multi-file refactors. Compress at logical boundaries such as after exploration or a completed milestone.