optimize-context

optimize-context is a skill for Claude Code, Codex from richfrem/agent-plugins-skills. It costs 164 tokens per session (2,870 once invoked), scanned D, original, MIT.

A cleanup tool for an AI coding project's instructions and installed skills. It removes duplicate skill copies and shortens instruction files while keeping their rules.

In plain words
What is it for?
Auditing skill folders and optimizing CLAUDE.md, GEMINI.md, or GitHub Copilot instruction files, with options to preview changes or focus on one area.
Why use it?
AI tools can load the same skill twice or spend context on stale and overly long instructions. This reduces repeated or unnecessary text so the agent has more room for the actual task.

Skill for Claude CodeCodex

Part of the dev-utils plugin — 17 skills, 2 agents shipped together

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 skills/richfrem/agent-plugins-skills/optimize-context
Any agent
npx skills add richfrem/agent-plugins-skills --skill optimize-context
Clone the repo
git clone --depth 1 https://github.com/richfrem/agent-plugins-skills

Made for: Claude Code, Codex.

Or install dev-utils, the plugin that ships this one along with the rest of its 17 skills, 2 agents.

Wrote this? Show the measurements

A badge with what this costs and how it scanned, read live from this page, so it follows the numbers instead of freezing them. Markdown for a README, HTML for a documentation site or a project page.

agentmods badge for optimize-context

README.md
[![agentmods](https://agentmods.dev/badge/skills/richfrem/agent-plugins-skills/optimize-context.svg)](https://agentmods.dev/skills/richfrem/agent-plugins-skills/optimize-context)
Your own site
<a href="https://agentmods.dev/skills/richfrem/agent-plugins-skills/optimize-context"><img src="https://agentmods.dev/badge/skills/richfrem/agent-plugins-skills/optimize-context.svg" alt="Measured on agentmods" height="20"></a>
Per session 164 Skills are progressive disclosure: only the name and description are preloaded; the body loads when the skill is used.
When invoked 2,870 The whole file, excluding the scripts and references it only reads on demand.
Security scan D 2 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.00164 $0.02870
Opus 5 $0.00082 $0.01435
Sonnet 5 $0.00033 $0.00574
Haiku 4.5 $0.00016 $0.00287

Measured yesterday against content hash 07b5d73d5865, method: parsed. Prices are Anthropic first-party input rates as of 2026-08-30, from the pricing page.

Security

Grade D, and why

optimize-context scanned grade D with 2 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 yesterday.

The scan reads SKILL.md. This mod also ships 1 executable file (scripts/optimize_context.py), listed below but not scanned — reading those needs a real analyzer, not pattern matching.

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.

Enumerates other installed skillsmediumAgent snooping

Other skills' SKILL.md files reveal prompts, capabilities and secrets that should be invisible to peers.

ls .claude/skills 2>/dev/null | wc -l

Recursive force deletehighDestructive command

rm -rf with a variable or a broad path is one typo away from removing the wrong tree.

rm -rf .claude/skills/* .claude/agents/* .claude/commands/* .claude/hooks/*
plugins/dev-utils/skills/optimize-context/SKILL.md · 284 lines

How it starts

The opening of the file, as written. The whole thing — 284 lines — stays where its author put it; the contents beside it link to each section on GitHub.

optimize-context: Agent Context Hygiene

Two-pass context optimization. Run both passes each time unless the user specifies otherwise.


Prerequisites

  • plugins/ directory present (plugin-canonical skills source)
  • scripts/optimize_context.py available in this plugin
  • At least one instruction file readable at project root: CLAUDE.md, GEMINI.md, or .github/copilot-instructions.md

Phase 1: Intent Capture

Parse $ARGUMENTS:

  • --dry-run → report only, no writes
  • --verbose → print every skill found
  • --project-root PATH → override project root (default: CWD)
  • --skills-only → skip instruction file audit
  • --instructions-only → skip skill deduplication

Confirm with the user before writing in live mode.


Phase 2: Skill Deduplication

Root cause: Through experiment, we confirmed Claude Code auto-scans the entire repository for SKILL.md files (identifying them as "Plugin" skills). It also scans .claude/skills/ (identifying them as "Project" skills). Because the installer used to symlink everything into .claude/, Claude Code was loading every skill twice.

Read the full file on GitHub · 284 lines

Files

What ships with it

3 files beside SKILL.md in the same directory: the scripts, references and assets a skill reads on demand. Not counted in the per-session cost; read them before you install if any of them is executable.

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. yesterday First seen · 284 lines · 164 tokens per session scan D 07b5d73d5865

Subscribe to this mod's changes

optimize-context is a skill published in the GitHub repository richfrem/agent-plugins-skills (6 stars, last pushed yesterday), licensed MIT. It adds 164 tokens to every session and 2,870 once invoked, about $0.0008 per session on Opus 5. A static security scan graded it D with 2 findings (enumerates other installed skills, recursive force delete). No closer match exists in the catalogue, so it is treated as the original; first seen 2026-09-03.