AgentScope 核心机制解析

AgentScope 核心机制解析

大模型会回答问题,但真正的 Agent 要解决的,是"把任务持续做下去"。

它需要记住历史、控制上下文长度、决定何时调用工具、在异常时重试或中断,还要允许开发者在关键节点插入自己的逻辑。

从这个角度看,AgentScope 的价值不在于"再包一层模型调用",而在于把 Agent 的运行机制工程化了。

一、框架定位

如果把大模型看成大脑,AgentScope 更像一套运行时系统。它把模型、记忆、工具、状态和执行流程组织起来,让一个 Agent 不只是"一问一答",而是能连续完成任务。
#mermaid-svg-mBNPL9CNr7h574A8{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;fill:#333;}@keyframes edge-animation-frame{from{stroke-dashoffset:0;}}@keyframes dash{to{stroke-dashoffset:0;}}#mermaid-svg-mBNPL9CNr7h574A8 .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#mermaid-svg-mBNPL9CNr7h574A8 .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#mermaid-svg-mBNPL9CNr7h574A8 .error-icon{fill:#552222;}#mermaid-svg-mBNPL9CNr7h574A8 .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-mBNPL9CNr7h574A8 .edge-thickness-normal{stroke-width:1px;}#mermaid-svg-mBNPL9CNr7h574A8 .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-mBNPL9CNr7h574A8 .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-mBNPL9CNr7h574A8 .edge-thickness-invisible{stroke-width:0;fill:none;}#mermaid-svg-mBNPL9CNr7h574A8 .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-mBNPL9CNr7h574A8 .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-mBNPL9CNr7h574A8 .marker{fill:#333333;stroke:#333333;}#mermaid-svg-mBNPL9CNr7h574A8 .marker.cross{stroke:#333333;}#mermaid-svg-mBNPL9CNr7h574A8 svg{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-mBNPL9CNr7h574A8 p{margin:0;}#mermaid-svg-mBNPL9CNr7h574A8 .label{font-family:"trebuchet ms",verdana,arial,sans-serif;color:#333;}#mermaid-svg-mBNPL9CNr7h574A8 .cluster-label text{fill:#333;}#mermaid-svg-mBNPL9CNr7h574A8 .cluster-label span{color:#333;}#mermaid-svg-mBNPL9CNr7h574A8 .cluster-label span p{background-color:transparent;}#mermaid-svg-mBNPL9CNr7h574A8 .label text,#mermaid-svg-mBNPL9CNr7h574A8 span{fill:#333;color:#333;}#mermaid-svg-mBNPL9CNr7h574A8 .node rect,#mermaid-svg-mBNPL9CNr7h574A8 .node circle,#mermaid-svg-mBNPL9CNr7h574A8 .node ellipse,#mermaid-svg-mBNPL9CNr7h574A8 .node polygon,#mermaid-svg-mBNPL9CNr7h574A8 .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-mBNPL9CNr7h574A8 .rough-node .label text,#mermaid-svg-mBNPL9CNr7h574A8 .node .label text,#mermaid-svg-mBNPL9CNr7h574A8 .image-shape .label,#mermaid-svg-mBNPL9CNr7h574A8 .icon-shape .label{text-anchor:middle;}#mermaid-svg-mBNPL9CNr7h574A8 .node .katex path{fill:#000;stroke:#000;stroke-width:1px;}#mermaid-svg-mBNPL9CNr7h574A8 .rough-node .label,#mermaid-svg-mBNPL9CNr7h574A8 .node .label,#mermaid-svg-mBNPL9CNr7h574A8 .image-shape .label,#mermaid-svg-mBNPL9CNr7h574A8 .icon-shape .label{text-align:center;}#mermaid-svg-mBNPL9CNr7h574A8 .node.clickable{cursor:pointer;}#mermaid-svg-mBNPL9CNr7h574A8 .root .anchor path{fill:#333333!important;stroke-width:0;stroke:#333333;}#mermaid-svg-mBNPL9CNr7h574A8 .arrowheadPath{fill:#333333;}#mermaid-svg-mBNPL9CNr7h574A8 .edgePath .path{stroke:#333333;stroke-width:2.0px;}#mermaid-svg-mBNPL9CNr7h574A8 .flowchart-link{stroke:#333333;fill:none;}#mermaid-svg-mBNPL9CNr7h574A8 .edgeLabel{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-mBNPL9CNr7h574A8 .edgeLabel p{background-color:rgba(232,232,232, 0.8);}#mermaid-svg-mBNPL9CNr7h574A8 .edgeLabel rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-mBNPL9CNr7h574A8 .labelBkg{background-color:rgba(232, 232, 232, 0.5);}#mermaid-svg-mBNPL9CNr7h574A8 .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#mermaid-svg-mBNPL9CNr7h574A8 .cluster text{fill:#333;}#mermaid-svg-mBNPL9CNr7h574A8 .cluster span{color:#333;}#mermaid-svg-mBNPL9CNr7h574A8 div.mermaidTooltip{position:absolute;text-align:center;max-width:200px;padding:2px;font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:12px;background:hsl(80, 100%, 96.2745098039%);border:1px solid #aaaa33;border-radius:2px;pointer-events:none;z-index:100;}#mermaid-svg-mBNPL9CNr7h574A8 .flowchartTitleText{text-anchor:middle;font-size:18px;fill:#333;}#mermaid-svg-mBNPL9CNr7h574A8 rect.text{fill:none;stroke-width:0;}#mermaid-svg-mBNPL9CNr7h574A8 .icon-shape,#mermaid-svg-mBNPL9CNr7h574A8 .image-shape{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-mBNPL9CNr7h574A8 .icon-shape p,#mermaid-svg-mBNPL9CNr7h574A8 .image-shape p{background-color:rgba(232,232,232, 0.8);padding:2px;}#mermaid-svg-mBNPL9CNr7h574A8 .icon-shape .label rect,#mermaid-svg-mBNPL9CNr7h574A8 .image-shape .label rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-mBNPL9CNr7h574A8 .label-icon{display:inline-block;height:1em;overflow:visible;vertical-align:-0.125em;}#mermaid-svg-mBNPL9CNr7h574A8 .node .label-icon path{fill:currentColor;stroke:revert;stroke-width:revert;}#mermaid-svg-mBNPL9CNr7h574A8 :root{--mermaid-font-family:"trebuchet ms",verdana,arial,sans-serif;} 用户
Agent
Model
Memory
Toolkit / Tool
State / Session
结果

一句话概括:

普通 LLM 调用解决"说什么",AgentScope 解决"怎么持续把事情做完"。

二、与 Spring AI Alibaba 的区别

两者不是简单替代关系,更像是重心不同。

维度 AgentScope Spring AI Alibaba
核心定位 Agent 运行机制 Java 企业 AI 应用框架
主要生态 Python 为主,也有 AgentScope Java Spring / Java
强项 记忆、上下文、Hook、工具执行、状态管理 Graph 编排、Spring 集成、企业落地
适合场景 快速实验、Agent 机制研究、灵活编排 企业系统接入、服务治理、Java 主栈开发

还有一个容易忽略的点:它们并不对立。Spring AI Alibaba 已提供 spring-ai-alibaba-starter-agentscope,可以把 AgentScope Java 作为能力接入到 SAA Graph 中。

所以更准确的理解是:

  • AgentScope 更像 Agent 内核
  • Spring AI Alibaba 更像企业 Java 落地框架

可以用下面这张图快速理解两者关系:
#mermaid-svg-RMearhXGeJyw1VKi{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;fill:#333;}@keyframes edge-animation-frame{from{stroke-dashoffset:0;}}@keyframes dash{to{stroke-dashoffset:0;}}#mermaid-svg-RMearhXGeJyw1VKi .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#mermaid-svg-RMearhXGeJyw1VKi .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#mermaid-svg-RMearhXGeJyw1VKi .error-icon{fill:#552222;}#mermaid-svg-RMearhXGeJyw1VKi .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-RMearhXGeJyw1VKi .edge-thickness-normal{stroke-width:1px;}#mermaid-svg-RMearhXGeJyw1VKi .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-RMearhXGeJyw1VKi .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-RMearhXGeJyw1VKi .edge-thickness-invisible{stroke-width:0;fill:none;}#mermaid-svg-RMearhXGeJyw1VKi .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-RMearhXGeJyw1VKi .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-RMearhXGeJyw1VKi .marker{fill:#333333;stroke:#333333;}#mermaid-svg-RMearhXGeJyw1VKi .marker.cross{stroke:#333333;}#mermaid-svg-RMearhXGeJyw1VKi svg{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-RMearhXGeJyw1VKi p{margin:0;}#mermaid-svg-RMearhXGeJyw1VKi .label{font-family:"trebuchet ms",verdana,arial,sans-serif;color:#333;}#mermaid-svg-RMearhXGeJyw1VKi .cluster-label text{fill:#333;}#mermaid-svg-RMearhXGeJyw1VKi .cluster-label span{color:#333;}#mermaid-svg-RMearhXGeJyw1VKi .cluster-label span p{background-color:transparent;}#mermaid-svg-RMearhXGeJyw1VKi .label text,#mermaid-svg-RMearhXGeJyw1VKi span{fill:#333;color:#333;}#mermaid-svg-RMearhXGeJyw1VKi .node rect,#mermaid-svg-RMearhXGeJyw1VKi .node circle,#mermaid-svg-RMearhXGeJyw1VKi .node ellipse,#mermaid-svg-RMearhXGeJyw1VKi .node polygon,#mermaid-svg-RMearhXGeJyw1VKi .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-RMearhXGeJyw1VKi .rough-node .label text,#mermaid-svg-RMearhXGeJyw1VKi .node .label text,#mermaid-svg-RMearhXGeJyw1VKi .image-shape .label,#mermaid-svg-RMearhXGeJyw1VKi .icon-shape .label{text-anchor:middle;}#mermaid-svg-RMearhXGeJyw1VKi .node .katex path{fill:#000;stroke:#000;stroke-width:1px;}#mermaid-svg-RMearhXGeJyw1VKi .rough-node .label,#mermaid-svg-RMearhXGeJyw1VKi .node .label,#mermaid-svg-RMearhXGeJyw1VKi .image-shape .label,#mermaid-svg-RMearhXGeJyw1VKi .icon-shape .label{text-align:center;}#mermaid-svg-RMearhXGeJyw1VKi .node.clickable{cursor:pointer;}#mermaid-svg-RMearhXGeJyw1VKi .root .anchor path{fill:#333333!important;stroke-width:0;stroke:#333333;}#mermaid-svg-RMearhXGeJyw1VKi .arrowheadPath{fill:#333333;}#mermaid-svg-RMearhXGeJyw1VKi .edgePath .path{stroke:#333333;stroke-width:2.0px;}#mermaid-svg-RMearhXGeJyw1VKi .flowchart-link{stroke:#333333;fill:none;}#mermaid-svg-RMearhXGeJyw1VKi .edgeLabel{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-RMearhXGeJyw1VKi .edgeLabel p{background-color:rgba(232,232,232, 0.8);}#mermaid-svg-RMearhXGeJyw1VKi .edgeLabel rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-RMearhXGeJyw1VKi .labelBkg{background-color:rgba(232, 232, 232, 0.5);}#mermaid-svg-RMearhXGeJyw1VKi .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#mermaid-svg-RMearhXGeJyw1VKi .cluster text{fill:#333;}#mermaid-svg-RMearhXGeJyw1VKi .cluster span{color:#333;}#mermaid-svg-RMearhXGeJyw1VKi div.mermaidTooltip{position:absolute;text-align:center;max-width:200px;padding:2px;font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:12px;background:hsl(80, 100%, 96.2745098039%);border:1px solid #aaaa33;border-radius:2px;pointer-events:none;z-index:100;}#mermaid-svg-RMearhXGeJyw1VKi .flowchartTitleText{text-anchor:middle;font-size:18px;fill:#333;}#mermaid-svg-RMearhXGeJyw1VKi rect.text{fill:none;stroke-width:0;}#mermaid-svg-RMearhXGeJyw1VKi .icon-shape,#mermaid-svg-RMearhXGeJyw1VKi .image-shape{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-RMearhXGeJyw1VKi .icon-shape p,#mermaid-svg-RMearhXGeJyw1VKi .image-shape p{background-color:rgba(232,232,232, 0.8);padding:2px;}#mermaid-svg-RMearhXGeJyw1VKi .icon-shape .label rect,#mermaid-svg-RMearhXGeJyw1VKi .image-shape .label rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-RMearhXGeJyw1VKi .label-icon{display:inline-block;height:1em;overflow:visible;vertical-align:-0.125em;}#mermaid-svg-RMearhXGeJyw1VKi .node .label-icon path{fill:currentColor;stroke:revert;stroke-width:revert;}#mermaid-svg-RMearhXGeJyw1VKi :root{--mermaid-font-family:"trebuchet ms",verdana,arial,sans-serif;} AgentScope
记忆
上下文压缩
Hook / Middleware
执行控制
Spring AI Alibaba
Spring 集成
Graph 编排
企业应用接入
平台化治理

