1.异步调用

事件处理机制

一个 Agent 实例可能同时面对多个事件:用户发来了新消息,之前调用的工具返回了结果,定时器到期了,另一个 Agent 发来了协作请求。如何高效、正确地处理这些事件,直接影响系统的性能和用户体验。

任务独立性判断

独立就可以并行,完成后立即返回给用户,历史会被加到主任务轨迹中

不独立:
紧急事件最高优先级,立即取消当前操作,更新待处理队列。把更新的轨迹作为LLM输入
常规事件直接放到队列末尾。等当前工具调用结束后,把队列事件作为LLM输入。

2.工具选择

类似泛型,根据功能相似性和使用场景重叠度对skill 进行整合。根据”明确的功能说明,适用边界,使用示例,输出格式,性能和成本提示“去调用一个函数。

感知工具:

搜索工具
读取工具
解析工具
查询工具

执行工具:

输入验证,权限控制
自我纠错:顺序修订——两个模型一个负责提议行动,一个负责审查批准。
自动验证,即时反馈:错误收尾截断

协作工具

帮助任务分解,并行处理,整合
sub-agent:提示词撰写;上下文传递(最小化,筛选,LLM生成上下文);

同步调用,异步调用(主agent要管理多个并发的子任务),流式协作(agent获得的东西有价值实时显示),多轮交互(对话式)

3.能力成长

智能总结机制,通过上下文感知,根据当前仍未和查询意图来提取最相关消息。
多方通信:第三方需要是结构化事件,agent才能保持清醒的认知被驱动,变成事件触发。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
SYSTEM = f"You are a coding agent at {os.getcwd()}. Use bash to solve tasks. Act, don't explain."

TOOLS = [{
    "name": "bash",
    "description": "Run a shell command.",
    "input_schema": {
        "type": "object",
        "properties": {"command": {"type": "string"}},
        "required": ["command"],
    },
}]
response = client.messages.create(
model=MODEL, system=SYSTEM, messages=messages,
tools=TOOLS, max_tokens=8000,
        )
output = run_bash(block.input["command"])

# -- The dispatch map: {tool_name: handler} --

TOOL_HANDLERS = {
    "bash":       lambda **kw: run_bash(kw["command"]),
    "read_file":  lambda **kw: run_read(kw["path"], kw.get("limit")),
    "write_file": lambda **kw: run_write(kw["path"], kw["content"]),
    "edit_file":  lambda **kw: run_edit(kw["path"], kw["old_text"], kw["new_text"]),
}
TOOLS = [
    {"name": "bash", "description": "Run a shell command.",
     "input_schema": {"type": "object", "properties": {"command": {"type": "string"}}, "required": ["command"]}},
    {"name": "read_file", "description": "Read file contents.",
     "input_schema": {"type": "object", "properties": {"path": {"type": "string"}, "limit": {"type": "integer"}}, "required": ["path"]}},
    {"name": "write_file", "description": "Write content to file.",
     "input_schema": {"type": "object", "properties": {"path": {"type": "string"}, "content": {"type": "string"}}, "required": ["path", "content"]}},
    {"name": "edit_file", "description": "Replace exact text in file.",
     "input_schema": {"type": "object", "properties": {"path": {"type": "string"}, "old_text": {"type": "string"}, "new_text": {"type": "string"}}, "required": ["path", "old_text", "new_text"]}},
]
handler = TOOL_HANDLERS.get(block.name)
output = handler(**block.input) if handler else f"Unknown tool: {block.name}"