上下文工程
一句话总结在有限的上下文窗口里,把”对的信息”以”对的形式”放进去:系统 prompt、工具说明、历史消息的裁剪/压缩、记忆与检索结果的注入。Agent 质量很大程度上取决于上下文工程,而非模型本身。
它解决什么问题
上下文窗口有限且昂贵;放太多会稀释注意力、增加成本,放太少会丢失关键信息。上下文工程决定 agent 在每一步”看到什么”。
设计维度 / 实现谱系
- Prompt 组织:硬编码字符串 ↔ 模板系统 ↔ 可组合的 prompt 对象(Semantic Kernel 的 functions)
- 历史管理:全量保留 ↔ 滑动窗口 ↔ 摘要压缩 ↔ 重要性筛选
- 记忆注入:何时检索 记忆、注入多少、放在哪个位置
- 工具呈现:把工具 schema 如何描述给模型(影响调用准确率)
- 透明度:框架是否让你看到/修改最终 prompt(高抽象框架常隐藏,见 design-tradeoffs)
关键要点
- “Context engineering > prompt engineering”:重点是动态组装每步上下文。
- 能否”掀开盖子”改最终 prompt,是框架在真实项目可用性的试金石。
- 与 memory 和 tool-use 深度耦合。
关联
各框架实现对比
下表汇总 47 个实现了「上下文工程」的框架(源码级阅读结论)。网站上以可展开 + 源码节选呈现。
| 框架 | 实现方式 |
|---|---|
| Aeon | prompt 注入分层:CLAUDE.md(Claude Code 自动加载的 agent 身份/规则/安全约束)+ 当前 SKILL.md + 链上下文文件 + var。可选 soul/(SOUL.md/STYLE.md/examples)注入人格风格。明确防注入:外部内容一律当不可信数据 |
| AG2 | system message 可静态或 UpdateSystemMessage 动态生成;group 模块的 ContextVariables 在 Agent 间共享可变状态并驱动条件转移/system 模板(ContextExpression/ContextStr);transform_messages capability 做消息裁剪/压缩/限长 |
| Agency Swarm | 运行前把 shared_instructions(agency 级)+ agent 自身 instructions + 本次 additional_instructions 拼成最终 system prompt(execution_helpers.py:307 起,运行后还原 :435);instructions 支持 str/文件路径;MessageFilter/MessageFormatter 决定哪些消息进 history、打 agent 元数据 |
| Agent-LLM (AGiXT) | 注入向量记忆(injected_memories/context_results)+ 近期对话(conversation_results)+ 可选 web 搜索(Websearch.py)+ 浏览链接;两段式命令选择先用大窗口模型从全部命令里筛出相关子集再注入,控制工具上下文膨胀;对话历史可压缩 |
| AgentDock | createSystemPrompt(agentConfig, dynamicState) 由 personality+动态 orchestration 状态(activeStep/recentlyUsedTools)拼 system prompt,并注入当前日期/时区;applyHistoryPolicy 按 none/lastN/all 裁剪历史;模板级 tokenOptimization(compressToolOutputs/maxToolOutputTokens) |
| AgentField | 自动上下文传播(Workflow/Session/Actor/Execution ID 经 header 转发,X-Workflow-ID/X-Execution-ID);app.ai 的 system/user/schema 拼装;harness 支持 system_prompt 覆盖与 env 注入。无内建 token 压缩/auto-compact(依赖外部 harness 自管) |
| Agentic Context Engine (ACE) | 本框架重点。Skillbook 即被工程化的上下文:策略以 XML |
| AgentScope | 每轮 reasoning 前 compress_context():count_tokens 超 trigger_ratio(0.8)context_size 即触发,按 reserve_ratio(0.1) 拆分待压/保留,用结构化 SummarySchema(task_overview/current_state/…) 让模型生成摘要回填;工具结果按 tool_result_limit(3000) 截断;system_prompt 可经 middleware on_system_prompt 管道改写 |
| Agentset | 检索后用模板把 chunks 包成 <source_n>…</source_n> 注入(utils.ts:13);强约束 system prompt 要求”仅基于来源作答 + 强制 [n] 引用”(prompts.ts:3);多轮先 condense 历史为单 query 控上下文 |
| AgentVerse | prompt 用 string.Template 占位符填充:${agent_name}/${env_description}/${role_description}/${chat_history}(tool agent 另加 ${tools}/${tool_names}/${tool_observation});prepend_prompt_template+append_prompt_template 分段拼接(agents/base.py:62);环境描述由 Describer 规则按 agent 动态生成注入 |
| Ailoy | 本地模型用 minijinja 渲染 chat template(src/model/local/chat_template.rs);RAG 文档经 DocumentPolyfill(Qwen3 模板)注入 system/query 消息,适配”原生不支持知识输入”的模型(src/model/polyfill.rs:13,42);推理参数 LangModelInferConfig(temperature/top_p/max_tokens/grammar/think_effort,src/model/language_model.rs:101) |
| Astron Agent | 模板拼装式:system prompt 由 {now}/{instruct}/{knowledge}/{tools}/{tool_names}/{r1_more} 占位替换(对 xdeepseekr1 模型走专用模板分支),user prompt 拼 {chat_history}+{question}+{scratchpad};knowledge 检索结果(含图片/表格引用替换)作为背景注入 |
| AutoGen | ChatCompletionContext 抽象管理喂给 LLM 的消息窗口;多策略实现:UnboundedChatCompletionContext、BufferedChatCompletionContext(取最近 N)、HeadAndTailChatCompletionContext、TokenLimitedChatCompletionContext;system_message + memory 注入 + _get_compatible_context(按 model_info 去图像) |
| Botpress | 双模 prompt 系统(chat-mode/ vs worker-mode/,Markdown 模板编译成 TS);运行时注入工具签名/schema/历史;truncateWrappedContent 按 token 上限智能截断(带 flex/minTokens 标记),保证不超模型窗口 |
| ConnectOnion | system_prompt 支持 str/文件/Path;auto_compact 插件在 context≥90% 时用 gemini-flash 摘要旧消息(保留 system+摘要+最近5条);system_reminder/ulw 等插件注入上下文 |
| Cordum | 指针化:Gateway 把输入 context JSON 写 Redis 设 context_ptr,总线只带指针;Safety Kernel 评估时按需解引用做内容级扫描(如 PII/payload 字段提取) |
| Cortex Memory | 核心卖点。三层渐进披露按查询意图动态加权(EntityLookup 偏 L2 0.7、Relational 偏 L1 0.5 等,search/weight_model.rs:45 weights_for_intent),让接入方只加载所需粒度,省 token |
| CrewAI | Task 描述模板插值({topic});task context 由前序 TaskOutput 串联(_get_context);执行前注入 knowledge(RAG) 检索与 memory 召回;超窗时 respect_context_window 处理 |
| Dust | system prompt 由 constructPromptMultiActions 组装并注入 memory/toolsets/user/workspace 上下文;超长时 compactionWorkflow 用专门 prompt 把历史摘要为 compaction 消息,保留最近若干轮交互 |
| Haystack | 框架核心卖点:上下文如何被检索/排序/过滤/拼装全部显式可控——PromptBuilder 用 Jinja2 模板拼 prompt,Ranker 重排,Joiner 合并多路文档,Router 条件路由;Agent 的 system/user prompt 支持 Jinja2 模板(ChatPromptBuilder),required_variables 校验 |
| hcom | bootstrap primer 注入身份+CLI 契约(bootstrap.rs:34);config 的 hints(追加到每条收到的消息)与 notes(启动一次性追加)(README:309);hcom bundle prepare 把事件/文件/转录片段打包成结构化 handoff 上下文(commands/bundle.rs, send.rs:23) |
| Hermes Agent | 可插拔 ContextEngine 抽象基类(config context.engine 选,默认 compressor);ContextCompressor 用辅助小模型摘要中段、保护首尾(token 预算)、结构化模板(Resolved/Pending/Remaining Work)、迭代式更新;记忆/skill 注入用 |
| Hive | ”洋葱模型”分层 prompt 组合:identity_prompt(Layer1 静态身份) + 节点 system_prompt 分层叠加(continuous/isolated 两种 conversation_mode,edge.py:366);Goal.to_prompt_context() 注入每次 LLM 调用;超 token 自动 compaction(LLM 摘要 + emergency summary) |
| Lagent | Aggregator 负责把 memory 拼成 OpenAI message:DefaultAggregator 合并 system+历史,InternLMToolAggregator 处理工具步骤;output_format(parser) 用 format_instruction() 注入格式要求、parse_response() 抽取 thought/action 存入 AgentMessage.formatted |
| LangChain | system_prompt 注入在 _execute_model_sync(factory.py:1300);SummarizationMiddleware 近上限时用 LLM 摘要旧消息并 RemoveMessage 重写历史;ContextEditingMiddleware(ClearToolUsesEdit) 清理工具输出;dynamic_prompt 钩子动态改 prompt |
| Llama Agentic System (llama-stack-apps) | instructions(system prompt) + sampling_params(top_p/greedy);document/attachment 注入(agent.create_turn(documents=…)),RAG query_config(max_chunks/max_tokens_in_context) 控制召回上下文长度;首轮注入 system message 的手动管理 |
| LlamaIndex | system_prompt 前置;state_prompt 把运行时 state 注入最后一条 user 消息(DEFAULT_STATE_PROMPT);ReAct 用 ReActChatFormatter 把工具描述+reasoning 步骤渲进 system header 模板;RAG 检索结果作为上下文喂入;memory block 模板化注入 |
| llm-agents | 极简:每轮把全部历史 ‘\n’.join(previous_responses) 原样拼回 prompt(无摘要/压缩/裁剪);唯一”工程”手段是 stop_pattern=[‘\nObservation:’, …] 防止 LLM 续写假 Observation |
| LoongFlow | ① GradeMemory 自动压缩控上下文长度(LLMCompressor 摘要 MTM+STM,grade/memory.py:108);② Planner 把”父代解+评测+总结”作为经验注入下一轮 prompt;③ ReAct 每步把 system_prompt+历史+工具声明拼成 CompletionRequest(default_reasoner.py:37);场景 Agent 用 prompt 模板(如 claude_code/general_prompt.py) |
| Maestro | 纯 prompt 拼接:历史结果直接字符串 join 进 prompt/system;无压缩/摘要/裁剪。仅有的”上下文管理”是输出≥4000 token 时递归续写防截断 |
| Mastra | processor 管线:input/output/error processors(及可作 processor 的 workflow)在 LLM 调用前后改写消息/系统提示/工具集,内置 token-limiter、message-selection、system-prompt-scrubber、moderation、PII、prompt-injection、structured-output 等;system-reminders 注入;instructions/model/tools 均支持 DynamicArgument(按 requestContext 动态解析) |
| MetaGPT | Role._get_prefix 用 PREFIX/CONSTRAINT 模板拼 system_prompt(含环境内其他角色名);_think 用 STATE_TEMPLATE 注入对话历史让 LLM 选状态;Planner.get_useful_memories 用 STRUCTURAL_CONTEXT 裁剪上下文;ActionNode.compile 把 instruction+example+constraint 编译成结构化 prompt |
| nanobot | ContextBuilder 组装 system prompt(identity + 引导文件 AGENTS/SOUL/USER.md + 长期记忆 + 技能摘要);runner 内做上下文治理:drop 孤儿 tool 结果、backfill 缺失、_microcompact 折叠旧工具结果、按 token 预算 _snip_history、大工具结果落盘(maybe_persist_tool_result);turn 级 MCP/CLI-app runtime 注释行 |
| Open Multi-Agent | 4 种 contextStrategy:sliding-window(按 turn 边界保尾)、summarize(LLM 摘要旧消息+缓存)、compact(规则压缩、保留 tool_use/截断长文本)、custom;另 compressToolResults 压已消费工具输出;按 turn 边界切分避免孤立 tool_use_id;prompt 注入仅默认给”依赖任务输出”(default-deny),memoryScope:‘all’ 才给全量 |
| OpenClaw | system prompt 由 harness 组装(注入 AGENTS.md/工作区文件 + 可见 skills 元数据 XML 块);transformContext/convertToLlm 在每轮把 AgentMessage[] 转 LLM Message[];超窗自动 compaction(摘要旧消息、保留 file-ops 清单 readFiles/modifiedFiles),并有 compaction-safeguard / context-pruning 运行时 hook |
| Pipecat | LLMContext 为单一上下文真相,由 LLMContextAggregatorPair 拆成 user/assistant 两个 aggregator 在管道里增量聚合;system_instruction 经 service 注入并可 append_system_instruction;LLMContextSummaryRequestFrame 触发上下文摘要(context-summarization 示例) |
| PraisonAI | system prompt 由 instructions/role/goal/backstory 拼装;可选 ContextCompactor(execution.context_compaction=True 时超 max_context_tokens 自动摘要压缩,带 BEFORE/AFTER_COMPACTION hook);ContextAgent 做 fast-context 注入;RAG 检索结果按需拼入 |
| Semantic Kernel | system prompt 经 IPromptTemplate(Handlebars/Liquid/SK 语法)渲染并注入变量;AggregateAIContextProvider 在每轮调用前聚合多个 AIContextProvider 的额外指令/函数注入到 kernel(AddFromAIContext);ChatHistory reducer 控制 token 预算 |
| smolagents | system_prompt 由 Jinja 模板 populate_template 注入 tools/managed_agents/authorized_imports/instructions;每步把记忆重放为消息;planning summary_mode 裁剪历史;observation/输出经 truncate_content 截断。无自动压缩 |
| Strands Agents | system_prompt 支持 str 或 SystemContentBlock 列表(含 cache point);ConversationManager 在 ContextWindowOverflowException 时 reduce_context 重试(agent.py:1055);count_tokens 启发式估算(tiktoken 或 chars/4)做前瞻压缩;context_offloader vended plugin |
| Swarm | system=instructions + history;context_variables 注入函数且对模型隐藏 |
| SwarmClaw | prompt 由众多 section 组合(identity/planning/thinking/runtime/workspace/agent-awareness/situational/project/credential/delegation/run-context…);自动 compaction(context 阈值触发,可配 generation preference);内部 meta 标记用”平衡括号+zod”剥离不外泄 |
| Swarms | system_prompt 注入 + 历史拼接(return_history_as_string);v12 ContextCompressor 在 token 用量超 90% 阈值时 maybe_compress 摘要旧消息;transforms 可在每轮重写 task_prompt;dynamic_context_window 工具 |
| Transformers Agents | 系统提示 + 工具描述 + ReAct 轨迹拼接 |
| Upsonic | context_management=True 时中间件在接近 model 上下文上限时剪枝工具历史 + 摘要旧消息(保留 context_management_keep_recent(默认5) 条,可指定更大窗口的 context_management_model);system_prompt 由 SystemPromptBuildStep 组装(role/goal/instructions/education/work_experience/culture/metadata) |
| vectara-agentic | 提示词模板化:GENERAL_PROMPT_TEMPLATE / REACT_PROMPT_TEMPLATE(prompts.py:134,150)由 format_prompt 注入 topic/date/general+custom 指令(factory.py:32)。get_general_instructions 按是否含 DB 工具动态拼接指令(prompts.py:111);强约束“仅基于工具输出、内联引用”。Gemini 工具需 sanitize_tools_for_gemini(agent.py:153) |
| VoltAgent | system prompt(instructions 静态/动态)、conversation buffer、消息归一化(message-normalizer)、按 token 的上下文裁剪(contextLimit)与 apply-summarization 摘要旧消息;createPrompt 工具 |
各框架实现对比 · 源码级
Aeon yaml prompt 注入分层:CLAUDE.md(Claude Code 自动加载的 agent 身份/规则/安全约束)+ 当前 SKILL.md + 链上下文文件 + var。可选 soul/(SOUL.md/STYLE.md/examples)注入人格风格。明确防注入:外部内容一律当不可信数据
prompt 注入分层:CLAUDE.md(Claude Code 自动加载的 agent 身份/规则/安全约束)+ 当前 SKILL.md + 链上下文文件 + var。可选 soul/(SOUL.md/STYLE.md/examples)注入人格风格。明确防注入:外部内容一律当不可信数据
github/workflows/aeon.yml:464 SKILL_NAME="${{ steps.skill.outputs.name }}"
VAR="$SKILL_VAR"
# Build prompt — inject chain context if running as part of a chain
CHAIN_CTX="${{ inputs.chain_context_file }}"
if [ -n "$CHAIN_CTX" ] && [ -f "$CHAIN_CTX" ]; then
CONTEXT=$(cat "$CHAIN_CTX")
PROMPT="Today is $TODAY.
## Context from prior chain steps
$CONTEXT
Read and execute the skill defined in skills/${SKILL_NAME}/SKILL.md AG2 python system message 可静态或 UpdateSystemMessage 动态生成;group 模块的 ContextVariables 在 Agent 间共享可变状态并驱动条件转移/system 模板(ContextExpression/ContextStr);transform_messages capability 做消息裁剪/压缩/限长
system message 可静态或 UpdateSystemMessage 动态生成;group 模块的 ContextVariables 在 Agent 间共享可变状态并驱动条件转移/system 模板(ContextExpression/ContextStr);transform_messages capability 做消息裁剪/压缩/限长
group/context_variables.py:18__CONTEXT_VARIABLES_PARAM_NAME__ = "context_variables"
class ContextVariables(BaseModel):
"""Stores and manages context variables for agentic workflows.
Utilises a dictionary-like interface for setting, getting, and removing variables.
"""
# Internal storage for context variables
data: dict[str, Any] = Field(default_factory=dict)
def __init__(self, data: dict[str, Any] | None = None, **kwargs: Any) -> None: Agency Swarm python 运行前把 shared_instructions(agency 级)+ agent 自身 instructions + 本次 additional_instructions 拼成最终 system prompt(execution_helpers.py:307 起,运行后还原 :435);instructions 支持 str/文件路径;MessageFilter/MessageFormatter 决定哪些消息进 history、打 agent 元数据
运行前把 shared_instructions(agency 级)+ agent 自身 instructions + 本次 additional_instructions 拼成最终 system prompt(execution_helpers.py:307 起,运行后还原 :435);instructions 支持 str/文件路径;MessageFilter/MessageFormatter 决定哪些消息进 history、打 agent 元数据
agent/execution_helpers.py:307 _validate_agency_for_delegation(agent, sender_name, agency_context)
# Store original instructions for restoration
original_instructions = agent.instructions
# Temporarily modify instructions if shared or additional instructions provided
shared_instructions_text = _resolve_latest_shared_instructions(agency_context)
if additional_instructions and not isinstance(additional_instructions, str):
raise ValueError("additional_instructions must be a string")
additional_for_run: str | None = additional_instructions or None Agent-LLM (AGiXT) python 注入向量记忆(injected_memories/context_results)+ 近期对话(conversation_results)+ 可选 web 搜索(Websearch.py)+ 浏览链接;两段式命令选择先用大窗口模型从全部命令里筛出相关子集再注入,控制工具上下文膨胀;对话历史可压缩
注入向量记忆(injected_memories/context_results)+ 近期对话(conversation_results)+ 可选 web 搜索(Websearch.py)+ 浏览链接;两段式命令选择先用大窗口模型从全部命令里筛出相关子集再注入,控制工具上下文膨胀;对话历史可压缩
Interactions.py:3247Interactions.py:2831Interactions.py:2521 )
return final_answer
async def run_stream(
self,
user_input: str = "",
context_results: int = 100,
conversation_name: str = "",
conversation_id: str = None,
browse_links: bool = False,
websearch: bool = False,
images: list = [],
log_user_input: bool = True, AgentDock typescript createSystemPrompt(agentConfig, dynamicState) 由 personality+动态 orchestration 状态(activeStep/recentlyUsedTools)拼 system prompt,并注入当前日期/时区;applyHistoryPolicy 按 none/lastN/all 裁剪历史;模板级 tokenOptimization(compressToolOutputs/maxToolOutputTokens)
createSystemPrompt(agentConfig, dynamicState) 由 personality+动态 orchestration 状态(activeStep/recentlyUsedTools)拼 system prompt,并注入当前日期/时区;applyHistoryPolicy 按 none/lastN/all 裁剪历史;模板级 tokenOptimization(compressToolOutputs/maxToolOutputTokens)
utils/prompt-utils.ts:25utils/message-utils.ts:89nodes/agent-node.ts:534 * @param dynamicState Optional dynamic state information for the current turn
* @returns The formatted system prompt string
*/
export function createSystemPrompt(
agentConfig: any,
dynamicState?: DynamicOrchestrationState
): string {
if (!agentConfig) {
return '';
}
// Start with the personality as the base prompt
const personality = agentConfig.personality; AgentField go 自动上下文传播(Workflow/Session/Actor/Execution ID 经 header 转发,X-Workflow-ID/X-Execution-ID);app.ai 的 system/user/schema 拼装;harness 支持 system_prompt 覆盖与 env 注入。无内建 token 压缩/auto-compact(依赖外部 harness 自管)
自动上下文传播(Workflow/Session/Actor/Execution ID 经 header 转发,X-Workflow-ID/X-Execution-ID);app.ai 的 system/user/schema 拼装;harness 支持 system_prompt 覆盖与 env 注入。无内建 token 压缩/auto-compact(依赖外部 harness 自管)
agent.py:34agent_ai.py:402agent.py:3335from agentfield.agent_server import AgentServer
from agentfield.agent_workflow import AgentWorkflow
from agentfield.client import AgentFieldClient, ApprovalResult
from agentfield.execution_context import (
ExecutionContext,
get_current_context,
reset_execution_context,
set_execution_context,
)
from agentfield.execution_state import ExecuteError
from agentfield.did_manager import DIDManager
from agentfield.vc_generator import VCGenerator
from agentfield.memory import MemoryClient, MemoryInterface Agentic Context Engine (ACE) python 本框架重点。Skillbook 即被工程化的上下文:策略以 XML <strategy> 注入 agent prompt(render_skills_xml);检索用 BM25+dense 的 RRF top-k;SkillManager 用 ADD/UPDATE/TAG/REMOVE 自策展;as_prompt() 渲染整本;外部 agent 用 wrap_skillbook_for_external_agent 注入
本框架重点。Skillbook 即被工程化的上下文:策略以 XML <strategy> 注入 agent prompt(render_skills_xml);检索用 BM25+dense 的 RRF top-k;SkillManager 用 ADD/UPDATE/TAG/REMOVE 自策展;as_prompt() 渲染整本;外部 agent 用 wrap_skillbook_for_external_agent 注入
ace/implementations/skill_rendering.py:44ace/core/skillbook.py:767ace/integrations/langchain.py:149 return sum(1 for keyword in keywords if keyword in skill.keywords)
def render_skills_xml(skills: list["Skill"]) -> str:
"""Render skills as XML ``<strategy>`` elements."""
if not skills:
return ""
parts: list[str] = []
for skill in skills:
keyword_attr = ",".join(skill.keywords)
body = [f" <issue>{escape(skill.issue)}</issue>"]
if skill.insight: AgentScope python 每轮 reasoning 前 compress_context():count_tokens 超 trigger_ratio(0.8)context_size 即触发,按 reserve_ratio(0.1) 拆分待压/保留,用结构化 SummarySchema(task_overview/current_state/...) 让模型生成摘要回填;工具结果按 tool_result_limit(3000) 截断;system_prompt 可经 middleware on_system_prompt 管道改写
每轮 reasoning 前 compress_context():count_tokens 超 trigger_ratio(0.8)context_size 即触发,按 reserve_ratio(0.1) 拆分待压/保留,用结构化 SummarySchema(task_overview/current_state/...) 让模型生成摘要回填;工具结果按 tool_result_limit(3000) 截断;system_prompt 可经 middleware on_system_prompt 管道改写
agent/_agent.py:300middleware/_base.py:215
await execute_chain()
async def _compress_context_impl(
self,
context_config: ContextConfig | None = None,
) -> None:
"""Compress the agent's context if the token count exceeds the
threshold.
Args:
context_config (`ContextConfig | None`, optional):
If provided, compress the context with the given context Agentset typescript 检索后用模板把 chunks 包成 <source_n>...</source_n> 注入(utils.ts:13);强约束 system prompt 要求"仅基于来源作答 + 强制 [n] 引用"(prompts.ts:3);多轮先 condense 历史为单 query 控上下文
检索后用模板把 chunks 包成 <source_n>...</source_n> 注入(utils.ts:13);强约束 system prompt 要求"仅基于来源作答 + 强制 [n] 引用"(prompts.ts:3);多轮先 condense 历史为单 query 控上下文
apps/web/src/lib/prompts.ts:3agentic/utils.ts:13chat/route.ts:87import { prmpt } from "@/lib/prompt";
export const DEFAULT_SYSTEM_PROMPT = prmpt`
You are an AI assistant powered by Agentset. Your primary task is to provide accurate, factual responses based STRICTLY on the provided search results. You must ONLY answer questions using information explicitly found in the search results - do not make assumptions or add information from outside knowledge.
Follow these STRICT guidelines:
1. If the search results do not contain information to fully answer the query, state clearly: "I cannot fully answer this question based on the available information." Then explain what specific aspects cannot be answered.
2. Only use information directly stated in the search results - do not infer, assume, or add external knowledge.
3. Your response must match the language of the user's query.
4. Citations are MANDATORY for every factual statement. Format citations by placing the chunk number in brackets immediately after the relevant statement with no space, like this: "The temperature is 20 degrees[3]"
5. When possible, include relevant direct quotes from the search results with proper citations.
6. Do not preface responses with phrases like "based on the search results" - simply provide the cited answer. AgentVerse python prompt 用 string.Template 占位符填充:${agent_name}/${env_description}/${role_description}/${chat_history}(tool agent 另加 ${tools}/${tool_names}/${tool_observation});prepend_prompt_template+append_prompt_template 分段拼接(agents/base.py:62);环境描述由 Describer 规则按 agent 动态生成注入
prompt 用 string.Template 占位符填充:${agent_name}/${env_description}/${role_description}/${chat_history}(tool agent 另加 ${tools}/${tool_names}/${tool_observation});prepend_prompt_template+append_prompt_template 分段拼接(agents/base.py:62);环境描述由 Describer 规则按 agent 动态生成注入
agents/simulation_agent/conversation.py:84agents/simulation_agent/tool.py:144agents/base.py:62 )
return message
def _fill_prompt_template(self, env_description: str = "") -> str:
"""Fill the placeholders in the prompt template
In the conversation agent, three placeholders are supported:
- ${agent_name}: the name of the agent
- ${env_description}: the description of the environment
- ${role_description}: the description of the role of the agent
- ${chat_history}: the chat history of the agent
"""
input_arguments = { Ailoy rust 本地模型用 minijinja 渲染 chat template(src/model/local/chat_template.rs);RAG 文档经 DocumentPolyfill(Qwen3 模板)注入 system/query 消息,适配"原生不支持知识输入"的模型(src/model/polyfill.rs:13,42);推理参数 LangModelInferConfig(temperature/top_p/max_tokens/grammar/think_effort,src/model/language_model.rs:101)
本地模型用 minijinja 渲染 chat template(src/model/local/chat_template.rs);RAG 文档经 DocumentPolyfill(Qwen3 模板)注入 system/query 消息,适配"原生不支持知识输入"的模型(src/model/polyfill.rs:13,42);推理参数 LangModelInferConfig(temperature/top_p/max_tokens/grammar/think_effort,src/model/language_model.rs:101)
src/model/polyfill.rs:42src/model/language_model.rs:101 }
}
pub fn polyfill(
&self,
mut msgs: Vec<Message>,
documents: Vec<Document>,
) -> anyhow::Result<Vec<Message>> {
// Find part indices
// @jhlee: Currently, templating applies only to the first text `Part` within a message.
// If the text `Part` is splitted, it might lead to undefined behavior.
// We need to find a more robust solution for handling multiple Parts and non-text cases.
fn get_part_idx(msgs: &[Message], msg_idx: Option<usize>) -> Option<usize> { Astron Agent python 模板拼装式:system prompt 由 {now}/{instruct}/{knowledge}/{tools}/{tool_names}/{r1_more} 占位替换(对 xdeepseekr1 模型走专用模板分支),user prompt 拼 {chat_history}+{question}+{scratchpad};knowledge 检索结果(含图片/表格引用替换)作为背景注入
模板拼装式:system prompt 由 {now}/{instruct}/{knowledge}/{tools}/{tool_names}/{r1_more} 占位替换(对 xdeepseekr1 模型走专用模板分支),user prompt 拼 {chat_history}+{question}+{scratchpad};knowledge 检索结果(含图片/表格引用替换)作为背景注入
cot_runner.py:44cot_prompt.py:1workflow_agent_builder.py:210 process_runner: CotProcessRunner
max_loop: int = Field(default=30)
async def create_system_prompt(self) -> str:
system_prompt = COT_SYSTEM_TEMPLATE.replace("{now}", self.cur_time())
system_prompt = system_prompt.replace("{instruct}", self.instruct or "无")
system_prompt = system_prompt.replace("{knowledge}", self.knowledge or "无")
system_prompt = system_prompt.replace(
"{tools}", "\n".join([tool.schema_template for tool in self.plugins])
)
system_prompt = system_prompt.replace(
"{tool_names}", ",".join([tool.name for tool in self.plugins])
) AutoGen python ChatCompletionContext 抽象管理喂给 LLM 的消息窗口;多策略实现:UnboundedChatCompletionContext、BufferedChatCompletionContext(取最近 N)、HeadAndTailChatCompletionContext、TokenLimitedChatCompletionContext;system_message + memory 注入 + _get_compatible_context(按 model_info 去图像)
ChatCompletionContext 抽象管理喂给 LLM 的消息窗口;多策略实现:UnboundedChatCompletionContext、BufferedChatCompletionContext(取最近 N)、HeadAndTailChatCompletionContext、TokenLimitedChatCompletionContext;system_message + memory 注入 + _get_compatible_context(按 model_info 去图像)
autogen-core/src/autogen_core/model_context/_chat_completion_context.py:10from ..models import LLMMessage
class ChatCompletionContext(ABC, ComponentBase[BaseModel]):
"""An abstract base class for defining the interface of a chat completion context.
A chat completion context lets agents store and retrieve LLM messages.
It can be implemented with different recall strategies.
Args:
initial_messages (List[LLMMessage] | None): The initial messages.
Example: Botpress typescript 双模 prompt 系统(chat-mode/ vs worker-mode/,Markdown 模板编译成 TS);运行时注入工具签名/schema/历史;truncateWrappedContent 按 token 上限智能截断(带 flex/minTokens 标记),保证不超模型窗口
双模 prompt 系统(chat-mode/ vs worker-mode/,Markdown 模板编译成 TS);运行时注入工具签名/schema/历史;truncateWrappedContent 按 token 上限智能截断(带 flex/minTokens 标记),保证不超模型窗口
packages/llmz/src/llmz.ts:444packages/llmz/src/truncator.ts:8 const modelLimit = Math.max(model.input.maxTokens, 8_000)
const responseLengthBuffer = getModelOutputLimit(modelLimit)
const messages = truncateWrappedContent({
messages: iteration.messages,
tokenLimit: modelLimit - responseLengthBuffer,
throwOnFailure: true,
}).filter(
(x) =>
// Filter out empty messages, as they are not valid inputs for the LLM
// This can happen when a message is truncated and the content is empty
typeof x.content !== 'string' || x.content.trim().length > 0
) ConnectOnion python system_prompt 支持 str/文件/Path;auto_compact 插件在 context≥90% 时用 gemini-flash 摘要旧消息(保留 system+摘要+最近5条);system_reminder/ulw 等插件注入上下文
system_prompt 支持 str/文件/Path;auto_compact 插件在 context≥90% 时用 gemini-flash 摘要旧消息(保留 system+摘要+最近5条);system_reminder/ulw 等插件注入上下文
useful_plugins/auto_compact.py:30 from ..core.agent import Agent
# Threshold for auto-compaction (90% = 10% remaining)
COMPACT_THRESHOLD = 90
@after_llm
def check_and_compact(agent: 'Agent') -> None:
"""Check context usage and auto-compact if needed."""
import uuid
# Get current context usage
context_percent = agent.context_percent Cordum go 指针化:Gateway 把输入 context JSON 写 Redis 设 context_ptr,总线只带指针;Safety Kernel 评估时按需解引用做内容级扫描(如 PII/payload 字段提取)
指针化:Gateway 把输入 context JSON 写 Redis 设 context_ptr,总线只带指针;Safety Kernel 评估时按需解引用做内容级扫描(如 PII/payload 字段提取)
DESIGN.md:112core/controlplane/scheduler/safety_client.go:185`source_component`, `details` (map), `trace_id`. Deprecated string fields (`level`,
`component`, `code`) remain populated for backward compatibility.
## 4) Pointer-Based State Separation
The bus carries **pointers**, not large payloads. Input, output, and artifacts
live in Redis and are referenced by pointers:
- `context_ptr` -> `redis://ctx:<job_id>`
- `result_ptr` -> `redis://res:<job_id>`
- `artifact_ptrs` -> `redis://art:<id>`
This keeps bus payloads small, preserves durability, and allows audit tooling Cortex Memory rust 核心卖点。三层渐进披露按查询意图动态加权(EntityLookup 偏 L2 0.7、Relational 偏 L1 0.5 等,search/weight_model.rs:45 weights_for_intent),让接入方只加载所需粒度,省 token
核心卖点。三层渐进披露按查询意图动态加权(EntityLookup 偏 L2 0.7、Relational 偏 L1 0.5 等,search/weight_model.rs:45 weights_for_intent),让接入方只加载所需粒度,省 token
search/weight_model.rs:11search/vector_engine.rs:353layers/generator.rs:56 pub l2: f32,
}
impl Default for LayerWeights {
fn default() -> Self {
Self {
l0: 0.2,
l1: 0.3,
l2: 0.5,
}
}
} CrewAI python Task 描述模板插值({topic});task context 由前序 TaskOutput 串联(_get_context);执行前注入 knowledge(RAG) 检索与 memory 召回;超窗时 respect_context_window 处理
Task 描述模板插值({topic});task context 由前序 TaskOutput 串联(_get_context);执行前注入 knowledge(RAG) 检索与 memory 召回;超窗时 respect_context_window 处理
crewai/agent/core.py:782crewai/crew.py:1564crewai/agents/crew_agent_executor.py:444 ValueError: If the max execution time is not a positive integer.
RuntimeError: If the agent execution fails for other reasons.
"""
task_prompt = self._prepare_task_execution(task, context)
knowledge_config = get_knowledge_config(self)
task_prompt = handle_knowledge_retrieval(
self,
task,
task_prompt,
knowledge_config,
self.knowledge.query if self.knowledge else lambda *a, **k: None,
self.crew.query_knowledge Dust typescript system prompt 由 constructPromptMultiActions 组装并注入 memory/toolsets/user/workspace 上下文;超长时 compactionWorkflow 用专门 prompt 把历史摘要为 compaction 消息,保留最近若干轮交互
system prompt 由 constructPromptMultiActions 组装并注入 memory/toolsets/user/workspace 上下文;超长时 compactionWorkflow 用专门 prompt 把历史摘要为 compaction 消息,保留最近若干轮交互
front/temporal/agent_loop/lib/compaction.ts:28import type { Result } from "@app/types/shared/result";
import { Err, Ok } from "@app/types/shared/result";
const COMPACTION_PROMPT = `Your task is to create a detailed summary of the conversation so far, \
paying close attention to the user's explicit requests and the agents' previous actions and \
responses. This summary should be thorough enough that the conversation can continue without \
losing important context.
While writing the summary make sure to consider for each messages so far:
- The user's explicit requests and intents
- The agents' approaches to addressing those requests
- Key decisions and information exchanged Haystack python 框架核心卖点:上下文如何被检索/排序/过滤/拼装全部显式可控——PromptBuilder 用 Jinja2 模板拼 prompt,Ranker 重排,Joiner 合并多路文档,Router 条件路由;Agent 的 system/user prompt 支持 Jinja2 模板(ChatPromptBuilder),required_variables 校验
框架核心卖点:上下文如何被检索/排序/过滤/拼装全部显式可控——PromptBuilder 用 Jinja2 模板拼 prompt,Ranker 重排,Joiner 合并多路文档,Router 条件路由;Agent 的 system/user prompt 支持 Jinja2 模板(ChatPromptBuilder),required_variables 校验
agent.py:314agent.py:343 component.set_output_types(self, **output_types)
# required_variables is initially set to [] and populated later by _register_prompt_variables
self._user_chat_prompt_builder = (
ChatPromptBuilder(template=user_prompt, required_variables=[]) if user_prompt is not None else None
)
# Only create a system prompt builder when the prompt uses Jinja2 message syntax
self._system_chat_prompt_builder: ChatPromptBuilder | None = None
if system_prompt is not None and _JINJA2_CHAT_TEMPLATE_RE.search(system_prompt):
self._system_chat_prompt_builder = ChatPromptBuilder(template=system_prompt, required_variables=[])
self._register_prompt_variables() hcom rust bootstrap primer 注入身份+CLI 契约(bootstrap.rs:34);config 的 hints(追加到每条收到的消息)与 notes(启动一次性追加)(README:309);hcom bundle prepare 把事件/文件/转录片段打包成结构化 handoff 上下文(commands/bundle.rs, send.rs:23)
bootstrap primer 注入身份+CLI 契约(bootstrap.rs:34);config 的 hints(追加到每条收到的消息)与 notes(启动一次性追加)(README:309);hcom bundle prepare 把事件/文件/转录片段打包成结构化 handoff 上下文(commands/bundle.rs, send.rs:23)
bootstrap.rs:34// lifecycle transitions (launch → stop → resume). The underlying binding
// changes; the flags don't.
const UNIVERSAL: &str = r#"[HCOM SESSION]
You have access to the hcom communication tool.
- Your name: {display_name}
- Authority: Prioritize @{SENDER} over others
- Important: Include this marker anywhere in your first response only: [hcom:{instance_name}]
You run hcom commands on behalf of the human user. The human uses natural language with you.
## MESSAGES Hermes Agent python 可插拔 ContextEngine 抽象基类(config context.engine 选,默认 compressor);ContextCompressor 用辅助小模型摘要中段、保护首尾(token 预算)、结构化模板(Resolved/Pending/Remaining Work)、迭代式更新;记忆/skill 注入用 <memory-context> 围栏 + 去注入清洗
可插拔 ContextEngine 抽象基类(config context.engine 选,默认 compressor);ContextCompressor 用辅助小模型摘要中段、保护首尾(token 预算)、结构化模板(Resolved/Pending/Remaining Work)、迭代式更新;记忆/skill 注入用 <memory-context> 围栏 + 去注入清洗
agent/context_engine.py:32agent/context_compressor.py:1agent/memory_manager.py:54from typing import Any, Dict, List
class ContextEngine(ABC):
"""Base class all context engines must implement."""
# -- Identity ----------------------------------------------------------
@property
@abstractmethod
def name(self) -> str:
"""Short identifier (e.g. 'compressor', 'lcm').""" Hive python "洋葱模型"分层 prompt 组合:identity_prompt(Layer1 静态身份) + 节点 system_prompt 分层叠加(continuous/isolated 两种 conversation_mode,edge.py:366);Goal.to_prompt_context() 注入每次 LLM 调用;超 token 自动 compaction(LLM 摘要 + emergency summary)
"洋葱模型"分层 prompt 组合:identity_prompt(Layer1 静态身份) + 节点 system_prompt 分层叠加(continuous/isolated 两种 conversation_mode,edge.py:366);Goal.to_prompt_context() 注入每次 LLM 调用;超 token 自动 compaction(LLM 摘要 + emergency summary)
orchestrator/edge.py:375schemas/goal.py:91 "'isolated': each node gets a fresh conversation."
),
)
identity_prompt: str | None = Field(
default=None,
description=(
"Agent-level identity prompt (Layer 1 of the onion model). "
"In continuous mode, this is the static identity that persists "
"unchanged across all node transitions. In isolated mode, ignored."
),
)
# Metadata Lagent python Aggregator 负责把 memory 拼成 OpenAI message:DefaultAggregator 合并 system+历史,InternLMToolAggregator 处理工具步骤;output_format(parser) 用 format_instruction() 注入格式要求、parse_response() 抽取 thought/action 存入 AgentMessage.formatted
Aggregator 负责把 memory 拼成 OpenAI message:DefaultAggregator 合并 system+历史,InternLMToolAggregator 处理工具步骤;output_format(parser) 用 format_instruction() 注入格式要求、parse_response() 抽取 thought/action 存入 AgentMessage.formatted
prompts/parsers/tool_parser.py:24prompts/parsers/tool_parser.py:93agents/agent.py:104 PARSING_ERROR = -1
class ToolParser(StrParser):
def __init__(
self,
tool_type: str,
template: str = '',
begin: str = '<tool>\n',
end: str = '</tool>\n',
validate: Callable[[str], Any] = None,
**kwargs LangChain python system_prompt 注入在 _execute_model_sync(factory.py:1300);SummarizationMiddleware 近上限时用 LLM 摘要旧消息并 RemoveMessage 重写历史;ContextEditingMiddleware(ClearToolUsesEdit) 清理工具输出;dynamic_prompt 钩子动态改 prompt
system_prompt 注入在 _execute_model_sync(factory.py:1300);SummarizationMiddleware 近上限时用 LLM 摘要旧消息并 RemoveMessage 重写历史;ContextEditingMiddleware(ClearToolUsesEdit) 清理工具输出;dynamic_prompt 钩子动态改 prompt
agents/middleware/summarization.py:33middleware/types.py:65
TokenCounter = Callable[[Iterable[MessageLikeRepresentation]], int]
DEFAULT_SUMMARY_PROMPT = """<role>
Context Extraction Assistant
</role>
<primary_objective>
Your sole objective in this task is to extract the highest quality/most relevant context from the conversation history below.
</primary_objective>
<objective_information>
You're nearing the total number of input tokens you can accept, so you must extract the highest quality/most relevant pieces of information from your conversation history. Llama Agentic System (llama-stack-apps) python instructions(system prompt) + sampling_params(top_p/greedy);document/attachment 注入(agent.create_turn(documents=...)),RAG query_config(max_chunks/max_tokens_in_context) 控制召回上下文长度;首轮注入 system message 的手动管理
instructions(system prompt) + sampling_params(top_p/greedy);document/attachment 注入(agent.create_turn(documents=...)),RAG query_config(max_chunks/max_tokens_in_context) 控制召回上下文长度;首轮注入 system message 的手动管理
examples/agents/chat_with_documents.py:83examples/agent_store/api.py:100examples/agents/agent_with_tools.py:49 ]
for prompt, documents in user_prompts:
response = agent.create_turn(
messages=[
{
"role": "user",
"content": prompt,
}
],
documents=documents,
session_id=session_id,
) LlamaIndex python system_prompt 前置;state_prompt 把运行时 state 注入最后一条 user 消息(DEFAULT_STATE_PROMPT);ReAct 用 ReActChatFormatter 把工具描述+reasoning 步骤渲进 system header 模板;RAG 检索结果作为上下文喂入;memory block 模板化注入
system_prompt 前置;state_prompt 把运行时 state 注入最后一条 user 消息(DEFAULT_STATE_PROMPT);ReAct 用 ReActChatFormatter 把工具描述+reasoning 步骤渲进 system header 模板;RAG 检索结果作为上下文喂入;memory block 模板化注入
agent/workflow/base_agent.py:437agent/react/formatter.py:51 # send to the current agent
return AgentInput(input=input_messages, current_agent_name=self.name)
@step
async def setup_agent(self, ctx: Context, ev: AgentInput) -> AgentSetup:
"""Main agent handling logic."""
llm_input = [*ev.input]
if self.system_prompt:
llm_input = [
ChatMessage(role="system", content=self.system_prompt),
*llm_input,
] llm-agents python 极简:每轮把全部历史 '\n'.join(previous_responses) 原样拼回 prompt(无摘要/压缩/裁剪);唯一"工程"手段是 stop_pattern=['\nObservation:', ...] 防止 LLM 续写假 Observation
极简:每轮把全部历史 '\n'.join(previous_responses) 原样拼回 prompt(无摘要/压缩/裁剪);唯一"工程"手段是 stop_pattern=['\nObservation:', ...] 防止 LLM 续写假 Observation
agent.py:69agent.py:42 print(prompt.format(previous_responses=''))
while num_loops < self.max_loops:
num_loops += 1
curr_prompt = prompt.format(previous_responses='\n'.join(previous_responses))
generated, tool, tool_input = self.decide_next_action(curr_prompt)
if tool == 'Final Answer':
return tool_input
if tool not in self.tool_by_names:
raise ValueError(f"Unknown tool: {tool}")
tool_result = self.tool_by_names[tool].use(tool_input)
generated += f"\n{OBSERVATION_TOKEN} {tool_result}\n{THOUGHT_TOKEN}"
print(generated)
previous_responses.append(generated) LoongFlow python ① GradeMemory 自动压缩控上下文长度(LLMCompressor 摘要 MTM+STM,grade/memory.py:108);② Planner 把"父代解+评测+总结"作为经验注入下一轮 prompt;③ ReAct 每步把 system_prompt+历史+工具声明拼成 CompletionRequest(default_reasoner.py:37);场景 Agent 用 prompt 模板(如 claude_code/general_prompt.py)
① GradeMemory 自动压缩控上下文长度(LLMCompressor 摘要 MTM+STM,grade/memory.py:108);② Planner 把"父代解+评测+总结"作为经验注入下一轮 prompt;③ ReAct 每步把 system_prompt+历史+工具声明拼成 CompletionRequest(default_reasoner.py:37);场景 Agent 用 prompt 模板(如 claude_code/general_prompt.py)
agentsdk/memory/grade/memory.py:91framework/react/components/default_reasoner.py:28 stm=stm, mtm=mtm, ltm=ltm, token_counter=token_counter, config=config
)
async def add(self, messages: Message | List[Message] | None) -> None:
"""
Adds new messages to the memory system and triggers compression if the threshold is exceeded.
"""
if messages is None:
return
await self.stm.add(messages)
if not self.config.auto_compress: Maestro python 纯 prompt 拼接:历史结果直接字符串 join 进 prompt/system;无压缩/摘要/裁剪。仅有的"上下文管理"是输出≥4000 token 时递归续写防截断
纯 prompt 拼接:历史结果直接字符串 join 进 prompt/system;无压缩/摘要/裁剪。仅有的"上下文管理"是输出≥4000 token 时递归续写防截断
maestro.py:52maestro.py:130 {
"role": "user",
"content": [
{"type": "text", "text": f"Based on the following objective{' and file content' if file_content else ''}, and the previous sub-task results (if any), please break down the objective into the next sub-task, and create a concise and detailed prompt for a subagent so it can execute that task. IMPORTANT!!! when dealing with code tasks make sure you check the code for errors and provide fixes and support as part of the next sub-task. If you find any bugs or have suggestions for better code, please include them in the next sub-task prompt. Please assess if the objective has been fully achieved. If the previous sub-task results comprehensively address all aspects of the objective, include the phrase 'The task is complete:' at the beginning of your response. If the objective is not yet fully achieved, break it down into the next sub-task and create a concise and detailed prompt for a subagent to execute that task.:\n\nObjective: {objective}" + ('\\nFile content:\\n' + file_content if file_content else '') + f"\n\nPrevious sub-task results:\n{previous_results_text}"}
]
}
]
if use_search:
messages[0]["content"].append({"type": "text", "text": "Please also generate a JSON object containing a single 'search_query' key, which represents a question that, when asked online, would yield important information for solving the subtask. The question should be specific and targeted t
… Mastra typescript processor 管线:input/output/error processors(及可作 processor 的 workflow)在 LLM 调用前后改写消息/系统提示/工具集,内置 token-limiter、message-selection、system-prompt-scrubber、moderation、PII、prompt-injection、structured-output 等;system-reminders 注入;instructions/model/tools 均支持 DynamicArgument(按 requestContext 动态解析)
processor 管线:input/output/error processors(及可作 processor 的 workflow)在 LLM 调用前后改写消息/系统提示/工具集,内置 token-limiter、message-selection、system-prompt-scrubber、moderation、PII、prompt-injection、structured-output 等;system-reminders 注入;instructions/model/tools 均支持 DynamicArgument(按 requestContext 动态解析)
processors/index.ts:52agent/agent.ts:334 public id: TAgentId;
public name: string;
public source?: DefinitionSource;
#instructions: DynamicArgument<AgentInstructions, TRequestContext>;
readonly #description?: string;
readonly #metadata?: DynamicArgument<Record<string, unknown>, TRequestContext>;
model: DynamicArgument<MastraModelConfig | ModelWithRetries[], TRequestContext> | ModelFallbacks;
#originalModel: DynamicArgument<MastraModelConfig | ModelWithRetries[], TRequestContext> | ModelFallbacks;
maxRetries?: number;
#mastra?: Mastra;
#pubsub?: PubSub;
#inheritedPubSub?: PubSub;
#memory?: DynamicArgument<MastraMemory, TRequestContext>; MetaGPT python Role._get_prefix 用 PREFIX/CONSTRAINT 模板拼 system_prompt(含环境内其他角色名);_think 用 STATE_TEMPLATE 注入对话历史让 LLM 选状态;Planner.get_useful_memories 用 STRUCTURAL_CONTEXT 裁剪上下文;ActionNode.compile 把 instruction+example+constraint 编译成结构化 prompt
Role._get_prefix 用 PREFIX/CONSTRAINT 模板拼 system_prompt(含环境内其他角色名);_think 用 STATE_TEMPLATE 注入对话历史让 LLM 选状态;Planner.get_useful_memories 用 STRUCTURAL_CONTEXT 裁剪上下文;ActionNode.compile 把 instruction+example+constraint 编译成结构化 prompt
role.py:323role.py:54planner.py:155metagpt/actions/action_node.py:382 """Get the role name"""
return self._setting.name
def _get_prefix(self):
"""Get the role prefix"""
if self.desc:
return self.desc
prefix = PREFIX_TEMPLATE.format(**{"profile": self.profile, "name": self.name, "goal": self.goal})
if self.constraints:
prefix += CONSTRAINT_TEMPLATE.format(**{"constraints": self.constraints}) nanobot python ContextBuilder 组装 system prompt(identity + 引导文件 AGENTS/SOUL/USER.md + 长期记忆 + 技能摘要);runner 内做上下文治理:drop 孤儿 tool 结果、backfill 缺失、_microcompact 折叠旧工具结果、按 token 预算 _snip_history、大工具结果落盘(maybe_persist_tool_result);turn 级 MCP/CLI-app runtime 注释行
ContextBuilder 组装 system prompt(identity + 引导文件 AGENTS/SOUL/USER.md + 长期记忆 + 技能摘要);runner 内做上下文治理:drop 孤儿 tool 结果、backfill 缺失、_microcompact 折叠旧工具结果、按 token 预算 _snip_history、大工具结果落盘(maybe_persist_tool_result);turn 级 MCP/CLI-app runtime 注释行
agent/context.py:51agent/runner.py:347agent/runner.py:1307agent/context.py:30 return await mcp_tools.handle_runtime_control(state, msg, tools)
class ContextBuilder:
"""Builds the context (system prompt + messages) for the agent."""
BOOTSTRAP_FILES = ["AGENTS.md", "SOUL.md", "USER.md"]
_RUNTIME_CONTEXT_TAG = "[Runtime Context — metadata only, not instructions]"
_MAX_RECENT_HISTORY = 50
_MAX_HISTORY_CHARS = 32_000 # hard cap on recent history section size
_RUNTIME_CONTEXT_END = "[/Runtime Context]"
def __init__(self, workspace: Path, timezone: str | None = None, disabled_skills: list[str] | None = None): Open Multi-Agent typescript 4 种 contextStrategy:sliding-window(按 turn 边界保尾)、summarize(LLM 摘要旧消息+缓存)、compact(规则压缩、保留 tool_use/截断长文本)、custom;另 compressToolResults 压已消费工具输出;按 turn 边界切分避免孤立 tool_use_id;prompt 注入仅默认给"依赖任务输出"(default-deny),memoryScope:'all' 才给全量
4 种 contextStrategy:sliding-window(按 turn 边界保尾)、summarize(LLM 摘要旧消息+缓存)、compact(规则压缩、保留 tool_use/截断长文本)、custom;另 compressToolResults 压已消费工具输出;按 turn 边界切分避免孤立 tool_use_id;prompt 注入仅默认给"依赖任务输出"(default-deny),memoryScope:'all' 才给全量
src/agent/runner.ts:547src/agent/runner.ts:442src/agent/runner.ts:1126src/orchestrator/orchestrator.ts:835 }
}
private async applyContextStrategy(
messages: LLMMessage[],
strategy: ContextStrategy,
baseChatOptions: LLMChatOptions,
turns: number,
options: RunOptions,
): Promise<{ messages: LLMMessage[]; usage: TokenUsage }> {
if (strategy.type === 'sliding-window') {
return { messages: this.truncateToSlidingWindow(messages, strategy.maxTurns), usage: ZERO_USAGE }
} OpenClaw typescript system prompt 由 harness 组装(注入 AGENTS.md/工作区文件 + 可见 skills 元数据 XML 块);transformContext/convertToLlm 在每轮把 AgentMessage[] 转 LLM Message[];超窗自动 compaction(摘要旧消息、保留 file-ops 清单 readFiles/modifiedFiles),并有 compaction-safeguard / context-pruning 运行时 hook
system prompt 由 harness 组装(注入 AGENTS.md/工作区文件 + 可见 skills 元数据 XML 块);transformContext/convertToLlm 在每轮把 AgentMessage[] 转 LLM Message[];超窗自动 compaction(摘要旧消息、保留 file-ops 清单 readFiles/modifiedFiles),并有 compaction-safeguard / context-pruning 运行时 hook
packages/agent-core/src/harness/system-prompt.ts:5harness/compaction/compaction.ts:1agent-loop.ts:355import type { Skill } from "./types.js";
/** Format model-visible skill metadata for inclusion in the harness system prompt. */
export function formatSkillsForSystemPrompt(skills: Skill[]): string {
// Hidden skills can still be invoked directly by host code, but should not be
// advertised to the model for autonomous selection.
const visibleSkills = skills.filter((skill) => !skill.disableModelInvocation);
if (visibleSkills.length === 0) {
return "";
}
const lines = [
"The following skills provide specialized instructions for specific tasks.", Pipecat python LLMContext 为单一上下文真相,由 LLMContextAggregatorPair 拆成 user/assistant 两个 aggregator 在管道里增量聚合;system_instruction 经 service 注入并可 append_system_instruction;LLMContextSummaryRequestFrame 触发上下文摘要(context-summarization 示例)
LLMContext 为单一上下文真相,由 LLMContextAggregatorPair 拆成 user/assistant 两个 aggregator 在管道里增量聚合;system_instruction 经 service 注入并可 append_system_instruction;LLMContextSummaryRequestFrame 触发上下文摘要(context-summarization 示例)
processors/aggregators/llm_context.py:93services/llm_service.py:476LLMContextMessage: TypeAlias = LLMStandardMessage | LLMSpecificMessage
class LLMContext:
"""Manages conversation context for LLM interactions.
Handles message history, tool definitions, tool choices, and multimedia
content for LLM conversations. Provides methods for message manipulation,
and content formatting.
"""
def __init__(
self, PraisonAI python system prompt 由 instructions/role/goal/backstory 拼装;可选 ContextCompactor(execution.context_compaction=True 时超 max_context_tokens 自动摘要压缩,带 BEFORE/AFTER_COMPACTION hook);ContextAgent 做 fast-context 注入;RAG 检索结果按需拼入
system prompt 由 instructions/role/goal/backstory 拼装;可选 ContextCompactor(execution.context_compaction=True 时超 max_context_tokens 自动摘要压缩,带 BEFORE/AFTER_COMPACTION hook);ContextAgent 做 fast-context 注入;RAG 检索结果按需拼入
agent/chat_mixin.py:558 # --- Context compaction (opt-in via ExecutionConfig.context_compaction) ---
# Compacts message history before sending to LLM. Zero overhead when disabled.
_execution_cfg = getattr(self, 'execution', None)
if _execution_cfg and getattr(_execution_cfg, 'context_compaction', False):
try:
from ..compaction import ContextCompactor
from ..hooks import HookEvent as _HookEvent
_max_tok = getattr(_execution_cfg, 'max_context_tokens', None) or 8000
_compactor = ContextCompactor(max_tokens=_max_tok)
if _compactor.needs_compaction(messages):
try:
self._hook_runner.execute_sync(_HookEvent.BEFORE_COMPACTION, None)
except Exception as e: Semantic Kernel csharp system prompt 经 IPromptTemplate(Handlebars/Liquid/SK 语法)渲染并注入变量;AggregateAIContextProvider 在每轮调用前聚合多个 AIContextProvider 的额外指令/函数注入到 kernel(AddFromAIContext);ChatHistory reducer 控制 token 预算
system prompt 经 IPromptTemplate(Handlebars/Liquid/SK 语法)渲染并注入变量;AggregateAIContextProvider 在每轮调用前聚合多个 AIContextProvider 的额外指令/函数注入到 kernel(AddFromAIContext);ChatHistory reducer 控制 token 预算
dotnet/src/SemanticKernel.Abstractions/Memory/AggregateAIContextProvider.cs:76dotnet/src/Agents/Core/ChatCompletionAgent.cs:84 }
/// <inheritdoc />
public override async Task<AIContext> ModelInvokingAsync(ICollection<ChatMessage> newMessages, CancellationToken cancellationToken = default)
{
var subContexts = await Task.WhenAll(this.Providers.Select(x => x.ModelInvokingAsync(newMessages, cancellationToken)).ToList()).ConfigureAwait(false);
subContexts = subContexts.Where(x => x != null).ToArray();
var combinedContext = new AIContext();
combinedContext.AIFunctions = subContexts.Where(x => x.AIFunctions != null).SelectMany(x => x.AIFunctions).ToList();
combinedContext.Instructions = string.Join("\n", subContexts.Where(x => !string.IsNullOrWhiteSpace(x.Instructions)).Select(x => x.Instructions));
return combinedContext;
} smolagents python system_prompt 由 Jinja 模板 populate_template 注入 tools/managed_agents/authorized_imports/instructions;每步把记忆重放为消息;planning summary_mode 裁剪历史;observation/输出经 truncate_content 截断。无自动压缩
system_prompt 由 Jinja 模板 populate_template 注入 tools/managed_agents/authorized_imports/instructions;每步把记忆重放为消息;planning summary_mode 裁剪历史;observation/输出经 truncate_content 截断。无自动压缩
agents.py:1620agents.py:684Strands Agents python system_prompt 支持 str 或 SystemContentBlock 列表(含 cache point);ConversationManager 在 ContextWindowOverflowException 时 reduce_context 重试(agent.py:1055);count_tokens 启发式估算(tiktoken 或 chars/4)做前瞻压缩;context_offloader vended plugin
system_prompt 支持 str 或 SystemContentBlock 列表(含 cache point);ConversationManager 在 ContextWindowOverflowException 时 reduce_context 重试(agent.py:1055);count_tokens 启发式估算(tiktoken 或 chars/4)做前瞻压缩;context_offloader vended plugin
agent.py:1208models/model.py:263 """
pass
async def count_tokens(
self,
messages: Messages,
tool_specs: list[ToolSpec] | None = None,
system_prompt: str | None = None,
system_prompt_content: list[SystemContentBlock] | None = None,
) -> int:
"""Estimate token count for the given input before sending to the model.
Used for proactive context management (e.g., triggering compression at a threshold). Swarm python system=instructions + history;context_variables 注入函数且对模型隐藏
system=instructions + history;context_variables 注入函数且对模型隐藏
core.py:42 debug: bool,
) -> ChatCompletionMessage:
context_variables = defaultdict(str, context_variables)
instructions = (
agent.instructions(context_variables)
if callable(agent.instructions)
else agent.instructions
)
messages = [{"role": "system", "content": instructions}] + history
debug_print(debug, "Getting chat completion for...:", messages)
tools = [function_to_json(f) for f in agent.functions]
# hide context_variables from model SwarmClaw typescript prompt 由众多 section 组合(identity/planning/thinking/runtime/workspace/agent-awareness/situational/project/credential/delegation/run-context…);自动 compaction(context 阈值触发,可配 generation preference);内部 meta 标记用"平衡括号+zod"剥离不外泄
prompt 由众多 section 组合(identity/planning/thinking/runtime/workspace/agent-awareness/situational/project/credential/delegation/run-context…);自动 compaction(context 阈值触发,可配 generation preference);内部 meta 标记用"平衡括号+zod"剥离不外泄
agents/main-agent-loop.ts:22import { getGoalById, resolveEffectiveGoal } from '@/lib/server/goals/goal-service'
import { getMission } from '@/lib/server/missions/mission-repository'
const LEGACY_META_LINE_RE = /\[(?:MAIN_LOOP_META|MAIN_LOOP_PLAN|MAIN_LOOP_REVIEW|AGENT_HEARTBEAT_META|AUTONOMY_TICK)\]\s*(\{[^\n]*\})?/i
const AUTONOMY_TICK_RE = /\[AUTONOMY_TICK\]\s*(\{[^\n]*\})/i
const HEARTBEAT_META_RE = /\[AGENT_HEARTBEAT_META\]\s*(\{[^\n]*\})/i
const MAX_PENDING_EVENTS = 16
const MAX_TIMELINE_ITEMS = 40
const MAX_WORKING_MEMORY_NOTES = 12
const DEFAULT_FOLLOWUP_DELAY_MS = 1500
const DEFAULT_MAX_FOLLOWUP_CHAIN = 3
const MAX_LIFETIME_ITERATIONS = 200 Swarms python system_prompt 注入 + 历史拼接(return_history_as_string);v12 ContextCompressor 在 token 用量超 90% 阈值时 maybe_compress 摘要旧消息;transforms 可在每轮重写 task_prompt;dynamic_context_window 工具
system_prompt 注入 + 历史拼接(return_history_as_string);v12 ContextCompressor 在 token 用量超 90% 阈值时 maybe_compress 摘要旧消息;transforms 可在每轮重写 task_prompt;dynamic_context_window 工具
agent.py:528agents/context_compressor.py:40agent.py:1658 # integer max_loops runs. Gated purely on the user-facing boolean.
self.context_compression = context_compression
if self.context_compression:
self._context_compressor = ContextCompressor(
threshold=0.9
)
else:
self._context_compressor = None
# Initialize autonomous loop tracking structures
self.autonomous_subtasks = [] # List of subtasks from plan
self.current_subtask_index = (
0 # Current subtask being executed Transformers Agents python 系统提示 + 工具描述 + ReAct 轨迹拼接
系统提示 + 工具描述 + ReAct 轨迹拼接
查看 Transformers Agents 完整笔记 →Upsonic python context_management=True 时中间件在接近 model 上下文上限时剪枝工具历史 + 摘要旧消息(保留 context_management_keep_recent(默认5) 条,可指定更大窗口的 context_management_model);system_prompt 由 SystemPromptBuildStep 组装(role/goal/instructions/education/work_experience/culture/metadata)
context_management=True 时中间件在接近 model 上下文上限时剪枝工具历史 + 摘要旧消息(保留 context_management_keep_recent(默认5) 条,可指定更大窗口的 context_management_model);system_prompt 由 SystemPromptBuildStep 组装(role/goal/instructions/education/work_experience/culture/metadata)
agent.py:246src/upsonic/agent/context_managers/context_management_middleware.py:80pipeline/steps.py:854 is_completed: Whether the run completed successfully.
"""
from upsonic.utils.printing import info_log, warning_log
if output is None:
return
session: Optional["AgentSession"] = getattr(self, "_pending_session", None)
if session is None:
return
try:
if output.usage: vectara-agentic python 提示词模板化:GENERAL_PROMPT_TEMPLATE / REACT_PROMPT_TEMPLATE(prompts.py:134,150)由 format_prompt 注入 topic/date/general+custom 指令(factory.py:32)。get_general_instructions 按是否含 DB 工具动态拼接指令(prompts.py:111);强约束“仅基于工具输出、内联引用”。Gemini 工具需 sanitize_tools_for_gemini(agent.py:153)
提示词模板化:GENERAL_PROMPT_TEMPLATE / REACT_PROMPT_TEMPLATE(prompts.py:134,150)由 format_prompt 注入 topic/date/general+custom 指令(factory.py:32)。get_general_instructions 按是否含 DB 工具动态拼接指令(prompts.py:111);强约束“仅基于工具输出、内联引用”。Gemini 工具需 sanitize_tools_for_gemini(agent.py:153)
prompts.py:35factory.py:32prompts.py:111
# Base instructions (without database-specific content)
_BASE_INSTRUCTIONS = """
- Use tools as your main source of information.
- Do not respond based on your internal knowledge. Your response should be strictly grounded in the tool outputs or user messages.
Avoid adding any additional text that is not supported by the tool outputs.
- Use the 'get_bad_topics' (if it exists) tool to determine the topics you are not allowed to discuss or respond to.
- Before responding to a user query that requires knowledge of the current date, call the 'get_current_date' tool to get the current date.
Never rely on previous knowledge of the current date.
Example queries that require the current date: "What is the revenue of Apple last october?" or "What was the stock price 5 days ago?".
Never call 'get_current_date' more than once for the same user query.
- If you are asked about a period of time, make sure to interpret that relative to the current date. VoltAgent typescript system prompt(instructions 静态/动态)、conversation buffer、消息归一化(message-normalizer)、按 token 的上下文裁剪(contextLimit)与 apply-summarization 摘要旧消息;createPrompt 工具
system prompt(instructions 静态/动态)、conversation buffer、消息归一化(message-normalizer)、按 token 的上下文裁剪(contextLimit)与 apply-summarization 摘要旧消息;createPrompt 工具
agent/agent.ts:5166 * Get system message with dynamic instructions and retriever context
*/
// biome-ignore lint/complexity/noExcessiveCognitiveComplexity: legacy system message assembly
private async getSystemMessage(
input: string | UIMessage[] | BaseMessage[],
oc: OperationContext,
options?: BaseGenerationOptions,
runtimeToolkits: Toolkit[] = [],
): Promise<BaseMessage | BaseMessage[]> {
const resolvedMemory = this.resolveMemoryRuntimeOptions(options, oc);
const workingMemoryConversationId = oc.conversationId ?? resolvedMemory.conversationId;
const workingMemoryUserId = oc.userId ?? resolvedMemory.userId;