Agent skill · writing content · jeremylongshore

openrouter-context-optimization

Optimize context window usage for OpenRouter models to reduce cost and

Why this skill is useful

Adds executable scripts and specific strategies for optimizing context window usage in OpenRouter models, which are not commonly found in public documentation.

What it needs

Requires curl, jq, node, python installed locally. Requires openrouter account access. About 5k tokens when loaded. Last updated 2026-08-07. 2,604 stars on the source repository.

What this skill does

OpenRouter Context Optimization Overview OpenRouter models have varying context windows (4K to 1M+ tokens). Since pricing is per-token, stuffing unnecessary context wastes money and can degrade output quality. This skill covers context window lookup, token estimation, conversation trimming, chunking strategies, and Anthropic prompt caching for large contexts. Prerequisites An OpenRouter API key (sk-or-v1-...) exported as OPENROUTERAPIKEY — see the openrouter-install-auth skill for setup Python 3.8+ with the OpenAI SDK and requests for model-metadata lookup; tiktoken for exact token counting per the references curl and jq to query context windows and pricing from /api/v1/models Node.js 18+ if you use the TypeScript context-budget calculator in the references Instructions 1. Run the Query Context Limits one-liner — it returns contextlength and prompt price per 1M tokens for each candidate model, so you know the real budget before writing code. 2. Estimate input size (~4 characters per token, or exactly with tiktoken per the references) and pick a model with selectmodelforcontext() from Context-Aware Model Selection — it applies an 80% safety margin and falls back through gpt-4o-mini (128K) → Claude 3.5 Sonnet (200K) → Gemini 2.0 Flash (1M). 3. Keep long conversations inside budget with trimconversation() per Conversation Trimming: system prompt plus the last N messages, with a trim-marker note injected where history was dropped. 4. For documents that exceed any window, use chunkandprocess() per Chunking for Large Documents — 8,000-char chunks with 500-char overlap, analyzed independently at temperature=0 and then synthesized. 5. Mark large static blocks with cachecontrol: {"type": "ephemeral"} per Prompt Caching for Repeated Context to cut repeated input cost by 90% on Anthropic models. 6. Monitor prompttokens on every response (Enterprise Considerations) to catch context bloat before it becomes a 400 contextlengthexceeded. …

How to use it

Reference it in AdaL, Claude Code, Cursor or any coding agent — nothing to install:

@skills jeremylongshore/openrouter-context-optimization--bf08de

View the source on GitHub

Browse the @skills marketplace