Getting it into your agent
One page per mod, every tool's command on it. A separate URL per tool would split the same page into five that compete with each other.
npx skills add echoVic/blade-code --skill context-compaction-token-budget-and-memorygit clone --depth 1 https://github.com/echoVic/blade-codeWrote this? Show the measurements
A badge with what this costs and how it scanned, read live from this page, so it follows the numbers instead of freezing them. Markdown for a README, HTML for a documentation site or a project page.
[](https://agentmods.dev/skills/echovic/blade-code/context-compaction-token-budget-and-memory)<a href="https://agentmods.dev/skills/echovic/blade-code/context-compaction-token-budget-and-memory"><img src="https://agentmods.dev/badge/skills/echovic/blade-code/context-compaction-token-budget-and-memory.svg" alt="Measured on agentmods" height="20"></a>What it costs to keep this loaded
Counted locally with the o200k_base tokenizer, which is exact for GPT models; Claude uses its own tokenizer and its counts differ. Treat this as one consistent yardstick across the catalogue rather than a bill. Prices are per million input tokens.
| Model | Per session | Once invoked |
|---|---|---|
| Fable 5.1 | $0.00140 | $0.01891 |
| Opus 5 | $0.00070 | $0.00945 |
| Sonnet 5 | $0.00028 | $0.00378 |
| Haiku 4.5 | $0.00014 | $0.00189 |
Grade A, and why
knowledge-session-state-and-context-context-compaction-token-budget-and-memory scanned grade A with 0 findings against 26 rules in 11 categories — prompt injection, anti-refusal, data exfiltration, privilege escalation, supply chain, agent snooping, system-prompt leakage, SSRF and excessive agency — measured 3d ago.
A static scan of the body, not an audit. Every finding is printed with the line that produced it so you can judge whether it matters here. A mod is markdown that instructs an agent; that is exactly why what it instructs is worth reading.
Nothing flagged
None of the 26 patterns this scan looks for appear in this file: no shell pipes, no recursive deletes, no credential paths, no hidden text, no instruction-override or anti-refusal phrasing, no agent-config snooping. That is not a guarantee, it is the absence of the things that are checkable.
This is a copy
88% identical to knowledge-session-state-and-context — 73 lines differ, which has more behind it and is treated as the original. This page carries a canonical link to it rather than competing with it.
How it starts
The opening of the file, as written. The whole thing — 58 lines — stays where its author put it; the contents beside it link to each section on GitHub.
Module Structure
该节点区分两种不同持久边界:compaction 只替换当前 Session 的模型可见上下文,Auto Memory 则把显式或启发式知识写入项目级文件供后续 Session 使用。二者只在成功压缩后的异步 consolidation 处相交。
Directory Layout
packages/cli/src/context/:Token 投影、handoff、snip、LLM/fallback 压缩与 replacement checkpointpackages/cli/src/agent/loop/executeLoopGenerator.ts:每次 Provider 边界前协调上下文预算packages/cli/src/services/SessionService.ts:分别加载完整 UI 历史与 replacement 后模型上下文packages/cli/src/memory/:项目记忆索引、主题文件和压缩后启发式提取packages/cli/src/tools/builtin/memory/:MemoryRead 与 MemoryWrite 工具适配packages/cli/src/prompts/builder.ts:把有界MEMORY.md索引注入新模型上下文
Key Entry Points
checkAndCompactInLoop()inpackages/cli/src/agent/loop/executeLoopGenerator.ts:每次 Provider 请求前的分层压缩协调CompactionService.compact()inpackages/cli/src/context/CompactionService.ts:LLM 摘要与 deterministic fallbackPersistentStore.saveCompaction()inpackages/cli/src/context/storage/PersistentStore.ts:原子保存 replacement checkpointAutoMemoryManager.loadIndex()inpackages/cli/src/memory/AutoMemoryManager.ts:启动时读取有界项目记忆consolidateAfterCompaction()inpackages/cli/src/memory/MemoryConsolidation.ts:从被压缩消息启发式提取项目记忆
Gotchas
ContextManager已是 PersistentStore 薄门面,不拥有当前 Session 内存、filter 或 compaction;新增 runtime 上下文逻辑应接入 Agent loop/CompactionService,而不是恢复旧式 manager 状态 (packages/cli/src/context/ContextManager.ts,git:a074815b)ContextAssembler目前只有测试消费者,ContextCompressor与ContextFilter没有生产调用点;修改这些类不会改变实际 Agent loop 的模型上下文 (packages/cli/src/context/ContextAssembler.ts,packages/cli/src/context/processors/ContextCompressor.ts,packages/cli/src/context/processors/ContextFilter.ts)checkAndCompactInLoop()的顺序是 cache-expiry micro compact、普通 snip、80% LLM compaction;snip 结果延迟应用,确保 LLM compaction 被取消或 checkpoint 失败时不会留下半更新内存 (packages/cli/src/agent/loop/executeLoopGenerator.ts)- replacement 必须先通过
saveCompaction()fsync 到 JSONL,再替换内存并重试 Provider;checkpoint commit 失败时本轮以checkpointphase 失败,不能仅在内存继续 (packages/cli/src/agent/loop/conversationPersistence.ts,packages/cli/src/agent/loop/executeLoopGenerator.ts) - 完整 transcript 始终保留,
loadSessionModelContext()只使用最新有效 replacement checkpoint 加其后事件;UI、搜索和导出仍看 materialized 完整历史 (packages/cli/src/services/SessionService.ts,packages/cli/src/context/compactionCheckpoint.ts) computeAdaptiveBudget()当前只有测试调用,生产 loop 仍使用默认MessageBudgetTracker与默认单结果预算;只修改 adaptive 函数不会改变实际 tool result 截断 (packages/cli/src/context/ToolResultBudget.ts,packages/cli/src/agent/loop/executeLoopGenerator.ts)- 压缩后 memory consolidation 是 fire-and-forget 且吞掉失败,并使用进程
getCwd()建立项目记忆;多 workspace/worktree 调用不能把它当作 checkpoint 成功条件或精确 active-workspace 写入 (packages/cli/src/context/CompactionService.ts,packages/cli/src/memory/MemoryConsolidation.ts) - MemoryWrite 的敏感信息检查是少量正则启发式,不是通用 secret scanner;通过检查不代表内容适合持久化 (
packages/cli/src/tools/builtin/memory/MemoryWriteTool.ts)
What ships with it
2 files beside SKILL.md in the same directory: the scripts, references and assets a skill reads on demand. Not counted in the per-session cost; read them before you install if any of them is executable.
What this file has done since we first saw it
Hashed on every crawl. A supply-chain change to an agent config is a question of when, not whether, so the history is kept rather than the latest state alone.
- 3d ago First seen · 58 lines · 140 tokens per session scan A b01d4c018429
knowledge-session-state-and-context-context-compaction-token-budget-and-memory is a skill published in the GitHub repository echoVic/blade-code (178 stars, last pushed yesterday), licensed MIT. It adds 140 tokens to every session and 1,891 once invoked, about $0.0007 per session on Opus 5. A static security scan graded it A with 0 findings. It is 88% identical to knowledge-session-state-and-context, differing in 73 lines, and is treated as a copy.
Other skills, from other repositories
algorithmic-art
Creating algorithmic art using p5.js with seeded randomness and interactive parameter exploration. Use this when users request creating art using code, generative art, algorithmic art, flow fields, or particle systems. Create original algorithmic art rather than copying existing artists' work to avoid copyright…
mcp-builder
Guide for creating high-quality MCP (Model Context Protocol) servers that enable LLMs to interact with external agents through well-designed tools. Use when building MCP servers to integrate external APIs or agents, whether in Python (FastMCP) or Node/TypeScript (MCP SDK).
slack-gif-creator
Toolkit for creating animated GIFs optimized for Slack, with validators for size constraints and composable animation primitives. This skill applies when users request animated GIFs or emoji animations for Slack from descriptions like "make me a GIF for Slack of X doing Y".
canvas-design
Create beautiful visual art in .png and .pdf documents using design philosophy. You should use this skill when the user asks to create a poster, piece of art, design, or other static piece. Create original visual designs, never copying existing artists' work to avoid copyright violations.
self-improvement
A process for recording useful lessons from unexpected failures, user corrections, missing abilities, and reusable project patterns.
travel-planner
Creates comprehensive travel itineraries with destination analysis, budget planning, transportation, accommodation, and local insights. Use when planning trips, organizing travel schedules, or seeking destination recommendations.