三、核心架构

AgentScope 的核心对象并不复杂,但组合方式很关键。

组件 作用 通俗理解
Agent 执行主体 负责思考、决策、行动
Model 推理生成 大脑
Memory 保存消息与标记 笔记本
Toolkit 工具接入与调用 手和脚
StateModule 保存运行态状态 当前工作现场
Session 保存和恢复状态 存档点

它的设计思想可以浓缩成一句话:

消息流动是血液,状态管理是骨架,工具是执行器,模型只负责推理。

对不写代码的人来说,可以把它理解成一个"会记笔记、会查工具、还能中途保存进度的智能助手"。

四、记忆存储

AgentScope 的 Memory 负责两件事:

  • 存储消息
  • 给消息打 mark

这个 mark 很重要。因为 Agent 并不是简单保存全部历史,而是要知道"哪些内容属于任务信息、哪些属于工具结果、哪些属于提示信息"。

python 复制代码
memory.add(msg, marks=["task"])
memory.add(tool_result, marks=["tool_result"])
memory.add(hint_msg, marks=["hint"])

这意味着记忆不是一串死历史,而是一组可分类、可筛选、可压缩的上下文对象。
#mermaid-svg-vVjU1GWPhtovMgLT{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;fill:#333;}@keyframes edge-animation-frame{from{stroke-dashoffset:0;}}@keyframes dash{to{stroke-dashoffset:0;}}#mermaid-svg-vVjU1GWPhtovMgLT .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#mermaid-svg-vVjU1GWPhtovMgLT .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#mermaid-svg-vVjU1GWPhtovMgLT .error-icon{fill:#552222;}#mermaid-svg-vVjU1GWPhtovMgLT .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-vVjU1GWPhtovMgLT .edge-thickness-normal{stroke-width:1px;}#mermaid-svg-vVjU1GWPhtovMgLT .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-vVjU1GWPhtovMgLT .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-vVjU1GWPhtovMgLT .edge-thickness-invisible{stroke-width:0;fill:none;}#mermaid-svg-vVjU1GWPhtovMgLT .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-vVjU1GWPhtovMgLT .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-vVjU1GWPhtovMgLT .marker{fill:#333333;stroke:#333333;}#mermaid-svg-vVjU1GWPhtovMgLT .marker.cross{stroke:#333333;}#mermaid-svg-vVjU1GWPhtovMgLT svg{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-vVjU1GWPhtovMgLT p{margin:0;}#mermaid-svg-vVjU1GWPhtovMgLT .label{font-family:"trebuchet ms",verdana,arial,sans-serif;color:#333;}#mermaid-svg-vVjU1GWPhtovMgLT .cluster-label text{fill:#333;}#mermaid-svg-vVjU1GWPhtovMgLT .cluster-label span{color:#333;}#mermaid-svg-vVjU1GWPhtovMgLT .cluster-label span p{background-color:transparent;}#mermaid-svg-vVjU1GWPhtovMgLT .label text,#mermaid-svg-vVjU1GWPhtovMgLT span{fill:#333;color:#333;}#mermaid-svg-vVjU1GWPhtovMgLT .node rect,#mermaid-svg-vVjU1GWPhtovMgLT .node circle,#mermaid-svg-vVjU1GWPhtovMgLT .node ellipse,#mermaid-svg-vVjU1GWPhtovMgLT .node polygon,#mermaid-svg-vVjU1GWPhtovMgLT .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-vVjU1GWPhtovMgLT .rough-node .label text,#mermaid-svg-vVjU1GWPhtovMgLT .node .label text,#mermaid-svg-vVjU1GWPhtovMgLT .image-shape .label,#mermaid-svg-vVjU1GWPhtovMgLT .icon-shape .label{text-anchor:middle;}#mermaid-svg-vVjU1GWPhtovMgLT .node .katex path{fill:#000;stroke:#000;stroke-width:1px;}#mermaid-svg-vVjU1GWPhtovMgLT .rough-node .label,#mermaid-svg-vVjU1GWPhtovMgLT .node .label,#mermaid-svg-vVjU1GWPhtovMgLT .image-shape .label,#mermaid-svg-vVjU1GWPhtovMgLT .icon-shape .label{text-align:center;}#mermaid-svg-vVjU1GWPhtovMgLT .node.clickable{cursor:pointer;}#mermaid-svg-vVjU1GWPhtovMgLT .root .anchor path{fill:#333333!important;stroke-width:0;stroke:#333333;}#mermaid-svg-vVjU1GWPhtovMgLT .arrowheadPath{fill:#333333;}#mermaid-svg-vVjU1GWPhtovMgLT .edgePath .path{stroke:#333333;stroke-width:2.0px;}#mermaid-svg-vVjU1GWPhtovMgLT .flowchart-link{stroke:#333333;fill:none;}#mermaid-svg-vVjU1GWPhtovMgLT .edgeLabel{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-vVjU1GWPhtovMgLT .edgeLabel p{background-color:rgba(232,232,232, 0.8);}#mermaid-svg-vVjU1GWPhtovMgLT .edgeLabel rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-vVjU1GWPhtovMgLT .labelBkg{background-color:rgba(232, 232, 232, 0.5);}#mermaid-svg-vVjU1GWPhtovMgLT .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#mermaid-svg-vVjU1GWPhtovMgLT .cluster text{fill:#333;}#mermaid-svg-vVjU1GWPhtovMgLT .cluster span{color:#333;}#mermaid-svg-vVjU1GWPhtovMgLT div.mermaidTooltip{position:absolute;text-align:center;max-width:200px;padding:2px;font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:12px;background:hsl(80, 100%, 96.2745098039%);border:1px solid #aaaa33;border-radius:2px;pointer-events:none;z-index:100;}#mermaid-svg-vVjU1GWPhtovMgLT .flowchartTitleText{text-anchor:middle;font-size:18px;fill:#333;}#mermaid-svg-vVjU1GWPhtovMgLT rect.text{fill:none;stroke-width:0;}#mermaid-svg-vVjU1GWPhtovMgLT .icon-shape,#mermaid-svg-vVjU1GWPhtovMgLT .image-shape{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-vVjU1GWPhtovMgLT .icon-shape p,#mermaid-svg-vVjU1GWPhtovMgLT .image-shape p{background-color:rgba(232,232,232, 0.8);padding:2px;}#mermaid-svg-vVjU1GWPhtovMgLT .icon-shape .label rect,#mermaid-svg-vVjU1GWPhtovMgLT .image-shape .label rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-vVjU1GWPhtovMgLT .label-icon{display:inline-block;height:1em;overflow:visible;vertical-align:-0.125em;}#mermaid-svg-vVjU1GWPhtovMgLT .node .label-icon path{fill:currentColor;stroke:revert;stroke-width:revert;}#mermaid-svg-vVjU1GWPhtovMgLT :root{--mermaid-font-family:"trebuchet ms",verdana,arial,sans-serif;} 用户输入
写入 Memory
工具返回
打 marks
后续读取
参与下一轮推理

官方文档里有一个很重要的边界:Memory 负责存储与管理,压缩逻辑主要在 Agent 层实现。

这其实很合理。存储层尽量稳定简单,复杂的"保留什么、压缩什么、何时压缩",交给 Agent 来决定。

五、上下文管理与六层压缩链路

这一部分是 AgentScope 最有工程味的地方。

长对话一定会遇到两个问题:

  • 上下文窗口会爆
  • token 成本和延迟会持续上升

最粗暴的做法是截断历史,但这样很容易丢掉真正重要的信息。AgentScope 的做法更系统。

如果只看 Python ReActAgent,你会看到"记忆压缩";如果把 AgentScope Java 2.0 的 Harness 文档也放进来,就会发现它已经把上下文管理做成了一条完整链路。

