← Back to AI Systems Engineer Mind Map
🚀 AI Systems EngineerID: aie-context-budget-compaction

Context Budgeting & Memory Compaction

上下文预算分配与记忆 Compaction
🎯Core Definition
Context Window Token Budgeting & Progressive Memory Compaction treats the finite LLM context window as scarce RAM managed through strict partition budgeting and automatic memory compaction; the budget allocation matrix partitions total tokens (e.g. 8k/32k) into: Fixed System & Tool Definitions (20%), Dynamic Working Memory (50% for recent detailed tool/thought traces), Output Buffer (20% for generation), and Safety Margin (10%); Progressive Compaction triggers when working memory saturates: 1) Sliding Window Truncation (retaining recent KK turns), 2) Recursive Semantic Summarization (using cheap small LLMs to distill older turns into dense structured state summaries), and 3) Tool Output Pruning (stripping multi-page raw JSON responses down to essential fields while offloading full payloads to vector stores).
💡Use Cases
Multi-hour 50+ turn conversational agents, autonomous codebase refactoring, and continuous research systems.
⚡Key Problems Solved
Naively appending conversation histories causes context window overflow, 'Lost in the Middle' attention degradation, and skyrocketing token fees; dynamic compaction ensures constant-memory, bounded-cost execution indefinitely.
🎯Core Interview Exam Points
Fundamentals · Production Scenarios · Deep Dive
Core Def
Explain the core definition, underlying mechanism/math, and key principles of Context Budgeting & Memory Compaction.
Use Cases
In production engineering, what are the primary use cases for Context Budgeting & Memory Compaction and what core pain points does it solve?
🔥 High-Frequency Deep-Dive Questions
1
Diagram the 4-tier context window token budget partitioning and dynamic reallocation thresholds?
2
Explain how recursive summarization preserves structured task states, active constraints, and resolved sub-goals without data loss?
3
Explain the 'Lost in the Middle' attention bias and why placing critical system constraints at the beginning and end maximizes retrieval accuracy?
4
How should tool execution wrappers safely truncate massive 50k-token CLI logs or HTML payloads before passing them to the LLM?
5
How to structure conversation history sequentially to preserve static prompt prefixes and maximize cloud KV Cache reuse?
Updated 2026-08-14
🎯
Verify Mastery: Practice Exam Questions for "Context Budgeting & Memory Compaction"
Take pitfall multiple-choice questions with derivation options; mistakes are automatically logged.
🚀 Start Card Practice ➔
← Previous CardLoop Hard Termination & Stagnation GuardsNext Card →Tool Validation, Idempotency & Fallbacks

🔗 More AI Systems Engineer Knowledge Cards

引用出处: TalentMe 450+ 工业级 AI 架构考点知识库 (Tech Vault) · 遵循 CC-BY-4.0 知识共享协议
URL: https://talentme.airsota.com/resources/tech/roadmap/mindmap/AIE/card/aie-context-budget-compaction
中文速览与面试要点

上下文预算分配与记忆 Compaction (Context Budgeting & Memory Compaction)

定义: 大模型 Agent 上下文窗口动态预算分配与长任务记忆压缩机制 (Context Window Token Budgeting & Progressive Memory Compaction) 是将宝贵的有限上下文窗口当作“稀缺系统内存”进行严格预算与分页压缩的核心工程技术;核心分层预算模型:设定固定预算池(如 8k/32k 窗口):System Prompt 与核心工具定义(预留 20% 静态预算)、动态工作记忆(预留 50% 用于存放最近 3 轮完整 Tool / Output Trace)、输出缓冲区(预留 20% 生成空间)、安全余量(预留 10% 缓冲);记忆压缩 (Compaction) 策略:当动态工作记忆达到阈值上限时,触发渐进式压缩——1) 滚动滑动窗口截断 (Sliding Window Truncation: 仅保留最近 K 条消息);2) 语义递归摘要 (Recursive Summarization: 调用快速轻量小模型将早期 10 轮历史对话压缩为高密度 Structured State Summary);3) 工具结果裁剪 (Tool Output Pruning: 对返回数万行的 API 响应截取关键字段,原文档存入向量库)。 使用场景: 超过 50 轮的长程交互会话、自主编程 Agent 分析大代码库、长文档持续分析系统。 解决的问题: 朴素直接拼接所有历史记录会导致上下文在第 15 轮迅速爆满 (Context Overflow OOM),引发模型注意力涣散 (Lost in the Middle) 与单次推理费用剧增;预算与压缩机制实现了无限长任务的恒定显存与恒定低延迟运行。

核心要点

  • 详细画出上下文窗口四大分区(System Prompt、Working Memory、Output Buffer、Safety Headroom)的 Token 预算配比与动态流动图?
  • 递归语义摘要 (Recursive Summarization) 算法:如何在压缩历史对话的同时严格保留“已完成子任务状态”、“未解决的缺陷”与“关键实体变量”?
  • 大模型在超长上下文下的“迷失在中间 (Lost in the Middle)”现象:为什么将关键 System 指令与最相关的 RAG 证据放置在上下文首尾能显著提升召回率?
  • 大工具响应裁剪策略:当 Bash 或 Web 搜索工具返回 50,000 字原始 HTML/日志时,如何通过正则表达式与 Head/Tail 采样将其安全截断至 1,000 tokens?
  • Prompt Caching 友好型上下文排布:如何保持 System Prompt 与历史前缀的绝对静态不变以最大化 KV Cache 命中率?