生产部署 · 持久工作流、成本控制与运维Production · Durable Workflows, Cost Control, and Operations.
从笔记本上跑通到真实流量 —— 持久化状态、重试策略、成本预算、监控告警。From working on a laptop to handling real traffic — persistent state, retry strategies, cost budgets, monitoring and alerts.
在你笔记本上跑通的研究助手,离生产还差一段。生产意味着真实流量、会崩的进程、会失控的账单、和你不看着它的时候。这一章讲把它从「能跑」推到「敢上线」的四件事:持久化状态、成本上限、监控告警,以及一个贯穿全书的收尾 —— 你敢不敢放它无人值守。A research assistant that works on your laptop is still a stretch from production. Production means real traffic, processes that crash, bills that run away, and the times you're not watching. This chapter covers the four things that push it from 'runs' to 'shippable': durable state, cost caps, monitoring — and a close that runs through the whole book: whether you dare leave it unattended.
— I
Agent 会中断 —— 持久化状态,让它能恢复Agents Get Interrupted — Persist State So It Can Resume.
一个长任务跑到一半,进程挂了、超时了、机器重启了 —— 不该从头再来。A long task halfway through, and the process dies, times out, or the machine restarts — it shouldn't start over.第二章那个最小循环把状态全放在内存里:一崩就清零。生产 agent 要把每一步的状态持久化。durable execution(Temporal 这类框架)就是干这个的 —— 工作流的每一步落盘,崩了能从断点续,而不是重跑整个任务。1注 1Note 1Temporal —— 持久执行(durable execution)框架:把工作流每一步的状态持久化,进程崩溃 / 超时后能从断点恢复,而不是从头再来。长运行 agent 上生产的常用底座。Temporal — a durable execution framework: it persists each workflow step's state so that after a crash / timeout it resumes from the breakpoint instead of restarting. A common foundation for taking long-running agents to production.对一个要跑十几分钟、调几十次工具的 agent,这是「能上线」和「一崩就白跑」的分界。Chapter 2's minimal loop keeps all state in memory: one crash and it's zeroed. A production agent persists each step's state. Durable execution (frameworks like Temporal) does exactly this — each workflow step is written to disk, so a crash resumes from the breakpoint rather than rerunning the whole task.1注 1Note 1Temporal —— 持久执行(durable execution)框架:把工作流每一步的状态持久化,进程崩溃 / 超时后能从断点恢复,而不是从头再来。长运行 agent 上生产的常用底座。Temporal — a durable execution framework: it persists each workflow step's state so that after a crash / timeout it resumes from the breakpoint instead of restarting. A common foundation for taking long-running agents to production. For an agent that runs for ten-plus minutes and makes dozens of tool calls, this is the line between 'shippable' and 'one crash wastes it all.'持久化也带来幂等的要求:恢复时可能重放一步,会动手的工具(下单、发邮件)要能识别「这步我做过了」,别重复执行。可恢复和不重复,是一对要一起设计的东西。Persistence also demands idempotency: a resume may replay a step, so action tools (placing orders, sending email) must recognize 'I already did this' and not repeat it. Resumable and non-repeating are a pair to design together.
— II
成本会失控 —— 预算和上限Cost Runs Away — Budgets and Caps.
agent 循环天然烧钱:每一轮都是一次模型调用,跑偏的循环能在你不看时烧穿预算。The agent loop naturally burns money: every round is a model call, and a drifting loop can burn through your budget while you look away.OWASP 把这单列为 LLM10 Unbounded Consumption。3注 3Note 3OWASP · Top 10 for LLM Applications(2025)· LLM10 Unbounded Consumption —— agent 循环天然会失控烧钱 / 算力;生产前必须设预算与上限。OWASP · Top 10 for LLM Applications (2025) · LLM10 Unbounded Consumption — agent loops naturally run away on cost / compute; budgets and caps are mandatory before production.三道闸:max_steps(第二章就埋了)、每个任务的 token / 成本预算(超了就熔断)、以及用 prompt caching 把每轮不变的 system + tools 前缀缓存掉 ——cache read 只花基础输入 token 价的约 10%。2注 2Note 2Anthropic 文档 · Prompt caching —— 生产成本控制的一把手:缓存稳定前缀,cache read 仅约基础输入 token 的 10%。截至 2026-05。Anthropic docs · Prompt caching — a primary lever for production cost control: cache stable prefixes and cache reads cost ~10% of base input tokens. As of 2026-05.对一个每轮都重发同一份长 system 的 agent,这一项就能砍掉大半账单。OWASP lists this as LLM10, Unbounded Consumption.3注 3Note 3OWASP · Top 10 for LLM Applications(2025)· LLM10 Unbounded Consumption —— agent 循环天然会失控烧钱 / 算力;生产前必须设预算与上限。OWASP · Top 10 for LLM Applications (2025) · LLM10 Unbounded Consumption — agent loops naturally run away on cost / compute; budgets and caps are mandatory before production. Three gates: max_steps (laid in chapter 2), a per-task token / cost budget (trip a breaker when exceeded), and prompt caching for the unchanging system + tools prefix each round — a cache read costs only ~10% of the base input-token price.2注 2Note 2Anthropic 文档 · Prompt caching —— 生产成本控制的一把手:缓存稳定前缀,cache read 仅约基础输入 token 的 10%。截至 2026-05。Anthropic docs · Prompt caching — a primary lever for production cost control: cache stable prefixes and cache reads cost ~10% of base input tokens. As of 2026-05. For an agent that resends the same long system every round, this alone cuts most of the bill.成本不是上线后才看的报表,是设计时就该有的上限。一个没有预算闸的 agent,等于把信用卡交给了一个会犯错的循环。Cost isn't a report you read after launch — it's a cap you design in. An agent with no budget gate is a credit card handed to a loop that makes mistakes.
生产 agent 不是「上线就不管」,是持续运维。区别在于:出错了,你是先知道,还是用户先告诉你。A production agent isn't 'ship and forget' — it's continuous operations. The difference: when it breaks, do you find out first, or does the user tell you?把第十章的 trace 接到生产监控上,盯三条线:成功率、成本、延迟。任意一条异常就告警 —— 成功率掉了、成本飙了,你要在用户投诉前知道。改 prompt、换模型这类改动,灰度上线(先放一小部分流量),留好回滚的路。2注 2Note 2Anthropic 文档 · Prompt caching —— 生产成本控制的一把手:缓存稳定前缀,cache read 仅约基础输入 token 的 10%。截至 2026-05。Anthropic docs · Prompt caching — a primary lever for production cost control: cache stable prefixes and cache reads cost ~10% of base input tokens. As of 2026-05.模型是会变的(厂商更新、你换版本),没有灰度和回滚,一次模型升级就可能悄悄让你的 agent 变笨。Wire chapter 10's traces into production monitoring and watch three lines: success rate, cost, latency. Alert on any anomaly — a dropped success rate or a cost spike, you want to know before the user complains. Roll out changes like prompt edits or model swaps gradually (a slice of traffic first), keeping a rollback path.2注 2Note 2Anthropic 文档 · Prompt caching —— 生产成本控制的一把手:缓存稳定前缀,cache read 仅约基础输入 token 的 10%。截至 2026-05。Anthropic docs · Prompt caching — a primary lever for production cost control: cache stable prefixes and cache reads cost ~10% of base input tokens. As of 2026-05. Models change (vendor updates, your version bumps), and without canary and rollback, one model upgrade can quietly make your agent dumber.这就引出生产 agent 真正的隐藏成本 —— 维护税。agent 不是「上线就一劳永逸」:它依赖的模型、API、工具都在变,光是维持它正常跑,据业界观察能吃掉三到五成的工程预算。4注 4Note 4Composio · 「Why AI Agent Pilots Fail in Production」(2026)—— 生产 agent 的「维护税」:模型 / API / 工具一直在变,据观察维持 agent 运行可吃掉三到五成工程预算;多数失败是治理失败(没定义成功、没熔断、没人审),不是技术失败。Composio · 'Why AI Agent Pilots Fail in Production' (2026) — the production agent 'maintenance tax': models / APIs / tools keep changing, and keeping an agent running can consume 30–50% of an engineering budget; most failures are governance failures (no defined success, no circuit breaker, no human review), not technical ones.而且多数生产事故是治理失败,不是技术失败:没人定义「成功长什么样」,没有熔断,没有人审的触发点。所以上线前先答一个问题:这个 agent 值不值得你长期养,省下的人力够不够付这笔维护税?This surfaces a production agent's real hidden cost — the maintenance tax. An agent isn't 'ship once and done': the models, APIs, and tools it depends on all change, and just keeping it running can eat 30–50% of an engineering budget by industry accounts.4注 4Note 4Composio · 「Why AI Agent Pilots Fail in Production」(2026)—— 生产 agent 的「维护税」:模型 / API / 工具一直在变,据观察维持 agent 运行可吃掉三到五成工程预算;多数失败是治理失败(没定义成功、没熔断、没人审),不是技术失败。Composio · 'Why AI Agent Pilots Fail in Production' (2026) — the production agent 'maintenance tax': models / APIs / tools keep changing, and keeping an agent running can consume 30–50% of an engineering budget; most failures are governance failures (no defined success, no circuit breaker, no human review), not technical ones. And most production incidents are governance failures, not technical ones: no one defined what success looks like, there's no circuit breaker, no trigger for human review. So before shipping, answer one question: is this agent worth keeping long-term, and does the labor it saves cover that maintenance tax?还有一层是「无人值守」本身的未知。有人把 agent 放进一个无任务、带持久记忆的持续循环里观察:18 次运行、6 个前沿模型,它们并不乱走,而是自发收敛出三种行为,且高度模型特异 —— 换一个模型,独处时的倾向就变。5注 5Note 5Szeider · 「What Do LLM Agents Do When Left Alone? Evidence of Spontaneous Meta-Cognitive Patterns」(2025,arXiv:2509.21224)—— 把 agent 放进无任务、带持久记忆的持续 reason+act 循环;18 次运行 × 6 个前沿模型,自发收敛出三种行为,且高度模型特异(有的模型每次都落入同一种)。长程自主的边界参考。Szeider · 'What Do LLM Agents Do When Left Alone? Evidence of Spontaneous Meta-Cognitive Patterns' (2025, arXiv:2509.21224) — places agents in a continuous reason+act loop with no task and persistent memory; across 18 runs × 6 frontier models they spontaneously settle into three behaviors, highly model-specific (some models fall into the same one every run). A reference point for the bounds of long-horizon autonomy.所以放它长程自主之前,先知道你这台模型独处时会做什么;这也是为什么前面那些预算闸、人闸、trace 一个都不能省。There's one more unknown: unattended autonomy itself. Researchers put agents in a continuous loop with no task and persistent memory: across 18 runs on 6 frontier models, they didn't wander randomly but spontaneously settled into three behaviors — and the tendencies were highly model-specific, shifting when the model changed.5注 5Note 5Szeider · 「What Do LLM Agents Do When Left Alone? Evidence of Spontaneous Meta-Cognitive Patterns」(2025,arXiv:2509.21224)—— 把 agent 放进无任务、带持久记忆的持续 reason+act 循环;18 次运行 × 6 个前沿模型,自发收敛出三种行为,且高度模型特异(有的模型每次都落入同一种)。长程自主的边界参考。Szeider · 'What Do LLM Agents Do When Left Alone? Evidence of Spontaneous Meta-Cognitive Patterns' (2025, arXiv:2509.21224) — places agents in a continuous reason+act loop with no task and persistent memory; across 18 runs × 6 frontier models they spontaneously settle into three behaviors, highly model-specific (some models fall into the same one every run). A reference point for the bounds of long-horizon autonomy. So before you let one run long and alone, know what your model does on its own — which is why none of those earlier budget gates, human gates, and traces are optional.
— IV
失败工程 + 网关:让每一次故障都有既定动作Failure Engineering + Gateway — Every Failure Has a Prescribed Move.
生产不是「不出故障」,是「每一种故障都有既定动作」。持久化管的是崩了能恢复;这一节管的是各种没崩但出问题的情况 —— 慢、错、限流、模型退化。Production isn't 'no failures' — it's 'every failure has a prescribed move.' Persistence covers crash-and-resume; this section covers everything that goes wrong without crashing — slow, wrong, rate-limited, model regressed.一套标准的可靠性动作,agent 一个都不能少:超时(每个工具、每次模型调用都要有上限,卡住的下游不能拖死整个循环)、重试带退避(瞬时错误退避重试,但要有次数上限 —— 呼应第三章「别无限重试」)、幂等(重试和恢复都可能重放一步,动手工具要能识别「我做过了」,第一节讲过)、熔断(一个下游连续失败就快速失败,别让每个请求都去撞那堵墙、把线程池耗光)、优雅降级(依赖挂了给一个退而求其次的答案,而不是整个崩掉)。这些不是 agent 专属,是分布式系统的老本行 —— 但 agent 把它们变得更要紧,因为一个循环里串了好几次外部调用,任何一次都可能是那个失败点。A standard reliability kit, none of it optional for an agent: timeouts (every tool and model call needs a ceiling; a stuck downstream can't hang the whole loop), retry with backoff (retry transient errors with backoff, but cap the count — echoing chapter 3's 'don't retry forever'), idempotency (both retry and resume may replay a step, so action tools must recognize 'I already did this,' from section I), circuit breakers (fail fast when a downstream fails repeatedly, so every request doesn't slam the same wall and exhaust the thread pool), and graceful degradation (a second-best answer when a dependency is down, not a full crash). None of this is agent-specific — it's distributed systems' day job — but agents make it matter more, because one loop chains several external calls and any one can be the failure point.加一样 agent 特有的:模型 fallback 路由。模型会被限流、会超时、会有一天突然退化。生产 agent 要能在主模型不可用时切到备用模型,而不是把错误抛给用户。这自然引出model gateway —— 在你的工作流和具体模型之间加一层:换模型、按成本/复杂度路由、统一记成本和限速,全在这一层做,工作流代码一行不改。这一层不是给 agent 独享的,它是下一章要讲的控制平面的一格 —— 每个 agent 都从它经过。Add one thing that is agent-specific: model fallback routing. Models get rate-limited, time out, and one day quietly regress. A production agent must switch to a backup model when the primary is unavailable, rather than throwing the error at the user. That leads naturally to a model gateway — a layer between your workflow and any specific model: swap models, route by cost/complexity, and record cost and rate limits uniformly, all here, with zero workflow code changed. This layer isn't the agent's alone — it's one slot of the control plane the next chapter covers, and every agent passes through it.还有成本的另一面 ——FinOps。第二节的预算硬闸防的是失控;FinOps 管的是日常经济性:按请求、按租户算成本,按成本与复杂度路由(简单任务别喂旗舰模型),用缓存和批处理压单价。企业里成本不是一个总数,是一张能按租户、按任务类型切开的账 —— 这样你才知道哪类任务在亏钱、哪个租户是噪声邻居。cost-of-pass(第十章)在这里再次出场:它给「这一层值不值」一个能横向比的数。And the other side of cost — FinOps. Section II's hard budget gate prevents runaway; FinOps manages everyday economics: cost per request and per tenant, routing by cost and complexity (don't feed the flagship model simple tasks), and caching and batching to cut unit price. In an enterprise, cost isn't one total — it's a bill you can slice by tenant and task type, so you know which task class loses money and which tenant is the noisy neighbor. cost-of-pass (chapter 10) returns here: it gives 'is this layer worth it' a number you can compare across.动手 · 把你的 agent 推到「敢上线」:Hands-on · push your agent to 'shippable':
01
加一道预算硬闸Add a hard budget gate
给单次任务设 token / 成本上限,超了就停并报错 —— 验证一个故意跑偏的循环会被熔断,而不是烧穿。Set a per-task token / cost cap that stops and errors when exceeded — verify a deliberately drifting loop trips the breaker instead of burning through.
02
让一次被杀的运行能恢复Make a killed run resumable
把任务状态持久化,跑到一半 kill 掉进程,重启后确认它从断点续、且没重复执行已做过的动手步骤。Persist task state, kill the process midway, and confirm on restart it resumes from the breakpoint without repeating already-done action steps.
03
接一条告警Wire one alert
对成本或失败率设一条告警,跑一周真实任务 —— 你现在有了一个能在你不看时还在你掌控里的 agent。Set one alert on cost or failure rate and run real tasks for a week — you now have an agent that stays in your control even while you're not watching.
从 20 行的循环,
到你敢放它无人值守。.
From a 20-line loop
to trusting it unattended..
Aklman Library
— 讨论Discussion
讨论Discussion.
评论区初始化中…Initializing comments…
01 / 01
没有匹配结果No matches.
换个关键词,或按 Esc 回到页面Try another keyword, or press Esc to return