为了方便理解,可以把它概括成一个更适合博客表达的"六层上下文管理机制"。这里的"六层"是基于官方 compactionmemoryagent 文档做的工程化归纳,不是官方单独命名的一条固定术语。

1. 预压缩参数截断

先对超长工具入参做字符串级截断,比如 write_fileedit_file 这类很长但后续通常不会再看的内容。

这一步不需要 LLM,成本最低。

2. 对话摘要压缩

当消息条数或 token 超过阈值时,把对话前缀压缩成结构化摘要,只保留最近几条原文消息。

3. 大工具结果卸载

如果某个工具结果太大,不直接塞进上下文,而是落盘保存;上下文里只保留摘要、首尾片段和可回读路径。

4. 压缩前写入长期记忆

真正压缩之前,先把对话中的事实信息抽取进长期记忆,避免"摘要后信息永久丢失"。

5. 压缩前写入原始会话日志

原始消息会被写入不参与压缩的日志文件,后续可以通过会话搜索或调试工具回查。

6. 溢出兜底重试

如果模型已经报 context_length_exceeded,框架会触发一次强制压缩,然后自动重试一次。

这六层并不是六个彼此独立的花哨功能,而是一条从"预防"到"兜底"的上下文治理链路。
#mermaid-svg-qKcabqfR0lePwpii{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;fill:#333;}@keyframes edge-animation-frame{from{stroke-dashoffset:0;}}@keyframes dash{to{stroke-dashoffset:0;}}#mermaid-svg-qKcabqfR0lePwpii .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#mermaid-svg-qKcabqfR0lePwpii .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#mermaid-svg-qKcabqfR0lePwpii .error-icon{fill:#552222;}#mermaid-svg-qKcabqfR0lePwpii .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-qKcabqfR0lePwpii .edge-thickness-normal{stroke-width:1px;}#mermaid-svg-qKcabqfR0lePwpii .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-qKcabqfR0lePwpii .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-qKcabqfR0lePwpii .edge-thickness-invisible{stroke-width:0;fill:none;}#mermaid-svg-qKcabqfR0lePwpii .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-qKcabqfR0lePwpii .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-qKcabqfR0lePwpii .marker{fill:#333333;stroke:#333333;}#mermaid-svg-qKcabqfR0lePwpii .marker.cross{stroke:#333333;}#mermaid-svg-qKcabqfR0lePwpii svg{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-qKcabqfR0lePwpii p{margin:0;}#mermaid-svg-qKcabqfR0lePwpii .label{font-family:"trebuchet ms",verdana,arial,sans-serif;color:#333;}#mermaid-svg-qKcabqfR0lePwpii .cluster-label text{fill:#333;}#mermaid-svg-qKcabqfR0lePwpii .cluster-label span{color:#333;}#mermaid-svg-qKcabqfR0lePwpii .cluster-label span p{background-color:transparent;}#mermaid-svg-qKcabqfR0lePwpii .label text,#mermaid-svg-qKcabqfR0lePwpii span{fill:#333;color:#333;}#mermaid-svg-qKcabqfR0lePwpii .node rect,#mermaid-svg-qKcabqfR0lePwpii .node circle,#mermaid-svg-qKcabqfR0lePwpii .node ellipse,#mermaid-svg-qKcabqfR0lePwpii .node polygon,#mermaid-svg-qKcabqfR0lePwpii .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-qKcabqfR0lePwpii .rough-node .label text,#mermaid-svg-qKcabqfR0lePwpii .node .label text,#mermaid-svg-qKcabqfR0lePwpii .image-shape .label,#mermaid-svg-qKcabqfR0lePwpii .icon-shape .label{text-anchor:middle;}#mermaid-svg-qKcabqfR0lePwpii .node .katex path{fill:#000;stroke:#000;stroke-width:1px;}#mermaid-svg-qKcabqfR0lePwpii .rough-node .label,#mermaid-svg-qKcabqfR0lePwpii .node .label,#mermaid-svg-qKcabqfR0lePwpii .image-shape .label,#mermaid-svg-qKcabqfR0lePwpii .icon-shape .label{text-align:center;}#mermaid-svg-qKcabqfR0lePwpii .node.clickable{cursor:pointer;}#mermaid-svg-qKcabqfR0lePwpii .root .anchor path{fill:#333333!important;stroke-width:0;stroke:#333333;}#mermaid-svg-qKcabqfR0lePwpii .arrowheadPath{fill:#333333;}#mermaid-svg-qKcabqfR0lePwpii .edgePath .path{stroke:#333333;stroke-width:2.0px;}#mermaid-svg-qKcabqfR0lePwpii .flowchart-link{stroke:#333333;fill:none;}#mermaid-svg-qKcabqfR0lePwpii .edgeLabel{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-qKcabqfR0lePwpii .edgeLabel p{background-color:rgba(232,232,232, 0.8);}#mermaid-svg-qKcabqfR0lePwpii .edgeLabel rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-qKcabqfR0lePwpii .labelBkg{background-color:rgba(232, 232, 232, 0.5);}#mermaid-svg-qKcabqfR0lePwpii .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#mermaid-svg-qKcabqfR0lePwpii .cluster text{fill:#333;}#mermaid-svg-qKcabqfR0lePwpii .cluster span{color:#333;}#mermaid-svg-qKcabqfR0lePwpii div.mermaidTooltip{position:absolute;text-align:center;max-width:200px;padding:2px;font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:12px;background:hsl(80, 100%, 96.2745098039%);border:1px solid #aaaa33;border-radius:2px;pointer-events:none;z-index:100;}#mermaid-svg-qKcabqfR0lePwpii .flowchartTitleText{text-anchor:middle;font-size:18px;fill:#333;}#mermaid-svg-qKcabqfR0lePwpii rect.text{fill:none;stroke-width:0;}#mermaid-svg-qKcabqfR0lePwpii .icon-shape,#mermaid-svg-qKcabqfR0lePwpii .image-shape{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-qKcabqfR0lePwpii .icon-shape p,#mermaid-svg-qKcabqfR0lePwpii .image-shape p{background-color:rgba(232,232,232, 0.8);padding:2px;}#mermaid-svg-qKcabqfR0lePwpii .icon-shape .label rect,#mermaid-svg-qKcabqfR0lePwpii .image-shape .label rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-qKcabqfR0lePwpii .label-icon{display:inline-block;height:1em;overflow:visible;vertical-align:-0.125em;}#mermaid-svg-qKcabqfR0lePwpii .node .label-icon path{fill:currentColor;stroke:revert;stroke-width:revert;}#mermaid-svg-qKcabqfR0lePwpii :root{--mermaid-font-family:"trebuchet ms",verdana,arial,sans-serif;} 未超阈值
超阈值
上下文溢出
新消息进入
预压缩参数截断
检查消息数 / Token
直接进入推理
压缩前 flush 到长期记忆
压缩前 offload 原始日志
对话摘要压缩
大工具结果卸载
模型调用
强制压缩并重试一次

如果你更偏 Python 视角,也可以这样理解它的核心压缩策略:

python 复制代码
from agentscope.agent import ReActAgent
from agentscope.token import CharTokenCounter

agent = ReActAgent(
    name="assistant",
    sys_prompt="You are a helpful assistant.",
    model=model,
    formatter=formatter,
    memory=memory,
    compression_config=ReActAgent.CompressionConfig(
        enable=True,
        agent_token_counter=CharTokenCounter(),
        trigger_threshold=10000,
        keep_recent=3,
    ),
)

这里最关键的不是"压缩"本身,而是它压缩后仍然保留任务连续性。

在官方设计里,压缩后的摘要会继续服务后续推理,而不是单纯为了缩短文本。

