Agent vs Workflow

Agent 与 Workflow 的边界

Workflow 路径写死,Agent 在每一步自己决定下一步——这是本质区别。

详解

区分 Agent 和 Workflow 的核心标准只有一条:下一步由谁决定?Workflow 的执行路径由你(开发者)写死在代码里——"先调检索,再调模型,最后输出" 这个顺序在代码里固定不变,模型只是其中一个节点,不会改变流程走向。Agent 则相反:每一步该做什么,由模型自己在运行时决定。模型看到当前状态后,自主选择调哪个工具、要不要继续循环、何时输出最终答案。两者都可以使用工具,区别在于"谁在掌舵"。工程上的选择原则:任务路径固定、要求结果可预测、需要控制成本时用 Workflow;任务路径不确定、需要根据中间结果临时调整策略时用 Agent。实际项目里两者常常混用——Workflow 里嵌入 Agent 节点,Agent 里调用固定的 Workflow 子流程。

一个类比
Workflow 像地铁——站点和顺序固定,乘客上车就按既定路线走,快且可靠。Agent 像打车——司机(模型)根据实时路况自己决定走哪条路,遇到堵车随时绕行,灵活但行程不完全可预测。
举个例子
# Workflow:路径写死在代码里,模型只负责单步生成
import anthropic

client = anthropic.Anthropic()

def workflow_summarize_and_translate(text: str) -> str:
    """固定流程:先摘要,再翻译——顺序由开发者决定,不可更改"""
    # 第一步:摘要(固定)
    summary_resp = client.messages.create(
        model="claude-sonnet-4-6",
        max_tokens=256,
        messages=[{"role": "user", "content": f"请用一句话总结:{text}"}]
    )
    summary = summary_resp.content[0].text

    # 第二步:翻译(固定,永远在摘要之后)
    trans_resp = client.messages.create(
        model="claude-sonnet-4-6",
        max_tokens=256,
        messages=[{"role": "user", "content": f"翻译成英文:{summary}"}]
    )
    return trans_resp.content[0].text

# Agent:模型自己决定是否调工具、何时结束(有 max_steps 安全阀)
def agent_answer(question: str, max_steps: int = 10) -> str:
    """Agent 循环:模型自主决定每步行动,直到它认为任务完成"""
    tools = [{"name": "search", "description": "搜索最新信息",
               "input_schema": {"type": "object",
                                "properties": {"query": {"type": "string"}},
                                "required": ["query"]}}]
    messages = [{"role": "user", "content": question}]

    for _ in range(max_steps):              # 安全阀:防止死循环
        resp = client.messages.create(
            model="claude-sonnet-4-6", max_tokens=1024,
            tools=tools, messages=messages
        )
        if resp.stop_reason == "end_turn":  # 模型自己判断:任务完成
            return next(b.text for b in resp.content if hasattr(b, "text"))
        # 模型选择了调工具——执行后继续循环
        tool_call = next(b for b in resp.content if b.type == "tool_use")
        result = f"搜索结果:关于 {tool_call.input['query']} 的最新数据"
        messages += [
            {"role": "assistant", "content": resp.content},
            {"role": "user", "content": [{"type": "tool_result",
             "tool_use_id": tool_call.id, "content": result}]}
        ]
    return "超出最大步数"
PYTHON 示例
相关概念