Sponsored byOutsenseAI that finds your next clients
SponsorGROUOutbound, LinkedIn content and leads for B2B teamsSponsorOutsenseAI that finds your next clientsAd+Your tool hereSeen by people installing AI tools@Weekly picksThe best new tools, one email a week

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.

  • Score 76
  • 3k stars
  • MIT
  • Updated
Works withClaude CodeClaude apps Antigravity CLI

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

Run in your terminal. Claude Code loads skills from ~/.claude/skills automatically.

Step-by-step: install openrouter-context-optimization in Claude Code

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 →

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.

openrouter-context-optimization Install

Add to stack

Loading.