下面这张时序图更容易看懂它是怎么工作的:
Memory 压缩模型 Token Counter ReActAgent 用户 Memory 压缩模型 Token Counter ReActAgent 用户 #mermaid-svg-hLXNDHQkHk7oia3Y{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;fill:#333;}@keyframes edge-animation-frame{from{stroke-dashoffset:0;}}@keyframes dash{to{stroke-dashoffset:0;}}#mermaid-svg-hLXNDHQkHk7oia3Y .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#mermaid-svg-hLXNDHQkHk7oia3Y .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#mermaid-svg-hLXNDHQkHk7oia3Y .error-icon{fill:#552222;}#mermaid-svg-hLXNDHQkHk7oia3Y .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-hLXNDHQkHk7oia3Y .edge-thickness-normal{stroke-width:1px;}#mermaid-svg-hLXNDHQkHk7oia3Y .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-hLXNDHQkHk7oia3Y .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-hLXNDHQkHk7oia3Y .edge-thickness-invisible{stroke-width:0;fill:none;}#mermaid-svg-hLXNDHQkHk7oia3Y .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-hLXNDHQkHk7oia3Y .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-hLXNDHQkHk7oia3Y .marker{fill:#333333;stroke:#333333;}#mermaid-svg-hLXNDHQkHk7oia3Y .marker.cross{stroke:#333333;}#mermaid-svg-hLXNDHQkHk7oia3Y svg{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-hLXNDHQkHk7oia3Y p{margin:0;}#mermaid-svg-hLXNDHQkHk7oia3Y .actor{stroke:hsl(259.6261682243, 59.7765363128%, 87.9019607843%);fill:#ECECFF;}#mermaid-svg-hLXNDHQkHk7oia3Y text.actor>tspan{fill:black;stroke:none;}#mermaid-svg-hLXNDHQkHk7oia3Y .actor-line{stroke:hsl(259.6261682243, 59.7765363128%, 87.9019607843%);}#mermaid-svg-hLXNDHQkHk7oia3Y .innerArc{stroke-width:1.5;stroke-dasharray:none;}#mermaid-svg-hLXNDHQkHk7oia3Y .messageLine0{stroke-width:1.5;stroke-dasharray:none;stroke:#333;}#mermaid-svg-hLXNDHQkHk7oia3Y .messageLine1{stroke-width:1.5;stroke-dasharray:2,2;stroke:#333;}#mermaid-svg-hLXNDHQkHk7oia3Y #arrowhead path{fill:#333;stroke:#333;}#mermaid-svg-hLXNDHQkHk7oia3Y .sequenceNumber{fill:white;}#mermaid-svg-hLXNDHQkHk7oia3Y #sequencenumber{fill:#333;}#mermaid-svg-hLXNDHQkHk7oia3Y #crosshead path{fill:#333;stroke:#333;}#mermaid-svg-hLXNDHQkHk7oia3Y .messageText{fill:#333;stroke:none;}#mermaid-svg-hLXNDHQkHk7oia3Y .labelBox{stroke:hsl(259.6261682243, 59.7765363128%, 87.9019607843%);fill:#ECECFF;}#mermaid-svg-hLXNDHQkHk7oia3Y .labelText,#mermaid-svg-hLXNDHQkHk7oia3Y .labelText>tspan{fill:black;stroke:none;}#mermaid-svg-hLXNDHQkHk7oia3Y .loopText,#mermaid-svg-hLXNDHQkHk7oia3Y .loopText>tspan{fill:black;stroke:none;}#mermaid-svg-hLXNDHQkHk7oia3Y .loopLine{stroke-width:2px;stroke-dasharray:2,2;stroke:hsl(259.6261682243, 59.7765363128%, 87.9019607843%);fill:hsl(259.6261682243, 59.7765363128%, 87.9019607843%);}#mermaid-svg-hLXNDHQkHk7oia3Y .note{stroke:#aaaa33;fill:#fff5ad;}#mermaid-svg-hLXNDHQkHk7oia3Y .noteText,#mermaid-svg-hLXNDHQkHk7oia3Y .noteText>tspan{fill:black;stroke:none;}#mermaid-svg-hLXNDHQkHk7oia3Y .activation0{fill:#f4f4f4;stroke:#666;}#mermaid-svg-hLXNDHQkHk7oia3Y .activation1{fill:#f4f4f4;stroke:#666;}#mermaid-svg-hLXNDHQkHk7oia3Y .activation2{fill:#f4f4f4;stroke:#666;}#mermaid-svg-hLXNDHQkHk7oia3Y .actorPopupMenu{position:absolute;}#mermaid-svg-hLXNDHQkHk7oia3Y .actorPopupMenuPanel{position:absolute;fill:#ECECFF;box-shadow:0px 8px 16px 0px rgba(0,0,0,0.2);filter:drop-shadow(3px 5px 2px rgb(0 0 0 / 0.4));}#mermaid-svg-hLXNDHQkHk7oia3Y .actor-man line{stroke:hsl(259.6261682243, 59.7765363128%, 87.9019607843%);fill:#ECECFF;}#mermaid-svg-hLXNDHQkHk7oia3Y .actor-man circle,#mermaid-svg-hLXNDHQkHk7oia3Y line{stroke:hsl(259.6261682243, 59.7765363128%, 87.9019607843%);fill:#ECECFF;stroke-width:2px;}#mermaid-svg-hLXNDHQkHk7oia3Y :root{--mermaid-font-family:"trebuchet ms",verdana,arial,sans-serif;} 新消息 读取记忆 统计 token 超过阈值 发送旧消息生成摘要 返回摘要 旧消息标记为 COMPRESSED 保存 compressed summary 保留最近 keep_recent 条消息 基于"摘要 + 最近上下文"继续回复

一句更生活化的话来总结就是:

这不是"删聊天记录",而是"把旧过程整理成还能继续工作的会议纪要"。

六、Hook 机制

Hook 可以理解成"在关键动作前后插入自定义逻辑"。

AgentScope 提供了比较完整的生命周期扩展点:

  • pre_reply / post_reply
  • pre_observe / post_observe
  • pre_print / post_print
  • pre_reasoning / post_reasoning
  • pre_acting / post_acting

#mermaid-svg-9D3rY2pptZnLa3OQ{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;fill:#333;}@keyframes edge-animation-frame{from{stroke-dashoffset:0;}}@keyframes dash{to{stroke-dashoffset:0;}}#mermaid-svg-9D3rY2pptZnLa3OQ .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#mermaid-svg-9D3rY2pptZnLa3OQ .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#mermaid-svg-9D3rY2pptZnLa3OQ .error-icon{fill:#552222;}#mermaid-svg-9D3rY2pptZnLa3OQ .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-9D3rY2pptZnLa3OQ .edge-thickness-normal{stroke-width:1px;}#mermaid-svg-9D3rY2pptZnLa3OQ .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-9D3rY2pptZnLa3OQ .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-9D3rY2pptZnLa3OQ .edge-thickness-invisible{stroke-width:0;fill:none;}#mermaid-svg-9D3rY2pptZnLa3OQ .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-9D3rY2pptZnLa3OQ .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-9D3rY2pptZnLa3OQ .marker{fill:#333333;stroke:#333333;}#mermaid-svg-9D3rY2pptZnLa3OQ .marker.cross{stroke:#333333;}#mermaid-svg-9D3rY2pptZnLa3OQ svg{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-9D3rY2pptZnLa3OQ p{margin:0;}#mermaid-svg-9D3rY2pptZnLa3OQ .label{font-family:"trebuchet ms",verdana,arial,sans-serif;color:#333;}#mermaid-svg-9D3rY2pptZnLa3OQ .cluster-label text{fill:#333;}#mermaid-svg-9D3rY2pptZnLa3OQ .cluster-label span{color:#333;}#mermaid-svg-9D3rY2pptZnLa3OQ .cluster-label span p{background-color:transparent;}#mermaid-svg-9D3rY2pptZnLa3OQ .label text,#mermaid-svg-9D3rY2pptZnLa3OQ span{fill:#333;color:#333;}#mermaid-svg-9D3rY2pptZnLa3OQ .node rect,#mermaid-svg-9D3rY2pptZnLa3OQ .node circle,#mermaid-svg-9D3rY2pptZnLa3OQ .node ellipse,#mermaid-svg-9D3rY2pptZnLa3OQ .node polygon,#mermaid-svg-9D3rY2pptZnLa3OQ .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-9D3rY2pptZnLa3OQ .rough-node .label text,#mermaid-svg-9D3rY2pptZnLa3OQ .node .label text,#mermaid-svg-9D3rY2pptZnLa3OQ .image-shape .label,#mermaid-svg-9D3rY2pptZnLa3OQ .icon-shape .label{text-anchor:middle;}#mermaid-svg-9D3rY2pptZnLa3OQ .node .katex path{fill:#000;stroke:#000;stroke-width:1px;}#mermaid-svg-9D3rY2pptZnLa3OQ .rough-node .label,#mermaid-svg-9D3rY2pptZnLa3OQ .node .label,#mermaid-svg-9D3rY2pptZnLa3OQ .image-shape .label,#mermaid-svg-9D3rY2pptZnLa3OQ .icon-shape .label{text-align:center;}#mermaid-svg-9D3rY2pptZnLa3OQ .node.clickable{cursor:pointer;}#mermaid-svg-9D3rY2pptZnLa3OQ .root .anchor path{fill:#333333!important;stroke-width:0;stroke:#333333;}#mermaid-svg-9D3rY2pptZnLa3OQ .arrowheadPath{fill:#333333;}#mermaid-svg-9D3rY2pptZnLa3OQ .edgePath .path{stroke:#333333;stroke-width:2.0px;}#mermaid-svg-9D3rY2pptZnLa3OQ .flowchart-link{stroke:#333333;fill:none;}#mermaid-svg-9D3rY2pptZnLa3OQ .edgeLabel{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-9D3rY2pptZnLa3OQ .edgeLabel p{background-color:rgba(232,232,232, 0.8);}#mermaid-svg-9D3rY2pptZnLa3OQ .edgeLabel rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-9D3rY2pptZnLa3OQ .labelBkg{background-color:rgba(232, 232, 232, 0.5);}#mermaid-svg-9D3rY2pptZnLa3OQ .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#mermaid-svg-9D3rY2pptZnLa3OQ .cluster text{fill:#333;}#mermaid-svg-9D3rY2pptZnLa3OQ .cluster span{color:#333;}#mermaid-svg-9D3rY2pptZnLa3OQ div.mermaidTooltip{position:absolute;text-align:center;max-width:200px;padding:2px;font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:12px;background:hsl(80, 100%, 96.2745098039%);border:1px solid #aaaa33;border-radius:2px;pointer-events:none;z-index:100;}#mermaid-svg-9D3rY2pptZnLa3OQ .flowchartTitleText{text-anchor:middle;font-size:18px;fill:#333;}#mermaid-svg-9D3rY2pptZnLa3OQ rect.text{fill:none;stroke-width:0;}#mermaid-svg-9D3rY2pptZnLa3OQ .icon-shape,#mermaid-svg-9D3rY2pptZnLa3OQ .image-shape{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-9D3rY2pptZnLa3OQ .icon-shape p,#mermaid-svg-9D3rY2pptZnLa3OQ .image-shape p{background-color:rgba(232,232,232, 0.8);padding:2px;}#mermaid-svg-9D3rY2pptZnLa3OQ .icon-shape .label rect,#mermaid-svg-9D3rY2pptZnLa3OQ .image-shape .label rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-9D3rY2pptZnLa3OQ .label-icon{display:inline-block;height:1em;overflow:visible;vertical-align:-0.125em;}#mermaid-svg-9D3rY2pptZnLa3OQ .node .label-icon path{fill:currentColor;stroke:revert;stroke-width:revert;}#mermaid-svg-9D3rY2pptZnLa3OQ :root{--mermaid-font-family:"trebuchet ms",verdana,arial,sans-serif;} observe
reasoning
acting
reply
pre/post observe
pre/post reasoning
pre/post acting
pre/post reply

它的价值主要有四类:

  • 日志与链路追踪
  • 安全审计
  • 输入输出改写
  • 耗时和调用统计

一个最小示例:

python 复制代码
def log_pre_reasoning(self, kwargs):
    print("start reasoning")
    return kwargs

def log_post_reasoning(self, kwargs, output):
    print("reasoning done")
    return output

agent.register_instance_hook(
    hook_type="pre_reasoning",
    hook_name="log_pre_reasoning",
    hook=log_pre_reasoning,
)

七、工具调用、中间件与执行控制

Hook 更偏 Agent 生命周期扩展点,Middleware 更偏 Tool 执行链扩展点。

