Tool Runtime
Tool Runtime
在 LangGraph 中使用 ToolNode 执行工具时,通过 InjectedState 注解访问当前 graph state 中的字段,如 task_plan、task_index 等。
ToolNode 是执行工具的标准方式,它自动将当前 state 注入到工具函数中。工具函数签名添加 state: Annotated[AibotState, InjectedState] 参数即可读取 state(只读),不会暴露给 LLM。
1 | from typing_extensions import Annotated |
关键点:
InjectedState确保state参数不暴露在工具 schema 中,LLM 只看到queryToolNode(tools)自动处理工具调用、并行执行、错误处理- 在 executor_node 中使用
tool_node,LLM 通过bind_tools(tools)看到工具描述 - 读取 state 是只读的;要更新 state,返回
Command(update={"field": value})
工具get_sandbox_status只看到['messages']是因为create_agent默认使用标准AgentState,它只包含messages字段,而忽略了自定义AibotState。
create_agent需要显式指定state_schema=AibotState才能让代理和工具访问完整自定义state,包括user_question、token_usage等字段。
- Title: Tool Runtime
- Author: moyigeek
- Created at : 2026-03-17 08:27:46
- Updated at : 2026-04-18 09:58:10
- Link: https://blog.moyihust.eu.org/2026/03/17/Tool Runtime/
- License: This work is licensed under CC BY-NC-SA 4.0.
Comments