← Back to Skills Marketplace
nicochow

Context Switch Token Optimizer

by nicochow · GitHub ↗ · v1.0.1 · MIT-0
cross-platform ✓ Security Clean
236
Downloads
0
Stars
0
Active Installs
2
Versions
Install in OpenClaw
/install context-switch-token-optimizer
Description
智能对话上下文切换与 Token 优化。分析对话主题连续性以管理上下文、在话题间切换并节省 Token。 适用于多任务对话、项目工作流、学习研究等需管理上下文与 Token 使用的场景。
README (SKILL.md)

Context Switch Token Optimizer

技能描述

通过关键词相似度、弱关联子串与时间衰减判断「连续 / 渐变漂移压缩 / 硬切换」,并可监控 Token、触发压缩与清理。实现见 context_manager.pytoken_optimizer.py;详细算法见 docs/TOPIC_AND_CONTINUITY.md

上下文动作(与代码一致)

动作 条件 行为
load_memory 无当前主题且无历史 建主题、可选加载 memory/topic 下记忆
continuous 与上一轮相似度 ≥ similarity_threshold 追加历史,不压缩
drift_compress 相似度 ∈ [continuity_threshold, similarity_threshold) 对话仍连续;压缩与当前弱相关的历史轮(is_compressed,保留短摘要)
switch_context 相似度 \x3C continuity_threshold 硬切换,换记忆、增 switch_count

相似度计算:calculate_topic_similarity()(关键词 Jaccard 式重叠 × 时间衰减 + 2 字子串/首字弱关联 + continuity_bonus_for_partial,弱关联封顶 drift_similarity_cap)。