AgentScope 的 Toolkit 中间件采用典型的洋葱模型:
#mermaid-svg-Bn4tomyeFgvCYjWP{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;fill:#333;}@keyframes edge-animation-frame{from{stroke-dashoffset:0;}}@keyframes dash{to{stroke-dashoffset:0;}}#mermaid-svg-Bn4tomyeFgvCYjWP .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#mermaid-svg-Bn4tomyeFgvCYjWP .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#mermaid-svg-Bn4tomyeFgvCYjWP .error-icon{fill:#552222;}#mermaid-svg-Bn4tomyeFgvCYjWP .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-Bn4tomyeFgvCYjWP .edge-thickness-normal{stroke-width:1px;}#mermaid-svg-Bn4tomyeFgvCYjWP .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-Bn4tomyeFgvCYjWP .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-Bn4tomyeFgvCYjWP .edge-thickness-invisible{stroke-width:0;fill:none;}#mermaid-svg-Bn4tomyeFgvCYjWP .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-Bn4tomyeFgvCYjWP .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-Bn4tomyeFgvCYjWP .marker{fill:#333333;stroke:#333333;}#mermaid-svg-Bn4tomyeFgvCYjWP .marker.cross{stroke:#333333;}#mermaid-svg-Bn4tomyeFgvCYjWP svg{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-Bn4tomyeFgvCYjWP p{margin:0;}#mermaid-svg-Bn4tomyeFgvCYjWP .label{font-family:"trebuchet ms",verdana,arial,sans-serif;color:#333;}#mermaid-svg-Bn4tomyeFgvCYjWP .cluster-label text{fill:#333;}#mermaid-svg-Bn4tomyeFgvCYjWP .cluster-label span{color:#333;}#mermaid-svg-Bn4tomyeFgvCYjWP .cluster-label span p{background-color:transparent;}#mermaid-svg-Bn4tomyeFgvCYjWP .label text,#mermaid-svg-Bn4tomyeFgvCYjWP span{fill:#333;color:#333;}#mermaid-svg-Bn4tomyeFgvCYjWP .node rect,#mermaid-svg-Bn4tomyeFgvCYjWP .node circle,#mermaid-svg-Bn4tomyeFgvCYjWP .node ellipse,#mermaid-svg-Bn4tomyeFgvCYjWP .node polygon,#mermaid-svg-Bn4tomyeFgvCYjWP .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-Bn4tomyeFgvCYjWP .rough-node .label text,#mermaid-svg-Bn4tomyeFgvCYjWP .node .label text,#mermaid-svg-Bn4tomyeFgvCYjWP .image-shape .label,#mermaid-svg-Bn4tomyeFgvCYjWP .icon-shape .label{text-anchor:middle;}#mermaid-svg-Bn4tomyeFgvCYjWP .node .katex path{fill:#000;stroke:#000;stroke-width:1px;}#mermaid-svg-Bn4tomyeFgvCYjWP .rough-node .label,#mermaid-svg-Bn4tomyeFgvCYjWP .node .label,#mermaid-svg-Bn4tomyeFgvCYjWP .image-shape .label,#mermaid-svg-Bn4tomyeFgvCYjWP .icon-shape .label{text-align:center;}#mermaid-svg-Bn4tomyeFgvCYjWP .node.clickable{cursor:pointer;}#mermaid-svg-Bn4tomyeFgvCYjWP .root .anchor path{fill:#333333!important;stroke-width:0;stroke:#333333;}#mermaid-svg-Bn4tomyeFgvCYjWP .arrowheadPath{fill:#333333;}#mermaid-svg-Bn4tomyeFgvCYjWP .edgePath .path{stroke:#333333;stroke-width:2.0px;}#mermaid-svg-Bn4tomyeFgvCYjWP .flowchart-link{stroke:#333333;fill:none;}#mermaid-svg-Bn4tomyeFgvCYjWP .edgeLabel{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-Bn4tomyeFgvCYjWP .edgeLabel p{background-color:rgba(232,232,232, 0.8);}#mermaid-svg-Bn4tomyeFgvCYjWP .edgeLabel rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-Bn4tomyeFgvCYjWP .labelBkg{background-color:rgba(232, 232, 232, 0.5);}#mermaid-svg-Bn4tomyeFgvCYjWP .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#mermaid-svg-Bn4tomyeFgvCYjWP .cluster text{fill:#333;}#mermaid-svg-Bn4tomyeFgvCYjWP .cluster span{color:#333;}#mermaid-svg-Bn4tomyeFgvCYjWP div.mermaidTooltip{position:absolute;text-align:center;max-width:200px;padding:2px;font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:12px;background:hsl(80, 100%, 96.2745098039%);border:1px solid #aaaa33;border-radius:2px;pointer-events:none;z-index:100;}#mermaid-svg-Bn4tomyeFgvCYjWP .flowchartTitleText{text-anchor:middle;font-size:18px;fill:#333;}#mermaid-svg-Bn4tomyeFgvCYjWP rect.text{fill:none;stroke-width:0;}#mermaid-svg-Bn4tomyeFgvCYjWP .icon-shape,#mermaid-svg-Bn4tomyeFgvCYjWP .image-shape{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-Bn4tomyeFgvCYjWP .icon-shape p,#mermaid-svg-Bn4tomyeFgvCYjWP .image-shape p{background-color:rgba(232,232,232, 0.8);padding:2px;}#mermaid-svg-Bn4tomyeFgvCYjWP .icon-shape .label rect,#mermaid-svg-Bn4tomyeFgvCYjWP .image-shape .label rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-Bn4tomyeFgvCYjWP .label-icon{display:inline-block;height:1em;overflow:visible;vertical-align:-0.125em;}#mermaid-svg-Bn4tomyeFgvCYjWP .node .label-icon path{fill:currentColor;stroke:revert;stroke-width:revert;}#mermaid-svg-Bn4tomyeFgvCYjWP :root{--mermaid-font-family:"trebuchet ms",verdana,arial,sans-serif;} Middleware A pre
Middleware B pre
Tool Execute
Middleware B post
Middleware A post

这意味着你可以在工具调用前后做很多工程化事情:

  • 参数校验
  • 审批与权限控制
  • 缓存
  • 限流
  • 异常兜底
  • 响应包装

一个最小中间件示意:

python 复制代码
class TimingMiddleware:
    async def __call__(self, ctx, call_next):
        start = time.time()
        result = await call_next(ctx)
        print(f"tool cost: {time.time() - start:.2f}s")
        return result

而真正让 Agent 能进生产环境的,是执行控制:

  • maxIters:防止无限循环
  • checkRunning:防止并发调用把状态打乱
  • timeout:防止模型或工具卡死
  • retry:应对超时、限流、网络波动

Java 侧的配置会更直观一些:

java 复制代码
ExecutionConfig modelConfig = ExecutionConfig.builder()
    .timeout(Duration.ofMinutes(2))
    .maxAttempts(3)
    .build();

ExecutionConfig toolConfig = ExecutionConfig.builder()
    .timeout(Duration.ofSeconds(30))
    .maxAttempts(1)
    .build();

ReActAgent agent = ReActAgent.builder()
    .name("Assistant")
    .model(model)
    .maxIters(10)
    .checkRunning(true)
    .modelExecutionConfig(modelConfig)
    .toolExecutionConfig(toolConfig)
    .build();

这里有一个很实用的工程原则:

  • 模型调用通常可以重试
  • 工具调用默认不建议重试,因为很多工具有副作用

另外,Python 文档明确说明,ReActAgent 支持 interrupt 机制,可以在回复或工具执行过程中中断,并正确保存记忆与状态。
工具 模型 Agent 用户 工具 模型 Agent 用户 #mermaid-svg-4qcgsyn6u9AvmR1N{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;fill:#333;}@keyframes edge-animation-frame{from{stroke-dashoffset:0;}}@keyframes dash{to{stroke-dashoffset:0;}}#mermaid-svg-4qcgsyn6u9AvmR1N .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#mermaid-svg-4qcgsyn6u9AvmR1N .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#mermaid-svg-4qcgsyn6u9AvmR1N .error-icon{fill:#552222;}#mermaid-svg-4qcgsyn6u9AvmR1N .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-4qcgsyn6u9AvmR1N .edge-thickness-normal{stroke-width:1px;}#mermaid-svg-4qcgsyn6u9AvmR1N .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-4qcgsyn6u9AvmR1N .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-4qcgsyn6u9AvmR1N .edge-thickness-invisible{stroke-width:0;fill:none;}#mermaid-svg-4qcgsyn6u9AvmR1N .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-4qcgsyn6u9AvmR1N .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-4qcgsyn6u9AvmR1N .marker{fill:#333333;stroke:#333333;}#mermaid-svg-4qcgsyn6u9AvmR1N .marker.cross{stroke:#333333;}#mermaid-svg-4qcgsyn6u9AvmR1N svg{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-4qcgsyn6u9AvmR1N p{margin:0;}#mermaid-svg-4qcgsyn6u9AvmR1N .actor{stroke:hsl(259.6261682243, 59.7765363128%, 87.9019607843%);fill:#ECECFF;}#mermaid-svg-4qcgsyn6u9AvmR1N text.actor>tspan{fill:black;stroke:none;}#mermaid-svg-4qcgsyn6u9AvmR1N .actor-line{stroke:hsl(259.6261682243, 59.7765363128%, 87.9019607843%);}#mermaid-svg-4qcgsyn6u9AvmR1N .innerArc{stroke-width:1.5;stroke-dasharray:none;}#mermaid-svg-4qcgsyn6u9AvmR1N .messageLine0{stroke-width:1.5;stroke-dasharray:none;stroke:#333;}#mermaid-svg-4qcgsyn6u9AvmR1N .messageLine1{stroke-width:1.5;stroke-dasharray:2,2;stroke:#333;}#mermaid-svg-4qcgsyn6u9AvmR1N #arrowhead path{fill:#333;stroke:#333;}#mermaid-svg-4qcgsyn6u9AvmR1N .sequenceNumber{fill:white;}#mermaid-svg-4qcgsyn6u9AvmR1N #sequencenumber{fill:#333;}#mermaid-svg-4qcgsyn6u9AvmR1N #crosshead path{fill:#333;stroke:#333;}#mermaid-svg-4qcgsyn6u9AvmR1N .messageText{fill:#333;stroke:none;}#mermaid-svg-4qcgsyn6u9AvmR1N .labelBox{stroke:hsl(259.6261682243, 59.7765363128%, 87.9019607843%);fill:#ECECFF;}#mermaid-svg-4qcgsyn6u9AvmR1N .labelText,#mermaid-svg-4qcgsyn6u9AvmR1N .labelText>tspan{fill:black;stroke:none;}#mermaid-svg-4qcgsyn6u9AvmR1N .loopText,#mermaid-svg-4qcgsyn6u9AvmR1N .loopText>tspan{fill:black;stroke:none;}#mermaid-svg-4qcgsyn6u9AvmR1N .loopLine{stroke-width:2px;stroke-dasharray:2,2;stroke:hsl(259.6261682243, 59.7765363128%, 87.9019607843%);fill:hsl(259.6261682243, 59.7765363128%, 87.9019607843%);}#mermaid-svg-4qcgsyn6u9AvmR1N .note{stroke:#aaaa33;fill:#fff5ad;}#mermaid-svg-4qcgsyn6u9AvmR1N .noteText,#mermaid-svg-4qcgsyn6u9AvmR1N .noteText>tspan{fill:black;stroke:none;}#mermaid-svg-4qcgsyn6u9AvmR1N .activation0{fill:#f4f4f4;stroke:#666;}#mermaid-svg-4qcgsyn6u9AvmR1N .activation1{fill:#f4f4f4;stroke:#666;}#mermaid-svg-4qcgsyn6u9AvmR1N .activation2{fill:#f4f4f4;stroke:#666;}#mermaid-svg-4qcgsyn6u9AvmR1N .actorPopupMenu{position:absolute;}#mermaid-svg-4qcgsyn6u9AvmR1N .actorPopupMenuPanel{position:absolute;fill:#ECECFF;box-shadow:0px 8px 16px 0px rgba(0,0,0,0.2);filter:drop-shadow(3px 5px 2px rgb(0 0 0 / 0.4));}#mermaid-svg-4qcgsyn6u9AvmR1N .actor-man line{stroke:hsl(259.6261682243, 59.7765363128%, 87.9019607843%);fill:#ECECFF;}#mermaid-svg-4qcgsyn6u9AvmR1N .actor-man circle,#mermaid-svg-4qcgsyn6u9AvmR1N line{stroke:hsl(259.6261682243, 59.7765363128%, 87.9019607843%);fill:#ECECFF;stroke-width:2px;}#mermaid-svg-4qcgsyn6u9AvmR1N :root{--mermaid-font-family:"trebuchet ms",verdana,arial,sans-serif;} alt 工具超时 工具成功 loop 最多 maxIters 次 发起任务 reasoning 是否需要调用工具 acting timeout fail / retry / interrupt result 最终结果

