Agent架构选型:三种运行模式与推理成本权衡
Decoding AI’s Open-Source Course Maps Three Ways to Run an Agent Loop and the Provider Economics Behind Each
Agent开发者必读,清晰拆解了不同场景下的架构取舍与成本模型,直接指导基础设施选型。
Most teams treat ‘which model’ as the important decision. The harness engineering literature keeps pointing somewhere else. In LangChain’s Terminal-Bench experiment, changing only the harness—same model throughout—moved a coding agent from roughly 30th place into the top 5.
大多数团队将‘选择哪个模型’视为关键决策。但 Harness 工程文献一直指向其他方向。在 LangChain 的 Terminal-Bench 实验中,仅更改 harness(全程使用同一模型),就将一个编码代理从大约第 30 名提升到了前 5 名。
That result reframes the question. If the harness decides quality, then how you run the loop becomes an architecture decision, not a deployment detail. Paul Iusztin’s open-source course Building a Coding Agent From Scratch builds a Python agent called Decode. Published through Decoding AI, it separates three run modes. Each mode has a different latency profile. Each one therefore wants a different inference provider.
这一结果重构了问题的视角。如果 harness 决定了质量,那么你如何运行循环就成为一个架构决策,而非部署细节。Paul Iusztin 的开源课程《从零构建编码代理》(Building a Coding Agent From Scratch)构建了一个名为 Decode 的 Python 代理。该课程通过 Decoding AI 发布,区分了三种运行模式。每种模式具有不同的延迟特征。因此,每种模式都需要不同的推理提供商。
One headless core, three shapes
一个无头核心,三种形态
The center of the system is a headless harness with no interface of its own. Inside it runs the agent loop every harness shares: the LLM picks an action, a tool executes, the observation feeds back. Everything reads from and writes to the context window.
系统的中心是一个无接口的无头 harness。其中运行着所有 harness 共享的代理循环:LLM 选择动作,工具执行,观察结果反馈回来。所有内容都从上下文窗口读取并写入。
The agent itself is small. In Decode it is a ~20-line Pydantic AI definition composing a model, tools, and an output type. In Claude Code’s leaked source, the core loop is roughly 150 lines. Everything else—memory, skills, sandbox, permissions, LSP feedback, compaction—is harness.
代理本身很小。在 Decode 中,它是一个约 20 行的 Pydantic AI 定义,组合了模型、工具和输出类型。在 Claude Code 泄露的源代码中,核心循环大约为 150 行。其余一切——记忆、技能、沙箱、权限、LSP 反馈、压缩——都属于 harness。
Interfaces then plug into that core. That is where the three modes appear:
接口随后接入该核心。这就是三种模式出现的地方:
Mode 1: Interactive, online
模式 1:交互式,在线
A terminal UI is wired to one live session, in memory, in the same process. Events stream back through async generators as tokens arrive.
终端 UI 连接到一个实时会话,位于内存中,且在同一进程内。随着 token 到达,事件通过异步生成器流式返回。
The hard problem here is steering. If you type while a tool call is in flight, injecting the message immediately corrupts the turn. Decode’s answer is a steering queue plus a priority gate. Input is buffered on arrival and injected only at a safe boundary. The loop exposes two: MODEL_REQUEST, before the next model call, and WOULD_STOP, when the turn would end.
这里的难点在于引导(steering)。如果你在工具调用进行中输入消息,立即注入会导致当前回合损坏。Decode 的解决方案是引导队列加上优先级门控。输入在到达时被缓冲,仅在安全边界处注入。循环暴露了两个时机:MODEL_REQUEST(在下一次模型调用之前)和 WOULD_STOP(当回合即将结束时)。
Three input modes map onto that. Plain Enter steers within the turn. Alt+Enter queues a follow-up until the turn stops. Esc triggers a cooperative abort at the next boundary, clearing both queues so history stays intact.
三种输入模式映射到上述机制。普通 Enter 在当前回合内进行引导。Alt+Enter 将后续输入排队,直到回合停止。Esc 触发在下一个边界的协作中止,清空两个队列,从而保持历史记录完整。
A human is reading every token. This mode is latency-bound, which is why it belongs on a low-latency hosted API.
人类正在阅读每一个 token。此模式受延迟限制,因此它适合部署在低延迟的托管 API 上。
Mode 2: Remote, offline
模式 2:远程,离线
Remote mode keeps the harness headless and runs it on a server through an agent runtime. Decode uses Kitaru, ZenML’s agent runtime, deployed to GCP, with the agents themselves executing on Modal.
远程模式保持 harness 无头,并通过代理运行时在服务器上运行它。Decode 使用 Kitaru(ZenML 的代理运行时),部署在 GCP 上,而代理本身则在 Modal 上执行。
Nobody is watching. A backlog of tickets fans out to N harnesses in parallel, each producing its own PR. Because the runtime records progress step by step, a sandbox that dies mid-task resumes from its last recorded step instead of restarting. A run that pauses for human input freezes and consumes no compute while it waits.
无人观看。积压的工单并行分发至 N 个 harness,每个都生成自己的 PR。由于运行时逐步记录进度,中途崩溃的沙箱会从最后记录的步骤恢复,而不是重新开始。需要人工输入的暂停运行会冻结,并在等待期间不消耗任何计算资源。
Tools execute inside Modal Sandboxes remotely, Docker locally. The metric that matters is throughput per dollar, not time-to-first-token.
工具在 Modal Sandboxes 中远程执行,或在本地 Docker 中执行。关键指标是每美元的吞吐量,而非首 token 时间。
Mode 3: Async, online
模式 3:异步、在线
The third shape sits between the two. A live session hands work to a job queue and returns immediately. Background workflows fan out LLM calls and post results back later.
第三种形态介于两者之间。实时会话将工作交给作业队列并立即返回。后台工作流并行发出 LLM 调用,随后回传结果。
The user is online but not watching each step. The queue owns the work, so the run outlives the client that started it. This is the pattern behind Slack-triggered agents and background PR review, and it bills like batch, not like chat.
用户在线但未观察每一步。队列拥有工作所有权,因此运行生命周期长于启动它的客户端。这是 Slack 触发代理和后台 PR 审查背后的模式,其计费方式类似批处理,而非聊天。
The interactive explainer
交互式解释器
Why the provider changes with the mode
为何提供者随模式变化
The cost model follows the latency requirement, and the gap is large.
成本模型遵循延迟要求,且差距巨大。
Take 1,000 documents at 30,000 input tokens each, roughly 500 output tokens per document. At frontier API rates of $3 per million input and $15 per million output, the lesson's arithmetic lands near $97. Prompt caching does not rescue it, because every document is a different prefix. Batched on a serverless GPU at around 3,000 tokens per second, the same work is under three hours of GPU time—roughly $13.
以 1,000 份文档为例,每份 30,000 输入 token,每份约 500 输出 token。按前沿 API 费率(每百万输入 3 美元,每百万输出 15 美元)计算,课程算术得出的费用接近 97 美元。提示缓存无法挽救这一局面,因为每份文档都有不同的前缀。在服务器端 GPU 上以每秒约 3,000 token 的速度批量处理,相同工作仅需不到三小时的 GPU 时间——大约 13 美元。
The reverse case is just as sharp. Decode's default test model, Qwen3.6 35B, runs on a single H200. Modal's published pricing lists H200 SXM at $0.001261 per second, or about $4.54 per hour. Leave an interactive agent idle overnight waiting on a y confirmation, and ten idle hours add roughly $45 to the bill.
反向情况同样尖锐。Decode 的默认测试模型 Qwen3.6 35B 运行在单个 H200 上。Modal 公布的定价显示 H200 SXM 为每秒 0.001261 美元,或每小时约 4.54 美元。让交互式代理整夜空闲等待 y 确认,十个空闲小时会使账单增加约 45 美元。
That is the whole argument. Interactive work pays per token because a human is waiting. Offline and async work pays per GPU-hour because throughput is the objective and idle time is the enemy.
这就是整个论点。交互式工作按 token 付费,因为有人在等待。离线和异步工作按 GPU 小时付费,因为吞吐量是目标,而空闲时间是敌人。
There is a second axis: serverless versus reserved capacity. Modal's pricing analysis reduces it to one comparison. Reservations charge the peak rate for the whole contract; serverless follows the demand curve. When the peak-to-average ratio exceeds the reservation discount, serverless is cheaper. Modal reports typical discounts of 2–5× against peak-to-average ratios of 5–10× for inference, training, and agentic development. Industry surveys it cites put reservation utilization below 30%, often under 10%.
还有第二个维度:无服务器与预留容量。Modal 的成本分析将其简化为一次比较。预留容量在整个合同期内按峰值费率收费;无服务器则跟随需求曲线。当峰值与平均值之比超过预留折扣时,无服务器更便宜。Modal 报告典型折扣为 2–5 倍,而推理、训练和代理开发的峰值与平均值之比为 5–10 倍。它引用的行业调查显示预留利用率低于 30%,通常低于 10%。
Key Takeaways
关键要点
- Harness beats model: swapping only the harness moved an agent from ~30th to top 5 on Terminal-Bench.
- Interactive mode is latency-bound and steers via a queue draining at MODEL_REQUEST and WOULD_STOP boundaries.
- Remote and async modes are throughput-bound, so GPU-hour billing beats per-token billing at volume.
- 1,000 documents cost ~$97 on frontier API rates versus ~$13 of batched GPU time.
- Serverless wins whenever peak-to-average demand exceeds the reservation discount, typically 5–10× against 2–5×.
- Harness 胜过模型:仅更换 harness 就使代理在 Terminal-Bench 上的排名从第 30 名左右跃升至前五。
- 交互模式受延迟限制,通过队列进行引导,队列在 MODEL_REQUEST 和 WOULD_STOP 边界处排空。
- 远程模式和异步模式受吞吐量限制,因此在大规模用量下,按 GPU 小时计费比按 token 计费更具优势。
- 根据前沿 API 费率,1,000 份文档的成本约为 97 美元,而批量 GPU 时间成本约为 13 美元。
- 每当峰值与平均需求之比超过预留折扣时,无服务器架构就能胜出,通常这一比例为 5–10 倍,而预留折扣通常为 2–5 倍。
Sources:
来源:
- Building a Coding Agent From Scratch (Lesson 1)
- The Bare-Bones Coding Agent Loop (Lesson 2)
- From a Raw Shell to a Sandboxed Coding Agent (Lesson 3)
- Course repository · Modal pricing
- How to price serverless GPUs
- LangChain: The anatomy of an agent harness
- 从零开始构建编码代理(第一课)
- 极简编码代理循环(第二课)
- 从原始 Shell 到沙盒化编码代理(第三课)
- 课程仓库 · Modal 定价
- 如何为无服务器 GPU 定价
- LangChain:代理 harness 的解剖结构
The post Decoding AI’s Open-Source Course Maps Three Ways to Run an Agent Loop and the Provider Economics Behind Each appeared first on MarkTechPost.
MarkTechPost 首发文章《解码 AI 开源课程:映射运行代理循环的三种方式及其背后的提供商经济》
更进一步:量化金融体系
看懂新闻只是起点——沿量化金融路径,把它变成能交付的工程能力