持久化状态格式(memory/context_switch_state.json

ContextState / TopicSummary 一致,主题历史项包含:

  • topic, keywords, timestamp, tokens_used, content_snippet, is_compressed
  • 顶层另有:current_topic, memory_context, last_switch_time, switch_count, total_tokens

配置文件(config.json

所有键名与 load_skill_config() 默认值一致;用户 JSON 按顶层块与默认合并(同一块内键覆盖,未写的子键保留默认)。

context_switch

键名 类型 默认 说明
similarity_threshold number 0.7 ≥ 此值判为 continuous
continuity_threshold number 0.35 \x3C 此值判为硬切换 switch_context
compress_relevance_threshold number 0.3 drift_compress 时,历史轮与当前相似度低于此则压缩该轮
continuity_bonus_for_partial number 0.3 存在弱关联时的相似度加成
drift_similarity_cap number 0.65 弱关联时相似度上限,便于进入 drift_compress
time_decay_factor number 0.95 按小时衰减的底数
max_topic_history int 10 主题历史条数上限
memory_relevance_threshold number 0.5 记忆文件匹配阈值
log_level string (不设) 可由环境变量 CONTEXT_SWITCH_LOG_LEVEL 写入

token_optimization

键名 类型 默认 说明
enabled bool true 为 false 时不自动触发优化(对应环境变量 TOKEN_OPTIMIZER_ENABLED
token_limit int 80000 Token 预算
compression_threshold int 56000 压缩参考阈值
context_cleanup_threshold number 0.8 上下文清理参考
memory_load_limit int 2000 记忆加载参考
optimization_interval int 300 优化间隔(秒,预留)
max_history_size int 100 Token 使用记录条数上限

memory_search

键名 类型 默认 说明
max_search_results int 5 最多返回记忆条数
keyword_limit int 5 每轮提取关键词数
search_depth int 2 可由环境变量 MEMORY_SEARCH_DEPTH 覆盖(1–3)
file_types string[] ["*.md"] 记忆目录 glob

optimization_settings

键名 说明
auto_optimization 是否自动优化(策略预留)
health_score_threshold 健康分阈值
suggestions_check_interval 建议检查间隔(秒)

环境变量(与 apply_environment_overrides() 一致)

环境变量 映射配置键 取值与行为
CONTEXT_HISTORY_SIZE context_switch.max_topic_history 正整数,限制在 1–100
MEMORY_SEARCH_DEPTH memory_search.search_depth 正整数,限制在 1–3
TOKEN_OPTIMIZER_ENABLED token_optimization.enabled true/1/yes/on 为启用;false/0/no/off 为禁用(禁用后使用率 >70% 也不触发 trigger_optimization
CONTEXT_SWITCH_LOG_LEVEL context_switch.log_level DEBUG / INFO / WARNING / ERROR / CRITICAL;设置根 logger 级别

未设置上述环境变量时,行为完全由 config.json 与代码默认决定。

CLI 与入口

  • 主入口:python3 context-switch-token-optimizer.py [--config path] --process|--status|--reset|--test
  • 子模块:context_manager.pytoken_optimizer.py(参数见 --help

维护: Hermosa
版本: v1.1
最后更新: 2026-03-17

Usage Guidance
This skill appears internally consistent and implements the described context-switching and token-optimization behavior without requesting secrets or performing network calls. Before installing/running: 1) Inspect or run it in an isolated directory (it will create/modify memory/context_switch_state.json and other files). 2) If you don't want automatic optimization, set TOKEN_OPTIMIZER_ENABLED=false to disable auto-triggering. 3) Enable debugging (CONTEXT_SWITCH_LOG_LEVEL=DEBUG) if you want to observe similarity calculations and compression decisions. 4) Review the memory/ files after test runs and confirm behavior on representative conversations. If you need the skill to never write to disk, do not run it or run with a sandboxed working directory.
Capability Analysis
Type: OpenClaw Skill Name: context-switch-token-optimizer Version: 1.0.1 The skill bundle implements a legitimate utility for managing LLM conversation context and optimizing token usage through topic similarity analysis and history compression. The code operates locally on state files within a designated 'memory/' directory, lacks network communication capabilities, and contains no evidence of malicious execution (e.g., eval/exec), data exfiltration, or prompt injection attacks. All logic in 'context_manager.py' and 'token_optimizer.py' is consistent with the stated purpose of maintaining conversation continuity while reducing token overhead.
Capability Assessment
Purpose & Capability
Name/description (context switching and token optimization) match the code and SKILL.md. The skill only requests configuration and logging environment overrides relevant to topic management and token optimization; no unrelated credentials, binaries, or external services are required.
Instruction Scope
SKILL.md and CLI instruct running the included Python scripts which read/write a local state file (memory/context_switch_state.json), load config.json, and may scan local memory files (configured via file_types). The runtime instructions do not ask the agent to read arbitrary OS secrets or network endpoints. Note: the skill will create and modify files in the working directory (memory/), so consider where the agent runs.
Install Mechanism
There is no install spec (instruction-only from registry perspective), which is low-risk. However the package includes multiple Python source files (no external downloads or package installers). Because code is present, running the skill will write state to disk; no remote fetch/install behavior was observed.
Credentials
Environment variables the skill reads (CONTEXT_HISTORY_SIZE, MEMORY_SEARCH_DEPTH, TOKEN_OPTIMIZER_ENABLED, CONTEXT_SWITCH_LOG_LEVEL) are directly tied to its configuration and behavior. No secrets/keys/tokens/passwords are required.
Persistence & Privilege
The skill persists its own state under memory/context_switch_state.json and updates it during operations (save_context_state). always:false and normal autonomous invocation are set. This is expected for a context manager, but be aware it will create/modify files in the agent's working directory.
How to Use
  1. Make sure OpenClaw is installed (local or Docker)
  2. Run the install command in chat: /install context-switch-token-optimizer
  3. After installation, invoke the skill by name or use /context-switch-token-optimizer
  4. Provide required inputs per the skill's parameter spec and get structured output
Version History
v1.0.1
- Added initial implementation files: context management and token optimization modules, plus test and debug scripts. - Provided detailed algorithm and configuration documentation. - Introduced a richer state persistence format including compression status and token usage tracking. - Supported advanced topic similarity calculation (Jaccard overlap, substring/partial bonus, time decay). - Environment variable overrides and merged user config supported. - Initial CLI support for processing, status, reset, and testing operations.
v1.0.0
Initial release of context-switch-token-optimizer: - Introduces intelligent context switching, summarizing dialog themes and managing conversation continuity. - Implements a three-level topic similarity mechanism for continuous, drift-compress, or hard context switching. - Features topic extraction, keyword analysis, and related memory search for efficient context management. - Includes token optimization strategies with progressive memory loading and context cleanup. - Provides monitoring and reporting of token usage and topic switches. - Suitable for workflows requiring efficient context and token management across conversations.
Metadata
Slug context-switch-token-optimizer
Version 1.0.1
License MIT-0
All-time Installs 0
Active Installs 0
Total Versions 2
Frequently Asked Questions

What is Context Switch Token Optimizer?

智能对话上下文切换与 Token 优化。分析对话主题连续性以管理上下文、在话题间切换并节省 Token。 适用于多任务对话、项目工作流、学习研究等需管理上下文与 Token 使用的场景。 It is an AI Agent Skill for Claude Code / OpenClaw, with 236 downloads so far.

How do I install Context Switch Token Optimizer?

Run "/install context-switch-token-optimizer" in the OpenClaw or Claude Code chat to install it in one step — no extra setup required.

Is Context Switch Token Optimizer free?

Yes, Context Switch Token Optimizer is completely free, licensed under MIT-0. You can download, install and use it at no cost.

Which platforms does Context Switch Token Optimizer support?

Context Switch Token Optimizer is cross-platform and runs anywhere OpenClaw / Claude Code is available (cross-platform).

Who created Context Switch Token Optimizer?

It is built and maintained by nicochow (@nicochow); the current version is v1.0.1.

💬 Comments