八、一个最小实战例子

假设我们做一个"研究助理 Agent",它的执行流程可以概括成这样:

  1. 接收用户问题
  2. 读取记忆,判断当前任务阶段
  3. 必要时调用搜索或文件工具
  4. 把工具结果写回记忆
  5. 上下文过长时触发压缩
  6. Hook 记录关键日志
  7. 达到目标或轮次上限后结束

LLM Tool Memory Agent 用户 LLM Tool Memory Agent 用户 #mermaid-svg-BX6len28uCCYTgLF{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;fill:#333;}@keyframes edge-animation-frame{from{stroke-dashoffset:0;}}@keyframes dash{to{stroke-dashoffset:0;}}#mermaid-svg-BX6len28uCCYTgLF .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#mermaid-svg-BX6len28uCCYTgLF .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#mermaid-svg-BX6len28uCCYTgLF .error-icon{fill:#552222;}#mermaid-svg-BX6len28uCCYTgLF .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-BX6len28uCCYTgLF .edge-thickness-normal{stroke-width:1px;}#mermaid-svg-BX6len28uCCYTgLF .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-BX6len28uCCYTgLF .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-BX6len28uCCYTgLF .edge-thickness-invisible{stroke-width:0;fill:none;}#mermaid-svg-BX6len28uCCYTgLF .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-BX6len28uCCYTgLF .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-BX6len28uCCYTgLF .marker{fill:#333333;stroke:#333333;}#mermaid-svg-BX6len28uCCYTgLF .marker.cross{stroke:#333333;}#mermaid-svg-BX6len28uCCYTgLF svg{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-BX6len28uCCYTgLF p{margin:0;}#mermaid-svg-BX6len28uCCYTgLF .actor{stroke:hsl(259.6261682243, 59.7765363128%, 87.9019607843%);fill:#ECECFF;}#mermaid-svg-BX6len28uCCYTgLF text.actor>tspan{fill:black;stroke:none;}#mermaid-svg-BX6len28uCCYTgLF .actor-line{stroke:hsl(259.6261682243, 59.7765363128%, 87.9019607843%);}#mermaid-svg-BX6len28uCCYTgLF .innerArc{stroke-width:1.5;stroke-dasharray:none;}#mermaid-svg-BX6len28uCCYTgLF .messageLine0{stroke-width:1.5;stroke-dasharray:none;stroke:#333;}#mermaid-svg-BX6len28uCCYTgLF .messageLine1{stroke-width:1.5;stroke-dasharray:2,2;stroke:#333;}#mermaid-svg-BX6len28uCCYTgLF #arrowhead path{fill:#333;stroke:#333;}#mermaid-svg-BX6len28uCCYTgLF .sequenceNumber{fill:white;}#mermaid-svg-BX6len28uCCYTgLF #sequencenumber{fill:#333;}#mermaid-svg-BX6len28uCCYTgLF #crosshead path{fill:#333;stroke:#333;}#mermaid-svg-BX6len28uCCYTgLF .messageText{fill:#333;stroke:none;}#mermaid-svg-BX6len28uCCYTgLF .labelBox{stroke:hsl(259.6261682243, 59.7765363128%, 87.9019607843%);fill:#ECECFF;}#mermaid-svg-BX6len28uCCYTgLF .labelText,#mermaid-svg-BX6len28uCCYTgLF .labelText>tspan{fill:black;stroke:none;}#mermaid-svg-BX6len28uCCYTgLF .loopText,#mermaid-svg-BX6len28uCCYTgLF .loopText>tspan{fill:black;stroke:none;}#mermaid-svg-BX6len28uCCYTgLF .loopLine{stroke-width:2px;stroke-dasharray:2,2;stroke:hsl(259.6261682243, 59.7765363128%, 87.9019607843%);fill:hsl(259.6261682243, 59.7765363128%, 87.9019607843%);}#mermaid-svg-BX6len28uCCYTgLF .note{stroke:#aaaa33;fill:#fff5ad;}#mermaid-svg-BX6len28uCCYTgLF .noteText,#mermaid-svg-BX6len28uCCYTgLF .noteText>tspan{fill:black;stroke:none;}#mermaid-svg-BX6len28uCCYTgLF .activation0{fill:#f4f4f4;stroke:#666;}#mermaid-svg-BX6len28uCCYTgLF .activation1{fill:#f4f4f4;stroke:#666;}#mermaid-svg-BX6len28uCCYTgLF .activation2{fill:#f4f4f4;stroke:#666;}#mermaid-svg-BX6len28uCCYTgLF .actorPopupMenu{position:absolute;}#mermaid-svg-BX6len28uCCYTgLF .actorPopupMenuPanel{position:absolute;fill:#ECECFF;box-shadow:0px 8px 16px 0px rgba(0,0,0,0.2);filter:drop-shadow(3px 5px 2px rgb(0 0 0 / 0.4));}#mermaid-svg-BX6len28uCCYTgLF .actor-man line{stroke:hsl(259.6261682243, 59.7765363128%, 87.9019607843%);fill:#ECECFF;}#mermaid-svg-BX6len28uCCYTgLF .actor-man circle,#mermaid-svg-BX6len28uCCYTgLF line{stroke:hsl(259.6261682243, 59.7765363128%, 87.9019607843%);fill:#ECECFF;stroke-width:2px;}#mermaid-svg-BX6len28uCCYTgLF :root{--mermaid-font-family:"trebuchet ms",verdana,arial,sans-serif;} 提问 读取历史 推理 需要调用工具 执行工具 返回结果 写入结果 继续推理 输出答案

一个足够概括的初始化示例如下:

python 复制代码
agent = ReActAgent(
    name="research-assistant",
    model=model,
    memory=memory,
    toolkit=toolkit,
    formatter=formatter,
    compression_config=ReActAgent.CompressionConfig(
        enable=True,
        trigger_threshold=10000,
        keep_recent=3,
    ),
)

这也是 AgentScope 和"聊天机器人封装"的根本差别:它不是每轮都重新开始,而是在持续维护一个任务执行现场。

九、选型建议

如果你更看重:

  • Python 生态
  • Agent 机制透明
  • 灵活实验和快速迭代
  • 对记忆、压缩、Hook、工具链做深度定制

那 AgentScope 更合适。

如果你更看重:

  • Java / Spring 主栈
  • 企业系统接入
  • Graph 编排和服务治理
  • 平台化和可视化集成

那 Spring AI Alibaba 往往更顺手。

最实际的判断不是"谁更先进",而是:

你现在最缺的是 Agent 内核运行机制,还是企业级落地框架。

十、结语

AgentScope 值得关注的地方,不是"它也能调模型",而是它把 Agent 真正难的部分做成了清晰的工程机制:记忆怎么存、上下文怎么压、关键节点怎么扩展、执行过程怎么限制、异常时怎么收场。

这也是 Agent 从"会聊天"走向"能干活"的分水岭。

附:文中可直接复用的结论图

