智能体运行时Agent runtime · 单元Unit 02
智能体生命周期:一次回复到底发生了什么Agent lifecycle: what happens during one reply
沿着 reply / reply_stream 走完一次推理:组装上下文、调用模型、执行工具、回填结果、更新状态。掌握中断、观察、压缩与迭代上限。Walk one reply end to end: assemble context, call the model, run tools, feed results back and persist state. Learn interruption, observation, compression and iteration limits.
学习目标Learning goals
- 区分 reply、reply_stream、observe 的职责Tell reply, reply_stream and observe apart
- 解释 ReAct 循环如何决定继续调用工具还是结束Explain how the ReAct loop decides to call more tools or finish
- 在 Java 中找到 call、stream、interrupt 的对应入口Find call, stream and interrupt entry points in Java
概念闪卡Concept cards
点击卡片翻面查看解释。Click a card to flip it and reveal the explanation.
关键 APIKey APIs
点击进入对应的 Python 或 Java 参考页。Open the exact Python or Java reference for each type.
问题巩固Check yourself
完成 10 道题;答错会自动进入错题本。Answer all ten questions. Wrong answers are added to your wrong-answer book automatically.
01Python 中执行一次完整回复的入口是?Which Python method runs one complete reply?
解析Why reply 会跑完整个 ReAct 循环并返回最终消息;reply_stream 是同一过程的流式版本。reply runs the whole ReAct loop and returns the final message; reply_stream is the streaming form of the same process.
02需要边生成边展示给前端时用哪个方法?Which method streams output to a UI as it is generated?
解析Why reply_stream 产出文本增量、思考增量、工具调用和生命周期事件。reply_stream emits text deltas, thinking deltas, tool calls and lifecycle events.
03只把外部消息写入上下文、不触发回复,应调用?How do you write an external message into context without triggering a reply?
解析Why observe 用于接收用户、其他 Agent 或系统消息,只更新上下文。observe accepts messages from users, other agents or the system and only updates context.
04上下文接近窗口上限时,优先使用哪个 API?Which API should you use when context approaches the window limit?
解析Why compress_context 压缩历史消息,避免直接截断丢失任务关键信息。compress_context compresses history instead of truncating and losing task-critical information.
05Java 中发起非流式调用使用哪个方法?Which Java method starts a non-streaming call?
解析Why Java 通过 CallableAgent.call 发起调用;ReActAgent 同时实现了流式与非流式入口。Java uses CallableAgent.call for non-streaming calls; ReActAgent implements both streaming and non-streaming entry points.
06Java 中需要事件流时使用哪个方法?Which Java method produces an event stream?
解析Why StreamableAgent.stream 返回事件流,可配合 StreamOptions 控制包含哪些事件。StreamableAgent.stream returns an event stream; StreamOptions controls which events are included.
07Java 中取消正在运行的智能体的方法是?How do you cancel a running Java agent?
解析Why Agent.interrupt 触发中断,运行中的循环会在安全检查点退出。Agent.interrupt requests cancellation and the running loop exits at a safety checkpoint.
08保存可恢复会话状态的核心类型是?Which type stores recoverable conversation state?
解析Why AgentState 保存上下文、reply id、迭代计数和未完成工具调用,可持久化后恢复。AgentState stores context, reply id, iteration counters and unfinished tool calls, and can be persisted and restored.
09ReAct 循环的迭代与推理参数由哪个配置类型承载?Which config type carries ReAct iteration and reasoning settings?
解析Why ReActConfig 控制最大迭代次数等循环参数;ModelConfig 负责模型配置。ReActConfig controls loop parameters such as max iterations; ModelConfig handles model settings.
10达到最大迭代次数时会发出哪个事件?Which event is emitted when the maximum iteration count is reached?
解析Why ExceedMaxItersEvent 表示循环因迭代上限结束,应检查工具是否陷入死循环或预算是否过小。ExceedMaxItersEvent means the loop stopped at the iteration cap; check for tool loops or an undersized budget.