经典智能体架构:带有工具的 while 循环
From personal assistants to complex automated systems, agents are revolutionizing how we interact with technology. For many developers, building a good agent feels like navigating a maze of frameworks, layers of optimization, and tools, each adding its own overhead.
从个人助手到复杂的自动化系统,代理正在彻底改变我们与技术的交互方式。对许多开发者来说,构建一个优秀的代理就像在框架迷宫、层层优化和工具堆栈中摸索,每一步都会带来额外开销。
Surprisingly, many of the most popular and successful agents, including Claude Code and the OpenAI Agents SDK for example, share a common, straightforward architecture: a while loop that makes tool calls.
令人惊讶的是,许多最受欢迎且成功的 agent——例如 Claude Code 和 OpenAI Agents SDK——都共享一种简单直接的架构:一个不断调用工具的 while 循环。
Here's the basic structure:
基本结构如下:
That's it. Each iteration passes the current state into a language model, receives back a decision (usually a tool invocation or text response), and moves forward. The agent is just a system prompt and a handful of well-crafted tools.
就这么简单。每次迭代都将当前状态传入语言模型,接收一个决策(通常是工具调用或文本回复),然后继续前进。agent 只是一个系统提示和少量精心设计的工具。
This pattern wins for the same reason as UNIX pipes and React components: it's simple, composable, and flexible enough to handle complexity without becoming complex itself. It naturally extends to more advanced concepts like sub-agents (a tool call that invokes an independent agent loop) and multi-agents (independent agent loops that perform message passing with tool calls). It also allows you to focus on the problems that matter most: tool design, context engineering, and evaluation.
这种模式之所以胜出,原因与 UNIX 管道和 React 组件相同:它简单、可组合,且足够灵活,能在不自身变复杂的情况下处理复杂性。它自然延伸到更高级的概念,如子智能体(一个工具调用触发独立的智能体循环)和多智能体(独立的智能体循环通过工具调用进行消息传递)。它还让你专注于最重要的问题:工具设计、上下文工程和评估。
When you expose every API argument to a language model, it can become overloaded with irrelevant details and make mistakes. Instead, define each tool with only the essential parameters and provide a clear description tailored to the agent’s task. Only include inputs that are directly relevant to the agent’s objective.
当你把每个 API 参数都暴露给语言模型时,它可能被无关细节淹没并出错。相反,应为每个工具仅定义必要参数,并提供针对智能体任务的清晰描述。只包含与智能体目标直接相...