Skip to content
Back to skills

Openrouter Context Optimization

ASecurity

Optimize token usage and context window management. Use when reducing costs or fitting more information into model context. Trigger with phrases like 'openrouter context', 'token optimization', 'context window', 'reduce tokens'.

  • 4 stars
  • 0 votes
  • 0 copies
  • 2 views
  • Added September 10, 2026
ai-agentspythonapidocumentation

Works with

  • api

Security analysis

A100/100

Pro scans all 10 files and shows the line behind each finding

Scanned September 10, 2026

npx -y skills add micsapp/micstec-skills --skill openrouter-context-optimization --agent claude-code

Installs into .claude/skills of the current project.

Are you the author of Openrouter Context Optimization?

Add the live security badge to your README. It updates with every re-scan.

Security grade badge for Openrouter Context Optimization
[![Security: A — Skills Directory](https://www.skillsdirectory.com/api/skills/micsapp-openrouter-context-optimization/badge)](https://www.skillsdirectory.com/skills/micsapp-openrouter-context-optimization)

More formats (shields.io, HTML) on the badges page. Keep it an A: scan every change in CI with Pro.

Download with Pro
SKILL.md
---
name: openrouter-context-optimization
description: |
  Optimize token usage and context window management. Use when reducing costs or fitting more information into model context. Trigger with phrases like 'openrouter context', 'token optimization', 'context window', 'reduce tokens'.
allowed-tools: Read, Write, Edit, Grep
version: 1.0.0
license: MIT
author: Jeremy Longshore <jeremy@intentsolutions.io>
compatible-with: claude-code, codex, openclaw
---
# Openrouter Context Optimization

## Overview

This skill covers techniques for managing token budgets, optimizing prompt length, and making the most of each model's context window.

## Prerequisites

- OpenRouter integration
- Tokenizer library (tiktoken for OpenAI models, or approximate counting)

## Instructions

1. **Count tokens before sending**: Use `tiktoken` (Python) or `js-tiktoken` (JS) to estimate token counts before making API calls; abort if the prompt exceeds the model's context limit
2. **Implement conversation pruning**: For multi-turn chats, drop or summarize older messages when total tokens approach the context limit, keeping system prompts and recent messages
3. **Optimize system prompts**: Move static instructions to the system message and keep them concise; every token in the system prompt is repeated on every request
4. **Use response length controls**: Set `max_tokens` to the minimum needed for your use case; shorter completions cost less and return faster
5. **Select models by context need**: Route short-context tasks to cheaper models and reserve large-context models (128K+) for tasks that genuinely need them

## Output

- Token budget calculator that warns before exceeding context limits
- Conversation pruning middleware that keeps chats within context bounds
- Cost reduction from optimized prompt engineering (typically 20-40% savings)

## Error Handling

| Error | Cause | Fix |
|-------|-------|-----|
| 400 `context_length_exceeded` | Prompt + max_tokens exceeds model limit | Prune messages or switch to a model with a larger context window |
| Truncated responses | `max_tokens` set too low | Increase `max_tokens` or split the task into smaller requests |
| Token count mismatch | Wrong tokenizer for the model | Use the correct tokenizer per model family; fall back to rough estimation (1 token ~ 4 chars) |

See `${CLAUDE_SKILL_DIR}/references/errors.md` for full error reference.

## Examples

See `${CLAUDE_SKILL_DIR}/references/examples.md` for runnable code samples.

## Resources

- [OpenRouter Documentation](https://openrouter.ai/docs)
- [OpenRouter Models](https://openrouter.ai/models)
- [OpenRouter API Reference](https://openrouter.ai/docs/api-reference)
- [OpenRouter Status](https://status.openrouter.ai)

Files in this skill

  • SKILL.md2.7 KB
  • references/context-recycling.md1.9 KB
  • references/context-truncation.md2.8 KB
  • references/efficient-message-patterns.md1.9 KB
  • references/errors.md323 B
  • references/examples.md3.7 KB
  • references/model-specific-optimization.md1 KB
  • references/prompt-optimization.md2 KB
  • references/response-length-control.md987 B
  • references/token-estimation.md1.5 KB

Attribution

Is this your skill, or is something wrong with this listing? Request removal or report an issue. Author removals are honored within 72 hours.

Comments

Loading comments…

Newsletter

Get the weekly list of safe new skills, and the ones we pulled.