上

上下文工程(Context Engineering)

作者:注册老炮@MedXpert I SynomosAIv5

当用户说『AI越来越笨/答非所问/忘了前面的设定』『上下文太长』『token烧太快』『RAG召回不准』,或要设计/优化 agent 的提示、记忆、检索、工具编排时使用。把『上下文窗口里到底该放什么』当成一门工程:审计相关性·冗余·预算·衰减 四维度,给出裁剪/检索/记忆/门禁四步流程与可运行脚本。触发词:上下文工程、context engineering、上下文窗口、上下文污染、上下文衰减、context rot、token预算、RAG召回、记忆编排、agent上下文、长上下文优化。

下载量
287
点赞
72
价格
免费

技能文档

---
name: context-engineering
display_name: 上下文工程(Context Engineering)
display_name_en: "Context Engineering"
description: "当用户说『AI越来越笨/答非所问/忘了前面的设定』『上下文太长』『token烧太快』『RAG召回不准』,或要设计/优化 agent 的提示、记忆、检索、工具编排时使用。把『上下文窗口里到底该放什么』当成一门工程:审计相关性·冗余·预算·衰减 四维度,给出裁剪/检索/记忆/门禁四步流程与可运行脚本。触发词:上下文工程、context engineering、上下文窗口、上下文污染、上下文衰减、context rot、token预算、RAG召回、记忆编排、agent上下文、长上下文优化。"
version: 1.0.1
agent_created: true
author: 诺声(Logos)@SynomosAI
license: MIT
license_scope: 代码(.py 文件)MIT;本 SKILL.md 与其中理论文本不在 MIT 覆盖范围内(见文末「License & attribution」)
allowed-tools: Read, Glob, Grep, Write, WebFetch, WebSearch
category: AI工程方法
platforms: [windows, macos, linux]
read_when:
  - AI 在长对话/多轮任务里开始答非所问、遗忘早期约束
  - 要搭建或优化一个 agent 的 system prompt / 记忆 / 检索 / 工具返回
  - token 消耗异常高,怀疑上下文里有冗余或重复内容
  - RAG 召回的文档塞进上下文后反而让回答变差
  - 用户抱怨「你上次说的怎么不算数了」
tags: [上下文工程, context engineering, 上下文窗口, token预算, RAG, 记忆编排, agent优化]
slug: context-engineering
title: 上下文工程(Context Engineering)
copyright: SynomosAI
description_zh: "把『上下文窗口里到底该放什么』当成一门工程:审计相关性·冗余·预算·衰减 四维度,给出裁剪/检索/记忆/门禁四步流程与可运行脚本(上下文预算计算器+审计清单生成器)。"
description_en: "Engineer what goes into the context window: relevance audit, memory/retrieval/tool orchestration patterns to fix degraded answers, context overflow and token burn."
classification:
  skillhub: ["security-compliance"]
  iso_25010: ["Security"]
risk_tier: medium
risk_rationale: "门禁批量补丁默认 medium:调用 API/读写文件/本地执行,最小权限,密钥走 Secret;若实际涉对外代执行(push/发布/发送/上架)须人工复评,改 high。"
ai_protocol: true
spec: usk/1.0
interface:
  type: cli
  entry_point: scripts/context_budget.py
  runtime: python3
  call_pattern: args
input_schema:
  - name: window
    type: int
    description: 上下文窗口 token 上限默认128000
    required: false
  - name: block
    type: string
    description: stdin 归属块名
    required: false
output_schema:
  stdout: 各块 token 预算分配建议
  exit_code:
    type: integer
    description: "0=OK/PASS; 1=findings/warnings; 2=usage or argument error"
capabilities:
  - context_budget
  - llm_engineering
  - optimization
permissions:
  network: false
  filesystem: false
  subprocess: false
requirements:
  python_packages: []
  min_python: "3.8"
changelog: "1.0.1: Bilingual rewrite (full EN+ZH, English first); 1.0.0: initial upload (USK v3)"
---

> 凡自治之物 · LGD 治理学 · 创造术家族 | 评测协议:skill-quality-gate(外部公开评测门禁文档)

## English

### When to use
1. The model starts giving off-topic answers or forgetting earlier settings ("context pollution / decay").
2. Designing / optimizing an agent's prompt, memory, RAG retrieval or tool orchestration.
3. Token burn is high; the context window is too long; memory orchestration needs refinement.
4. Trigger words: "context engineering", "long-context optimization", "RAG recall".

### Usage
1. Run `context_budget.py --calculate` to estimate current token budget and remaining window.
2. Set `--budget_limit` (int) and `--model_name` for model-specific advice.
3. `context_budget.py --audit` produces a relevance/redundancy checklist.
4. For doc-style tasks, read the in-file function library for memory-decay and retrieval-recall helpers.

### Flow
1. **Context audit**: scan interaction history; flag low-relevance, stale or duplicate info.
2. **Budget accounting**: compute current token share and remaining-window risk.
3. **Trimming**: apply the four-dimension standard (relevance / redundancy / budget / decay); compress memory.
4. **Retrieval tuning**: adjust RAG recall params and tool order so key data loads first.
5. **Gate check**: gate new incoming info so new tokens don't overwrite old key settings.
6. **Closed loop**: re-inject the optimized context into the model; log adjustments.

### Evidence & safety
- Evidence: TBD. Version: 1.0.1. Security: no independent scan (TBD).

## 中文

