AGENT SKILL

openrouter-context-optimization

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'.

License 许可
MIT
最近更新
2026-05-26
Stars
★ 0
Forks
0

01 前言 Front Matter

name
openrouter-context-optimization
author
Jeremy Longshore <jeremy@intentsolutions.io>
license
MIT
version
1.0.0
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
compatible-with
claude-code, codex, openclaw

02 资源文件 9

03 SKILL.md 正文

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

04 同仓库更多 Skills

SamuelChien
SamuelChien/mega-skills-collection

zustand-store-ts

Create Zustand stores with TypeScript, subscribeWithSelector middleware, and proper state/action separation. Use when building React state management, creating global stores, or implementing reactive state patterns with Zustand.

★ 0
SamuelChien
SamuelChien/mega-skills-collection

zustand-store-creator

Zustand Store Creator - Auto-activating skill for Frontend Development. Triggers on: zustand store creator, zustand store creator Part of the Frontend Development skill category.

★ 0
SamuelChien
SamuelChien/mega-skills-collection

zero-trust-config-helper

Zero Trust Config Helper - Auto-activating skill for Security Advanced. Triggers on: zero trust config helper, zero trust config helper Part of the Security Advanced skill category.

★ 0
SamuelChien
SamuelChien/mega-skills-collection

zapier-integration-helper

Zapier Integration Helper - Auto-activating skill for Business Automation. Triggers on: zapier integration helper, zapier integration helper Part of the Business Automation skill category.

★ 0
SamuelChien
SamuelChien/mega-skills-collection

zai-cli

Execute z.AI CLI providing vision, search, reader, and GitHub exploration via CLI and MCP. Use when user needs image/video analysis, OCR, UI-to-code conversion, error diagnosis, real-time web search, web page to markdown extraction, or GitHub code exploration. Trigger with phrases like "analyze this image", "search the web for", "read this page", "explore this repo", or "use zai". Requires Z_AI_API_KEY.

★ 0
SamuelChien
SamuelChien/mega-skills-collection

yt-research

Research competitor YouTube channels, niches, and trending topics for your content strategy. Use this skill whenever the user says "research channels", "analyze competitors", "find trending topics", "niche analysis", "competitive research", "what are other creators doing", "scrape YouTube channels", or wants to understand the competitive landscape for a specific tool or topic area. Use when working with yt research. Trigger with 'yt', 'research'.

★ 0