token-optimizer

A set of rules that routes file reading, searching, file finding, and large edits through token-optimizer tools when those tools are available.

In plain words
What is it for?
It is for reading changed portions of files, searching project contents, finding files by pattern, and editing large files with bounded output.
Why use it?
It helps limit unnecessary output and repeated file content in the coding agent's context, while falling back to built-in tools when the optimizer is unavailable.

Cursor rule

Install

Getting it into your agent

One page per mod, every tool's command on it. A separate URL per tool would split the same page into five that compete with each other.

agentmods
npx agentmods add rules/ooples/token-optimizer-mcp/token-optimizer
Clone the repo
git clone --depth 1 https://github.com/ooples/token-optimizer-mcp
Per session 619 This file is loaded in full into every session.
When invoked 619 The same file — it is already loaded in full.
Security scan A 0 findings. Scan, not verified.
Origin original No closer match found in the catalogue.
Token cost

What it costs to keep this loaded

Counted locally with the o200k_base tokenizer, which is exact for GPT models; Claude uses its own tokenizer and its counts differ. Treat this as one consistent yardstick across the catalogue rather than a bill. Prices are per million input tokens.

ModelPer sessionOnce invoked
Fable 5 $0.00619 $0.00619
Opus 5 $0.00309 $0.00309
Sonnet 5 $0.00124 $0.00124
Haiku 4.5 $0.00062 $0.00062

Measured 2d ago against content hash 4b09a30ba0e0, method: parsed. Prices are Anthropic first-party input rates as of 2026-08-30, from the pricing page.

Security

Grade A, and why

token-optimizer scanned grade A with 0 findings against 26 rules in 11 categories — prompt injection, anti-refusal, data exfiltration, privilege escalation, supply chain, agent snooping, system-prompt leakage, SSRF and excessive agency — measured 2d ago.

A static scan of the body, not an audit. Every finding is printed with the line that produced it so you can judge whether it matters here. A mod is markdown that instructs an agent; that is exactly why what it instructs is worth reading.

Nothing flagged

None of the 26 patterns this scan looks for appear in this file: no shell pipes, no recursive deletes, no credential paths, no hidden text, no instruction-override or anti-refusal phrasing, no agent-config snooping. That is not a guarantee, it is the absence of the things that are checkable.

integrations/cursor/token-optimizer.mdc · 52 lines

What it actually says

Use a token-optimizer MCP tool only when that exact tool is visible in the current CLI's registered tool inventory. A config file or installed plugin is not proof that the server started successfully. If a named optimizer tool is absent, keep the native tool available and use a bounded native operation; never retry or redirect to an unregistered schema.

When registered, you MUST use the token-optimizer MCP tools for every route listed below instead of the corresponding built-in file, search, or shell tool. This is mandatory routing policy, not a preference: the optimized tools cache, diff, and bound output. Cursor's packaged native hook enforces the pre-tool routes below and injects graph findings when its lifecycle permits.

ALWAYS:

  • Reading a file over ~25 KB, or ANY file already read this session -> smart_read (on a repeat it returns only what changed, not the file)
  • Searching file contents -> smart_grep
  • Finding files by name or pattern -> smart_glob
  • Editing a file over ~25 KB -> smart_edit (returns a diff, not the whole file)
  • Printing a large file via cat/head/tail/type/Get-Content -> smart_read
  • Recursive shell searches (grep -r, rg) -> smart_grep

WHEN CONTEXT IS TIGHT: call optimize_session to move prior file operations out of context. Call get_optimization_report to show the user what was saved.

STASHING BULKY OUTPUT: optimize_text stores it under a key, out of context. Do NOT use compress_text for that -- its base64 output has MORE tokens than the input; it is for at-rest storage only.

LIVE GRAPH — THE ACTIVE MODEL DOES THE SEMANTIC HARVEST:

  • When wiki_write is visible in the current tool inventory, call it as soon as you establish a durable, non-obvious conclusion: a failed approach and why, a decision and its rejected alternative, or the command that finally worked.
  • Anchor every claim to a real file path or path#symbol. Never invent a claim merely to populate the graph, and do not delegate harvesting to another model.
  • Include the concrete evidence, when it applies, confidenceLabel (verified/probable/speculative), scope (project/organization/global), and any condition that would invalidate it. Use project scope unless transfer is genuinely justified.
  • Before finishing substantive work, reflect once and write any still-unrecorded conclusion while you hold the reasoning. This is what makes the lesson available across sessions and projects instead of losing it to compaction.
  • If wiki_write is absent, do not claim the conclusion was harvested. Continue the work with native tools; lifecycle hooks may still capture structural facts.

NOT WORTH IT: small one-off reads, tiny edits. The built-ins are fine there -- the overhead would exceed the saving.

Changes

What this file has done since we first saw it

Hashed on every crawl. A supply-chain change to an agent config is a question of when, not whether, so the history is kept rather than the latest state alone.

  1. 2d ago First seen · 52 lines · 619 tokens per session scan A 4b09a30ba0e0

Subscribe to this mod's changes

token-optimizer is a cursor rule published in the GitHub repository ooples/token-optimizer-mcp (501 stars, last pushed 2d ago), licensed MIT. It adds 619 tokens to every session, about $0.0031 per session on Opus 5. A static security scan graded it A with 0 findings. No closer match exists in the catalogue, so it is treated as the original; first seen 2026-08-30.