context-optimization

A method for reducing the amount of text and data sent to an AI model while measuring cost, latency, and output quality.

In plain words
What is it for?
Use it when prompts are large, response times are high, context limits are near, or per-call costs exceed the target.
Why use it?
It prevents unsupported claims that a shorter prompt is cheaper or faster by requiring measurements before and after each change.

Skill for Claude CodeCodex

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/rbraga01/builder-ai/context-optimization
Any agent
npx skills add RBraga01/builder-ai --skill context-optimization
Clone the repo
git clone --depth 1 https://github.com/RBraga01/builder-ai

Made for: Claude Code, Codex.

Per session 48 Skills are progressive disclosure: only the name and description are preloaded; the body loads when the skill is used.
When invoked 1,282 The whole file, excluding the scripts and references it only reads on demand.
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.00048 $0.01282
Opus 5 $0.00024 $0.00641
Sonnet 5 $0.00010 $0.00256
Haiku 4.5 $0.00005 $0.00128

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

Security

Grade A, and why

context-optimization 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.

skills/context-optimization/SKILL.md · 138 lines

How it starts

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

Context Optimization

The Law

PROMPT COST IS NOT OPTIMISED BY GUESSING.
"I already have a short prompt" is a guess about token count.
"Reducing context will hurt quality" is a guess about the quality/cost curve.
Measure first. Apply the hierarchy. Measure again. THEN claim improvement.

When to Use

Trigger when:

  • Prompt + context exceeds 60% of the model's context window
  • p95 latency is above target
  • Cost per 1k calls exceeds budget
  • RAG pipeline is stuffing too many chunks into each call
  • Output quality is degrading on long inputs (long context dilution)

When NOT to Use

  • The feature has < 100 calls/month and total monthly cost is < $20 — optimisation ROI is negative at this volume
  • Quality is currently below threshold — fix quality first; optimising a broken pipeline only makes it cheaper to be wrong
  • The system prompt changes every call — prompt caching (Level 5) has no effect; skip straight to Level 1–3

The Reduction Hierarchy

Apply in order. Stop when the target is met. Do not apply all steps preemptively.

Level 1 — Trim the System Prompt

Audit every sentence:

  • Run the prompt without each instruction — does output quality change?
  • Remove any instruction the model follows without it
  • Remove few-shot examples that duplicate knowledge the model already has
  • Replace verbose explanations with single directives

Target: system prompt under 500 tokens for most tasks. Measure token count precisely:

import tiktoken
enc = tiktoken.get_encoding("cl100k_base")
print(len(enc.encode(system_prompt)))

Level 2 — Reduce Retrieved Context

If using RAG, before any other change:

  1. Reduce top_k by 1 and run eval-before-ship — recall often holds at lower top_k
  2. Add or tighten a similarity threshold (filter chunks below 0.75 cosine similarity)
  3. Set a hard context budget: MAX_CONTEXT_TOKENS = context_window × 0.4

Level 3 — Compress Retrieved Content

Before injecting:

  • Strip document headers, footers, and repeated boilerplate
  • Normalise whitespace and remove formatting artefacts
  • Convert verbose JSON to compact tabular format
  • For chunks over 600 tokens: LLM-summarise to 1/3 length with a fast model

Read the full file on GitHub · 138 lines

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 · 138 lines · 48 tokens per session scan A 139a70ad55ad

Subscribe to this mod's changes

context-optimization is a skill published in the GitHub repository RBraga01/builder-ai (2 stars, last pushed 1mo ago), licensed MIT. It adds 48 tokens to every session and 1,282 once invoked, about $0.0002 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-31.

Related

Other skills, from other repositories

metrics-instrumentation

Specification for instrumenting an opik-backend workflow with operational OpenTelemetry metrics — per-stage throughput/latency/error counters and native histograms, dimensioned per-customer (workspace). Use when a pipeline (scoring, ingestion, experiments, jobs) needs per-stage visibility. Covers metric emission only…

comet-ml/opik · 93 tokens

comet-verify

Comet Phase 4: Verify and Close. Invoke with /comet-verify. Verify implementation matches design, handle development branch.

rpamis/comet · 32 tokens

comet-design

Comet Classic 阶段 2 —— 为 change 产出深度技术 Design Doc。.

rpamis/comet · 24 tokens

happiness-skill

当用户问「怎么才能更幸福/为什么得到了还不满足/怎么减少焦虑」时调用。 核心理念: 幸福是缺憾感清空的默认状态, 是可训练的技能; 欲望是与自己的契约(得到前不快乐), 同时只留一个重大欲望; 活在当下。 不适用于: 临床抑郁等需要专业治疗的场景(本书方法不能替代医疗)。 Triggers: 幸福/不快乐/欲望/焦虑/知足/活在当下/happiness/desire/anxiety.

kangarooking/cangjie-skill · 136 tokens

comet-github

将 Comet GitHub 维护请求路由到基于证据的 PR 审阅、Issue 分诊、本地想法收集、CI 诊断或 Issue 实施流程。用户提到 Comet GitHub Issue/PR 但未指定流程,或询问下一步如何处理时使用。.

rpamis/comet · 72 tokens

superlint

This skill describes the mandatory standard operating procedure for using our internal SuperLint tool. Use this when tasks require fixing code quality issues according to corporate standards.

mgechev/skillgrade · 0 tokens