#mermaid-svg-yHtNpWUPRiWdM4G3{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;fill:#333;}@keyframes edge-animation-frame{from{stroke-dashoffset:0;}}@keyframes dash{to{stroke-dashoffset:0;}}#mermaid-svg-yHtNpWUPRiWdM4G3 .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#mermaid-svg-yHtNpWUPRiWdM4G3 .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#mermaid-svg-yHtNpWUPRiWdM4G3 .error-icon{fill:#552222;}#mermaid-svg-yHtNpWUPRiWdM4G3 .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-yHtNpWUPRiWdM4G3 .edge-thickness-normal{stroke-width:1px;}#mermaid-svg-yHtNpWUPRiWdM4G3 .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-yHtNpWUPRiWdM4G3 .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-yHtNpWUPRiWdM4G3 .edge-thickness-invisible{stroke-width:0;fill:none;}#mermaid-svg-yHtNpWUPRiWdM4G3 .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-yHtNpWUPRiWdM4G3 .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-yHtNpWUPRiWdM4G3 .marker{fill:#333333;stroke:#333333;}#mermaid-svg-yHtNpWUPRiWdM4G3 .marker.cross{stroke:#333333;}#mermaid-svg-yHtNpWUPRiWdM4G3 svg{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-yHtNpWUPRiWdM4G3 p{margin:0;}#mermaid-svg-yHtNpWUPRiWdM4G3 .edge{stroke-width:3;}#mermaid-svg-yHtNpWUPRiWdM4G3 .section--1 rect,#mermaid-svg-yHtNpWUPRiWdM4G3 .section--1 path,#mermaid-svg-yHtNpWUPRiWdM4G3 .section--1 circle,#mermaid-svg-yHtNpWUPRiWdM4G3 .section--1 polygon,#mermaid-svg-yHtNpWUPRiWdM4G3 .section--1 path{fill:hsl(240, 100%, 76.2745098039%);}#mermaid-svg-yHtNpWUPRiWdM4G3 .section--1 text{fill:#ffffff;}#mermaid-svg-yHtNpWUPRiWdM4G3 .node-icon--1{font-size:40px;color:#ffffff;}#mermaid-svg-yHtNpWUPRiWdM4G3 .section-edge--1{stroke:hsl(240, 100%, 76.2745098039%);}#mermaid-svg-yHtNpWUPRiWdM4G3 .edge-depth--1{stroke-width:17;}#mermaid-svg-yHtNpWUPRiWdM4G3 .section--1 line{stroke:hsl(60, 100%, 86.2745098039%);stroke-width:3;}#mermaid-svg-yHtNpWUPRiWdM4G3 .disabled,#mermaid-svg-yHtNpWUPRiWdM4G3 .disabled circle,#mermaid-svg-yHtNpWUPRiWdM4G3 .disabled text{fill:lightgray;}#mermaid-svg-yHtNpWUPRiWdM4G3 .disabled text{fill:#efefef;}#mermaid-svg-yHtNpWUPRiWdM4G3 .section-0 rect,#mermaid-svg-yHtNpWUPRiWdM4G3 .section-0 path,#mermaid-svg-yHtNpWUPRiWdM4G3 .section-0 circle,#mermaid-svg-yHtNpWUPRiWdM4G3 .section-0 polygon,#mermaid-svg-yHtNpWUPRiWdM4G3 .section-0 path{fill:hsl(60, 100%, 73.5294117647%);}#mermaid-svg-yHtNpWUPRiWdM4G3 .section-0 text{fill:black;}#mermaid-svg-yHtNpWUPRiWdM4G3 .node-icon-0{font-size:40px;color:black;}#mermaid-svg-yHtNpWUPRiWdM4G3 .section-edge-0{stroke:hsl(60, 100%, 73.5294117647%);}#mermaid-svg-yHtNpWUPRiWdM4G3 .edge-depth-0{stroke-width:14;}#mermaid-svg-yHtNpWUPRiWdM4G3 .section-0 line{stroke:hsl(240, 100%, 83.5294117647%);stroke-width:3;}#mermaid-svg-yHtNpWUPRiWdM4G3 .disabled,#mermaid-svg-yHtNpWUPRiWdM4G3 .disabled circle,#mermaid-svg-yHtNpWUPRiWdM4G3 .disabled text{fill:lightgray;}#mermaid-svg-yHtNpWUPRiWdM4G3 .disabled text{fill:#efefef;}#mermaid-svg-yHtNpWUPRiWdM4G3 .section-1 rect,#mermaid-svg-yHtNpWUPRiWdM4G3 .section-1 path,#mermaid-svg-yHtNpWUPRiWdM4G3 .section-1 circle,#mermaid-svg-yHtNpWUPRiWdM4G3 .section-1 polygon,#mermaid-svg-yHtNpWUPRiWdM4G3 .section-1 path{fill:hsl(80, 100%, 76.2745098039%);}#mermaid-svg-yHtNpWUPRiWdM4G3 .section-1 text{fill:black;}#mermaid-svg-yHtNpWUPRiWdM4G3 .node-icon-1{font-size:40px;color:black;}#mermaid-svg-yHtNpWUPRiWdM4G3 .section-edge-1{stroke:hsl(80, 100%, 76.2745098039%);}#mermaid-svg-yHtNpWUPRiWdM4G3 .edge-depth-1{stroke-width:11;}#mermaid-svg-yHtNpWUPRiWdM4G3 .section-1 line{stroke:hsl(260, 100%, 86.2745098039%);stroke-width:3;}#mermaid-svg-yHtNpWUPRiWdM4G3 .disabled,#mermaid-svg-yHtNpWUPRiWdM4G3 .disabled circle,#mermaid-svg-yHtNpWUPRiWdM4G3 .disabled text{fill:lightgray;}#mermaid-svg-yHtNpWUPRiWdM4G3 .disabled text{fill:#efefef;}#mermaid-svg-yHtNpWUPRiWdM4G3 .section-2 rect,#mermaid-svg-yHtNpWUPRiWdM4G3 .section-2 path,#mermaid-svg-yHtNpWUPRiWdM4G3 .section-2 circle,#mermaid-svg-yHtNpWUPRiWdM4G3 .section-2 polygon,#mermaid-svg-yHtNpWUPRiWdM4G3 .section-2 path{fill:hsl(270, 100%, 76.2745098039%);}#mermaid-svg-yHtNpWUPRiWdM4G3 .section-2 text{fill:#ffffff;}#mermaid-svg-yHtNpWUPRiWdM4G3 .node-icon-2{font-size:40px;color:#ffffff;}#mermaid-svg-yHtNpWUPRiWdM4G3 .section-edge-2{stroke:hsl(270, 100%, 76.2745098039%);}#mermaid-svg-yHtNpWUPRiWdM4G3 .edge-depth-2{stroke-width:8;}#mermaid-svg-yHtNpWUPRiWdM4G3 .section-2 line{stroke:hsl(90, 100%, 86.2745098039%);stroke-width:3;}#mermaid-svg-yHtNpWUPRiWdM4G3 .disabled,#mermaid-svg-yHtNpWUPRiWdM4G3 .disabled circle,#mermaid-svg-yHtNpWUPRiWdM4G3 .disabled text{fill:lightgray;}#mermaid-svg-yHtNpWUPRiWdM4G3 .disabled text{fill:#efefef;}#mermaid-svg-yHtNpWUPRiWdM4G3 .section-3 rect,#mermaid-svg-yHtNpWUPRiWdM4G3 .section-3 path,#mermaid-svg-yHtNpWUPRiWdM4G3 .section-3 circle,#mermaid-svg-yHtNpWUPRiWdM4G3 .section-3 polygon,#mermaid-svg-yHtNpWUPRiWdM4G3 .section-3 path{fill:hsl(300, 100%, 76.2745098039%);}#mermaid-svg-yHtNpWUPRiWdM4G3 .section-3 text{fill:black;}#mermaid-svg-yHtNpWUPRiWdM4G3 .node-icon-3{font-size:40px;color:black;}#mermaid-svg-yHtNpWUPRiWdM4G3 .section-edge-3{stroke:hsl(300, 100%, 76.2745098039%);}#mermaid-svg-yHtNpWUPRiWdM4G3 .edge-depth-3{stroke-width:5;}#mermaid-svg-yHtNpWUPRiWdM4G3 .section-3 line{stroke:hsl(120, 100%, 86.2745098039%);stroke-width:3;}#mermaid-svg-yHtNpWUPRiWdM4G3 .disabled,#mermaid-svg-yHtNpWUPRiWdM4G3 .disabled circle,#mermaid-svg-yHtNpWUPRiWdM4G3 .disabled text{fill:lightgray;}#mermaid-svg-yHtNpWUPRiWdM4G3 .disabled text{fill:#efefef;}#mermaid-svg-yHtNpWUPRiWdM4G3 .section-4 rect,#mermaid-svg-yHtNpWUPRiWdM4G3 .section-4 path,#mermaid-svg-yHtNpWUPRiWdM4G3 .section-4 circle,#mermaid-svg-yHtNpWUPRiWdM4G3 .section-4 polygon,#mermaid-svg-yHtNpWUPRiWdM4G3 .section-4 path{fill:hsl(330, 100%, 76.2745098039%);}#mermaid-svg-yHtNpWUPRiWdM4G3 .section-4 text{fill:black;}#mermaid-svg-yHtNpWUPRiWdM4G3 .node-icon-4{font-size:40px;color:black;}#mermaid-svg-yHtNpWUPRiWdM4G3 .section-edge-4{stroke:hsl(330, 100%, 76.2745098039%);}#mermaid-svg-yHtNpWUPRiWdM4G3 .edge-depth-4{stroke-width:2;}#mermaid-svg-yHtNpWUPRiWdM4G3 .section-4 line{stroke:hsl(150, 100%, 86.2745098039%);stroke-width:3;}#mermaid-svg-yHtNpWUPRiWdM4G3 .disabled,#mermaid-svg-yHtNpWUPRiWdM4G3 .disabled circle,#mermaid-svg-yHtNpWUPRiWdM4G3 .disabled text{fill:lightgray;}#mermaid-svg-yHtNpWUPRiWdM4G3 .disabled text{fill:#efefef;}#mermaid-svg-yHtNpWUPRiWdM4G3 .section-5 rect,#mermaid-svg-yHtNpWUPRiWdM4G3 .section-5 path,#mermaid-svg-yHtNpWUPRiWdM4G3 .section-5 circle,#mermaid-svg-yHtNpWUPRiWdM4G3 .section-5 polygon,#mermaid-svg-yHtNpWUPRiWdM4G3 .section-5 path{fill:hsl(0, 100%, 76.2745098039%);}#mermaid-svg-yHtNpWUPRiWdM4G3 .section-5 text{fill:black;}#mermaid-svg-yHtNpWUPRiWdM4G3 .node-icon-5{font-size:40px;color:black;}#mermaid-svg-yHtNpWUPRiWdM4G3 .section-edge-5{stroke:hsl(0, 100%, 76.2745098039%);}#mermaid-svg-yHtNpWUPRiWdM4G3 .edge-depth-5{stroke-width:-1;}#mermaid-svg-yHtNpWUPRiWdM4G3 .section-5 line{stroke:hsl(180, 100%, 86.2745098039%);stroke-width:3;}#mermaid-svg-yHtNpWUPRiWdM4G3 .disabled,#mermaid-svg-yHtNpWUPRiWdM4G3 .disabled circle,#mermaid-svg-yHtNpWUPRiWdM4G3 .disabled text{fill:lightgray;}#mermaid-svg-yHtNpWUPRiWdM4G3 .disabled text{fill:#efefef;}#mermaid-svg-yHtNpWUPRiWdM4G3 .section-6 rect,#mermaid-svg-yHtNpWUPRiWdM4G3 .section-6 path,#mermaid-svg-yHtNpWUPRiWdM4G3 .section-6 circle,#mermaid-svg-yHtNpWUPRiWdM4G3 .section-6 polygon,#mermaid-svg-yHtNpWUPRiWdM4G3 .section-6 path{fill:hsl(30, 100%, 76.2745098039%);}#mermaid-svg-yHtNpWUPRiWdM4G3 .section-6 text{fill:black;}#mermaid-svg-yHtNpWUPRiWdM4G3 .node-icon-6{font-size:40px;color:black;}#mermaid-svg-yHtNpWUPRiWdM4G3 .section-edge-6{stroke:hsl(30, 100%, 76.2745098039%);}#mermaid-svg-yHtNpWUPRiWdM4G3 .edge-depth-6{stroke-width:-4;}#mermaid-svg-yHtNpWUPRiWdM4G3 .section-6 line{stroke:hsl(210, 100%, 86.2745098039%);stroke-width:3;}#mermaid-svg-yHtNpWUPRiWdM4G3 .disabled,#mermaid-svg-yHtNpWUPRiWdM4G3 .disabled circle,#mermaid-svg-yHtNpWUPRiWdM4G3 .disabled text{fill:lightgray;}#mermaid-svg-yHtNpWUPRiWdM4G3 .disabled text{fill:#efefef;}#mermaid-svg-yHtNpWUPRiWdM4G3 .section-7 rect,#mermaid-svg-yHtNpWUPRiWdM4G3 .section-7 path,#mermaid-svg-yHtNpWUPRiWdM4G3 .section-7 circle,#mermaid-svg-yHtNpWUPRiWdM4G3 .section-7 polygon,#mermaid-svg-yHtNpWUPRiWdM4G3 .section-7 path{fill:hsl(90, 100%, 76.2745098039%);}#mermaid-svg-yHtNpWUPRiWdM4G3 .section-7 text{fill:black;}#mermaid-svg-yHtNpWUPRiWdM4G3 .node-icon-7{font-size:40px;color:black;}#mermaid-svg-yHtNpWUPRiWdM4G3 .section-edge-7{stroke:hsl(90, 100%, 76.2745098039%);}#mermaid-svg-yHtNpWUPRiWdM4G3 .edge-depth-7{stroke-width:-7;}#mermaid-svg-yHtNpWUPRiWdM4G3 .section-7 line{stroke:hsl(270, 100%, 86.2745098039%);stroke-width:3;}#mermaid-svg-yHtNpWUPRiWdM4G3 .disabled,#mermaid-svg-yHtNpWUPRiWdM4G3 .disabled circle,#mermaid-svg-yHtNpWUPRiWdM4G3 .disabled text{fill:lightgray;}#mermaid-svg-yHtNpWUPRiWdM4G3 .disabled text{fill:#efefef;}#mermaid-svg-yHtNpWUPRiWdM4G3 .section-8 rect,#mermaid-svg-yHtNpWUPRiWdM4G3 .section-8 path,#mermaid-svg-yHtNpWUPRiWdM4G3 .section-8 circle,#mermaid-svg-yHtNpWUPRiWdM4G3 .section-8 polygon,#mermaid-svg-yHtNpWUPRiWdM4G3 .section-8 path{fill:hsl(150, 100%, 76.2745098039%);}#mermaid-svg-yHtNpWUPRiWdM4G3 .section-8 text{fill:black;}#mermaid-svg-yHtNpWUPRiWdM4G3 .node-icon-8{font-size:40px;color:black;}#mermaid-svg-yHtNpWUPRiWdM4G3 .section-edge-8{stroke:hsl(150, 100%, 76.2745098039%);}#mermaid-svg-yHtNpWUPRiWdM4G3 .edge-depth-8{stroke-width:-10;}#mermaid-svg-yHtNpWUPRiWdM4G3 .section-8 line{stroke:hsl(330, 100%, 86.2745098039%);stroke-width:3;}#mermaid-svg-yHtNpWUPRiWdM4G3 .disabled,#mermaid-svg-yHtNpWUPRiWdM4G3 .disabled circle,#mermaid-svg-yHtNpWUPRiWdM4G3 .disabled text{fill:lightgray;}#mermaid-svg-yHtNpWUPRiWdM4G3 .disabled text{fill:#efefef;}#mermaid-svg-yHtNpWUPRiWdM4G3 .section-9 rect,#mermaid-svg-yHtNpWUPRiWdM4G3 .section-9 path,#mermaid-svg-yHtNpWUPRiWdM4G3 .section-9 circle,#mermaid-svg-yHtNpWUPRiWdM4G3 .section-9 polygon,#mermaid-svg-yHtNpWUPRiWdM4G3 .section-9 path{fill:hsl(180, 100%, 76.2745098039%);}#mermaid-svg-yHtNpWUPRiWdM4G3 .section-9 text{fill:black;}#mermaid-svg-yHtNpWUPRiWdM4G3 .node-icon-9{font-size:40px;color:black;}#mermaid-svg-yHtNpWUPRiWdM4G3 .section-edge-9{stroke:hsl(180, 100%, 76.2745098039%);}#mermaid-svg-yHtNpWUPRiWdM4G3 .edge-depth-9{stroke-width:-13;}#mermaid-svg-yHtNpWUPRiWdM4G3 .section-9 line{stroke:hsl(0, 100%, 86.2745098039%);stroke-width:3;}#mermaid-svg-yHtNpWUPRiWdM4G3 .disabled,#mermaid-svg-yHtNpWUPRiWdM4G3 .disabled circle,#mermaid-svg-yHtNpWUPRiWdM4G3 .disabled text{fill:lightgray;}#mermaid-svg-yHtNpWUPRiWdM4G3 .disabled text{fill:#efefef;}#mermaid-svg-yHtNpWUPRiWdM4G3 .section-10 rect,#mermaid-svg-yHtNpWUPRiWdM4G3 .section-10 path,#mermaid-svg-yHtNpWUPRiWdM4G3 .section-10 circle,#mermaid-svg-yHtNpWUPRiWdM4G3 .section-10 polygon,#mermaid-svg-yHtNpWUPRiWdM4G3 .section-10 path{fill:hsl(210, 100%, 76.2745098039%);}#mermaid-svg-yHtNpWUPRiWdM4G3 .section-10 text{fill:black;}#mermaid-svg-yHtNpWUPRiWdM4G3 .node-icon-10{font-size:40px;color:black;}#mermaid-svg-yHtNpWUPRiWdM4G3 .section-edge-10{stroke:hsl(210, 100%, 76.2745098039%);}#mermaid-svg-yHtNpWUPRiWdM4G3 .edge-depth-10{stroke-width:-16;}#mermaid-svg-yHtNpWUPRiWdM4G3 .section-10 line{stroke:hsl(30, 100%, 86.2745098039%);stroke-width:3;}#mermaid-svg-yHtNpWUPRiWdM4G3 .disabled,#mermaid-svg-yHtNpWUPRiWdM4G3 .disabled circle,#mermaid-svg-yHtNpWUPRiWdM4G3 .disabled text{fill:lightgray;}#mermaid-svg-yHtNpWUPRiWdM4G3 .disabled text{fill:#efefef;}#mermaid-svg-yHtNpWUPRiWdM4G3 .section-root rect,#mermaid-svg-yHtNpWUPRiWdM4G3 .section-root path,#mermaid-svg-yHtNpWUPRiWdM4G3 .section-root circle,#mermaid-svg-yHtNpWUPRiWdM4G3 .section-root polygon{fill:hsl(240, 100%, 46.2745098039%);}#mermaid-svg-yHtNpWUPRiWdM4G3 .section-root text{fill:#ffffff;}#mermaid-svg-yHtNpWUPRiWdM4G3 .section-root span{color:#ffffff;}#mermaid-svg-yHtNpWUPRiWdM4G3 .section-2 span{color:#ffffff;}#mermaid-svg-yHtNpWUPRiWdM4G3 .icon-container{height:100%;display:flex;justify-content:center;align-items:center;}#mermaid-svg-yHtNpWUPRiWdM4G3 .edge{fill:none;}#mermaid-svg-yHtNpWUPRiWdM4G3 .mindmap-node-label{dy:1em;alignment-baseline:middle;text-anchor:middle;dominant-baseline:middle;text-align:center;}#mermaid-svg-yHtNpWUPRiWdM4G3 :root{--mermaid-font-family:"trebuchet ms",verdana,arial,sans-serif;} AgentScope
框架定位
Agent运行时
不只是模型SDK
核心机制
记忆存储
上下文压缩
Hook
Middleware
执行控制
选型关系
Python/机制优先
Java/企业优先

