# 设计理念

|  | 传统 Agent | Agentic Programming |
| --- | --- | --- |
| 流程 | LLM 规划，循环运行到它说完成 | Python 代码 |
| 决策 | LLM 判断每一步、工具和参数 | Python 决定是否调用 LLM |
| 状态 | 塞进上下文 | 函数变量、返回值 |
| 测试 | 提示词回归 | 单元测试 |

把控制权交给概率黑盒，去做确定性代码本可以完成的工作，轨迹每次不同，历史不断膨胀。无法保证完成，出错也分不清是提示词、工具还是幻觉。

所以把任务拆成函数调用图，逐节点决定：用普通 Python，还是用调用 `llm(...)` 的 `Agent` 方法来做理解、生成或判断。LLM 成为你调用、约束和组合的东西。

## 三个基本元素

### `Agent` 方法

```
from openprogram import Agent
from openprogram.runtime.agent import llm

class ExampleAgent(Agent):
    method_options = {'summarize': {'tool': True}}

    def summarize(self, text: str) -> str:
        """Summarize a text in one sentence, preserving the core point."""
        return llm([{"type": "text", "text": (
            f"Summarize in one sentence, preserving the core point:\n\n{text}"
        )}])

_example_agent = ExampleAgent()
summarize = _example_agent.summarize
```

docstring 作为描述性上下文随调用传递；`llm(...)` 在环境 Runtime 上发起一次模型调用。对调用者来说，`summarize(article)` 就是普通 Python 函数。

### `Runtime`

打包对话历史，调用 Provider（Anthropic、OpenAI、Claude Code 等），把结果写回上下文。编排用 `llm()`；`Runtime.exec()` 是其下层的嵌入 API。

### `Context`

| 规则 | 行为 |
| --- | --- |
| DAG | 每次 Agent 执行拥有一个。对话记录用户消息、模型请求和直接派发的工具；普通帮助函数不产生持久节点。 |
| 另一个 Agent | 独立 DAG；父级保存结果和 `child_session_id` 引用 |
| 归属 | 权限、取消和文件检查点仍属于发起的执行 |
| `llm()` 历史 | 从 DAG 渲染，所以上下文是在函数上声明的属性 |
| `expose` | 调用完成后向父级暴露什么。`"io"`（名称、输入、输出；工具默认），普通方法默认 `"full"`。 |
| `render_range` | 函数自身的调用读取多少历史。`{"callers": 0}` 得到隔离的草稿上下文。 |

同一个 DAG 也是调试视图：可视化、Token 统计、回放失败路径。

## 衍生概念

| 概念 | 含义 |
| --- | --- |
| LLM 也写代码 | Agent 用普通文件编辑编写函数，不需要专门的 `create()` 或 `fix()`。通过 `openprogram programs check` 后，worker 无需重启即可从 `~/.openprogram/programs/` 加载。代码是数据，LLM 是编译器，函数是产物。 |
| 双模式 | 既是可导入、手写的库，也是在 CLI 或 WebUI 里提需求的产品。得到的是可读的 Python，可以逐步理解。 |

## 与传统框架对比

| 场景 | LangChain、AutoGPT | Agentic Programming |
| --- | --- | --- |
| 抓取 10 个页面并逐个摘要 | Agent 决定顺序和并行 | `for url in urls: summarize(fetch(url))` |
| 跨 3 次对话记住上下文 | 记忆库，每次查询 | 一个 Python 局部变量 |
| 让 LLM 决定调用哪个工具 | 函数调用加 agent loop | `agent(prompt, tools=[...])` 或 `decision.make(prompt, options)` |
| 出错重试 | Agent 自行决定 | `try/except` 和代码门控：校验拦下无效选择，模型重新决定 |

Agent 框架适合完全开放、目标模糊的任务。

**范式在代码中的位置**

| 路径 | 内容 |
| --- | --- |
| `openprogram/runtime/agent/` | 引擎 |
| `openprogram/runtime/context/` | 扁平 DAG 上下文 |
| `openprogram/models/` | LLM Provider |
| `openprogram/programs/` | 按此范式写成的工具和应用 |
| `apps/web/` | 不写代码也能运行的 Web UI |

[论文](https://arxiv.org/abs/2606.15874)，KDD 2026 AgenticSE workshop · [开始使用](https://fzkuji.github.io/OpenProgram/docs/start/GETTING_STARTED.zh.html) · [API 参考](https://fzkuji.github.io/OpenProgram/docs/reference/API.zh.html) · [设计](https://fzkuji.github.io/OpenProgram/docs/reference/design/README.zh.html)