### 何时用
1. 当用户反馈模型"答非所问"、"遗忘设定"或遭遇"上下文污染"、"上下文衰减"等质量问题时。
2. 在进行 Agent 设计、优化提示词(Prompt)、记忆管理或 RAG 检索策略的技术调试与评估场景。
3. 监控对话成本,涉及"token 预算烧太快"、"上下文窗口过长"或需要精细化"记忆编排"时的决策节点。
4. 触发关键词包含"context engineering"、"长上下文优化"、"RAG 召回"及具体指标时自动介入。

### 用法
1. 执行核心计算脚本 `context_budget.py`,启用 `--calculate` 模式以输出当前对话的 Token 预算与剩余窗口估算。
2. 配置 `--budget_limit` 参数(整数)以设定最大可接受上下文长度,配合 `--model_name` 指定目标模型优化建议。
3. 调用 `context_budget.py --audit` 生成包含相关性、冗余度评分的详细清单,用于审查历史会话质量。
4. 对于文档型任务,可通过读取 `context_budget.py` 文件内的函数库获取预定义的"内存衰减"与"检索召回"算法接口。

### 流程
1. **上下文审计**:扫描当前交互历史,识别低相关性、过期或重复信息(使用审计清单生成器)。
2. **预算核算**:调用 `context_budget.py` 计算当前输入占用的 Token 比例及剩余窗口风险。
3. **信息裁剪**:依据四维度标准(相关性·冗余·预算·衰减)删除不必要节点,实施记忆压缩。
4. **检索优化**:调整 RAG 召回参数与工具编排顺序,确保关键数据被优先加载。
5. **门禁校验**:对新进入的信息进行"上下文门控"检查,防止新 Token 覆盖旧的关键设定。
6. **闭环反馈**:将优化后的上下文结构重新注入模型,并记录本次调整日志用于下一轮参考。

### 证据与安全声明
- 证据:TBD(暂无评测/徽章链接;产生后在此更新实际链接)
- 版本:1.0.1
- 安全:未做独立安全扫描(TBD),结论待补;补全前不作推荐背书。

使用说明

# context-engineering · 上下文工程

[![LGD Powered](lgd-powered.png)](https://github.com/zhaoxinghua09-cell/lgd-theory)

**EN** — Turn "what goes into the context window" into an auditable, trimmable, quantifiable engineering discipline. Audit relevance · redundancy · budget · decay; ship a 4-step flow (audit → trim → retrieve → gate) plus a runnable `context_budget.py` (token budget calculator + audit checklist generator). Prompting writes one sentence; context engineering designs everything the model sees each turn.

**中文** — 把「每轮喂给模型的信息」当成一门工程:审计相关性·冗余·预算·衰减四维度,给出裁剪/检索/记忆/门禁四步流程,附可运行脚本(上下文预算计算器 + 审计清单生成器)。prompt 是写一句话,context engineering 是设计 AI 每轮看到的全部信息。

## Why it beats "just write a better prompt"

| Pain | This skill |
|---|---|
| AI forgets early constraints in long chats | Key constraints pinned to system top; decay checklist |
| Token bill explodes | Budget template caps each block; redundancy detector |
| RAG hurts answers | Retrieval blocks carry source + relevance score |
| Memory chaos | Memory entries gated (backup → reversible → confirm) |

## What's inside

- `SKILL.md` — 4-dimension audit + 4-step flow + budget template + 铁律
- `scripts/context_budget.py` — zero-dependency CLI: token estimate per block, window %, cross-block redundancy, audit checklist

```bash
python scripts/context_budget.py --window 128000 --system sys.txt --memory mem.txt --retrieval retr.txt
```

## Benchmark vs alternatives

| Tool | It does | Gap this fills |
|---|---|---|
| Prompt engineering guides | How to phrase one prompt | No system view of the whole window |
| LangChain/LLamaIndex memory | Store & retrieve | No governance: poison / dup / decay unchecked |
| Long-context "just use 200k" | Bigger window | Budget & relevance still matter; decay unsolved |

Theoretical root: **LGD Three Laws** (registered · evidenced · gated). Pairs with `memory-governance` and `release-gate`.

---

© 2026  / SynomosAI·China (ORCID 0009-0001-0512-1237) · 代码 MIT · 理论文本保留所有权利

---

## License & attribution
**分层许可(务必并读;代码许可不覆盖文档与理论文本)**

- **代码**(本包 `.py` 文件):**MIT**
- **本 `README.md` / `SKILL.md` 及其中的理论文本、方法论与一切理论表述**:**不在 MIT 覆盖范围内**,保留所有权利(All rights reserved)

```
© 2026 SynomosAI · China. All rights reserved.
理论署名 (attribution) : LGD(Lifecycle Governance Doctrine / 全程治理论)— SynomosAI initiative
名称状态 (name status)  : "SynomosAI" — 未申请实体注册、未申请商标注册
                        (not a registered legal entity; no trademark registered)
生产参考部署 (production reference, self-reported) : 无(未声明外部生产参考部署)
                    ← 非认证、非背书、非监管认可(not a certification or endorsement)
代码许可 (code license) : 本包未附 LICENSE 文件(许可待定)
                    本文本与理论表述不在任何代码许可覆盖范围内
引用格式 (cite as)      : 本资产无 DOI
```
(上列为《LGD 对外表述规范》§3.2 统一块,**整体复制、未删改**;发布/重提前须照最新版本复核。)

如何安装此技能?

访问技能市场,点击「安装」按钮,按提示将技能包放入 AI 编程助手的 skills 目录即可。

浏览技能市场

支持平台:Qoder · QoderWork · Claude · Codex 等 AI 编程助手