附:可直接摘用的三句总结

  • AgentScope 更像 Agent 的运行内核,Spring AI Alibaba 更像企业级 Java 落地框架。
  • AgentScope 的重点不是把模型接进来,而是让 Agent 在长任务里稳定跑下去。
  • 上下文压缩不是删历史,而是把历史整理成还能继续工作的上下文。

参考资料

相关推荐
grey_csdn14 小时前
1923_Qwen 3.6-35B-A3B的本地部署与调优
ai
wMoqi18 小时前
trae cn 中ai开发配置skill mcp 规则参考(yudao-cloud项目-个人用)
ai·skill·yudao·mcp·trae
「QT(C++)开发工程师」20 小时前
AI Agent 核心组件
人工智能·ai·aigc·ai编程·ai写作
武子康21 小时前
2026 开源视频生成模型全景图:按 9 类任务路由比按参数量选模型更可靠
人工智能·ai·chatgpt·openai·claude·世界模型·视频模型
笨蛋©21 小时前
2026年制造业实战:CAD图纸气泡图自动化生成与检验计划数字化指南
ai·数字化·cad·质量管理·制造业
ofoxcoding1 天前
Windows 上 Codex 报 “failed to start codex app-server“:成因与修复(2026)
windows·ai
曦尧1 天前
AWS 官方 AI 编码 Agent 工具包:Agent Toolkit for AWS 深度解读
ai·自动化
一叶龙洲1 天前
Cursor中安装Skills
ai
曦尧1 天前
Kimi CLI → Kimi Code CLI:月之暗面的终端 AI Agent 演进路线全解析
ai·自动化