openrouter-context-optimization
Skill by jeremylongshore
Optimizes context window usage for OpenRouter models to lower token costs and improve output quality. Aimed at developers who hit context limits, manage long conversations, or build RAG systems.
Install openrouter-context-optimization
Built from the published package, never by AI.
git clone --depth 1 https://github.com/jeremylongshore/tons-of-skills-marketplace /tmp/tons-of-skills-marketplace && mkdir -p ~/.claude/skills && cp -r /tmp/tons-of-skills-marketplace/skills/.curated/openrouter-context-optimization ~/.claude/skills/openrouter-context-optimizationRun in your terminal. Claude Code loads skills from ~/.claude/skills automatically.
Step-by-step: install openrouter-context-optimization in Claude Code
git clone --depth 1 https://github.com/jeremylongshore/tons-of-skills-marketplace /tmp/tons-of-skills-marketplace && mkdir -p ~/.gemini/antigravity-cli/skills && cp -r /tmp/tons-of-skills-marketplace/skills/.curated/openrouter-context-optimization ~/.gemini/antigravity-cli/skills/openrouter-context-optimizationAntigravity CLI loads skills from ~/.gemini/antigravity-cli/skills, or .agents/skills for one project.
Step-by-step: install openrouter-context-optimization in Antigravity CLI
Skills can run scripts on your machine. Some need an API key: check the README.
About
This skill helps reduce context window usage when calling OpenRouter models. It covers looking up context lengths and prompt pricing from the OpenRouter models API, estimating token counts, and picking a model that fits the input within an 80% safety margin. It also covers trimming long conversations, chunking large documents, and using Anthropic prompt caching for repeated context.
The skill provides Python and shell code snippets. Conversation trimming keeps the system prompt and recent messages and inserts a note where older history was dropped. Chunking splits documents into 8,000-character pieces with 500-character overlap, then synthesizes the partial answers. It is aimed at developers building apps or RAG systems on OpenRouter who hit context limits or want lower token costs.
Written from the project's SKILL.md and checked against it.
What it can do
- Query context limitsLists context_length and prompt price per 1M tokens for chosen models from /api/v1/models.
- select_model_for_context()Picks a model that fits the estimated input, with an 80% safety margin and a fallback chain.
- trim_conversation()Keeps the system prompt and recent messages, with a note where older messages were trimmed.
- chunk_and_process()Analyzes a large document in overlapping chunks at temperature 0, then synthesizes the results.
- Prompt cachingMarks large static blocks with cache_control for Anthropic models to reduce repeated input cost.
What you need
- OpenRouter API key exported as OPENROUTER_API_KEY
- Python 3.8+ with the OpenAI SDK and requests
- curl and jq
- Node.js 18+ for the optional TypeScript context-budget calculator
Try asking
- Check the context length and prompt price of Claude 3.5 Sonnet and GPT-4o on OpenRouter.
- Pick a model that fits my 300K character document with the 80% safety margin.
- Trim my long chat history to the system prompt and last few messages.
Good to know
- Token estimates use roughly 4 characters per token unless tiktoken is used.
- Prompt caching savings are stated for Anthropic models only.
- The fallback model chain is fixed in the example code.
Security check
B- Allowed to use Write, Edit without limitsIn
SKILL.md
Checked automatically: skills/.curated/openrouter-context-optimization/SKILL.md. This looks for known risky patterns and is not a full audit. How grades work
Inside the skill
Allowed tools: ReadWriteEditGrepBash(python3:*)Bash(node:*)Bash(curl:*)Bash(jq:*)
Alternatives to openrouter-context-optimization
All alternatives →Aligns a Qwen Code feature with a Codex or Claude Code reference agent. It runs both under the same scenario, captures HTTP and terminal traces, and compares them until behavior matches.
Searches, views, creates, updates, and deletes the user's Google Contacts from an AI assistant. It is for people who want to manage their address book through conversation. A one-time Google connect is required before it returns data.
Guides an AI assistant on error handling choices. It covers when to use Result, Option, ? propagation, unwrap, expect and panic, plus anyhow and thiserror.
Caveman is an agent skill that makes an AI assistant reply in a terse style. It cuts filler and keeps technical facts, code, commands and errors exact. It is built for users who want shorter, cheaper replies.
docs-sync audits or updates English SDK documentation against the requested implementation scope. It is for maintainers who want docs to match main branch features, configuration options, and defaults.
Guides an AI assistant through setting up and running monorepos with pnpm workspaces, Turborepo and Nx. Aimed at developers who need faster builds, shared code and managed dependencies across multiple packages.
Compare with agent-reproduce-align, google_contacts or m06-error-handling
openrouter-context-optimization: common questions
Does this skill count tokens exactly?
It uses a rough estimate of 1 token per 4 characters for English text. The source says exact counts are possible with tiktoken, per the references.
What should I do when I get a context_length_exceeded error?
The source suggests trimming messages or switching to a model with a larger context window. It also suggests reducing max_tokens if that value alone is too large.
Why does the skill query /api/v1/models at startup?
The source says context limits change over time, so they should be cached at startup rather than hardcoded.
How do I install openrouter-context-optimization?
Run the command above to copy the skill into ~/.claude/skills/. Claude Code picks it up automatically.
Is openrouter-context-optimization free?
The code is published under the MIT licence. The service it connects to may have its own pricing.
Is openrouter-context-optimization safe to use?
It scores 76 of 100. Grades: maintenance A, licence A, quality A. Security check: grade B (allowed to use write, edit without limits). Review the code before you install. How grades work.
What are alternatives to openrouter-context-optimization?
Similar skills: agent-reproduce-align, google_contacts, m06-error-handling, caveman.
