固定流程与自适应分解
已知步骤使用提示词链;开放式任务使用自适应分解。
固定流程与自适应分解 是 CoddyKit 上的免费 Claude Architect 课时。 这是第 1 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 Claude Architect 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 Claude Architect 课程共包含 4 节课。
本课时的部分内容尚未翻译,以英文显示。
Two Ways to Break Down Work
When a task is too big for a single prompt, you decompose it. There are two fundamental strategies, and choosing the right one is a core architecture skill.
- Fixed pipeline (prompt chaining): you hard-code a known sequence of steps. Step 1 feeds Step 2 feeds Step 3.
- Adaptive decomposition: the model decides what to do next based on what it discovers, looping until the goal is met.
The rule of thumb: fixed pipelines for known sequential steps; adaptive for open-ended investigations. This lesson makes that decision precise.
Fixed Pipeline = Prompt Chaining
A fixed pipeline is just prompt chaining: you orchestrate a deterministic sequence in your own code. You know the steps in advance, so you wire them up explicitly. Each call does one focused job, and its output becomes the next call's input.
Because you control the order, the flow is predictable, debuggable, and cheap to reason about. There is no model-driven branching about what to do next — only the work inside each step.
# Fixed pipeline: extract -> classify -> summarize
from anthropic import Anthropic
client = Anthropic()
def step(system, user):
r = client.messages.create(
model="claude-sonnet-4-5",
max_tokens=1024,
system=system,
messages=[{"role": "user", "content": user}],
)
return r.content[0].text
entities = step("Extract entities as JSON.", document)
category = step("Classify this record.", entities)
summary = step("Write a one-line summary.", category)When a Fixed Pipeline Wins
Reach for a fixed pipeline when the work has a stable, known shape:
- The steps are the same every run (extract, then validate, then format).
- Order matters and rarely changes.
- You want predictable cost, latency, and easy debugging.
- Each stage has a clear, narrow responsibility.
Classic example: structured data extraction. Extract fields, then run a validation/retry pass, then attach provenance. You always do those three things, in that order — so chain them.
Adaptive Decomposition = Model-Driven
Adaptive decomposition hands the planning to the model. You give it tools and a goal; it inspects results and chooses the next action. This is the agentic loop: send a request, inspect the stop_reason, and if it's tool_use, run the tools, append results to history, and repeat until end_turn.
You cannot pre-wire the path because you don't know it in advance — it depends on what each step reveals.
# Adaptive: the model decides which tool to call next
while True:
resp = client.messages.create(
model="claude-sonnet-4-5",
max_tokens=2048,
tools=tools,
messages=messages,
)
if resp.stop_reason == "tool_use":
results = run_tools(resp) # execute requested tools
messages.append({"role": "assistant", "content": resp.content})
messages.append({"role": "user", "content": results})
continue
break # terminate on stop_reason, never by parsing textWhen Adaptive Decomposition Wins
Choose adaptive decomposition for open-ended investigations where the path can't be known ahead of time:
- Debugging from a vague symptom — you grep, read, then grep usages based on what you find.
- Multi-source research where follow-up queries depend on earlier answers.
- Customer support where the next action depends on what a lookup returns.
Here the branching IS the work. A rigid pipeline would either miss paths or explode into unmaintainable conditionals.
Terminate on stop_reason, Not on Text
The single most common adaptive-loop mistake on the exam: ending the loop by scanning the model's text for words like "done" or "finished." Don't. Decisions are model-driven; terminate on the API stop_reason.
The loop ends when stop_reason is end_turn. While it's tool_use, you keep going. An iteration cap is a safety net only — never the primary stop mechanism. Reserve hard-coded control for guarantees, not for deciding when the work is conceptually complete.
# WRONG: parsing text for completion
if "done" in resp.content[0].text.lower():
break
# RIGHT: drive on stop_reason; cap is only a safety net
MAX_STEPS = 25
for _ in range(MAX_STEPS):
resp = client.messages.create(...)
if resp.stop_reason != "tool_use":
break # end_turn / max_tokens / stop_sequenceHybrid: Pipelines Containing Adaptive Stages
Real systems mix both. The outer flow is often a fixed pipeline, but one stage may be an adaptive agentic loop. You get the predictability of chaining at the macro level and flexibility where you genuinely need it.
Example: an extraction pipeline whose middle stage is an adaptive "resolve ambiguities" agent that may call lookup tools an unknown number of times — wrapped by a deterministic validate-and-format stage before and after.
# Outer = fixed chain; one stage = adaptive loop
raw = extract_stage(document) # fixed
resolved = adaptive_resolver(raw) # agentic loop inside
final = validate_and_format(resolved) # fixedMulti-Pass Code Review: A Decomposition Choice
Decomposition isn't only about pipelines vs loops — it's also about how you split a task to protect the model's attention. Multi-file code review is the canonical case.
A single pass over many files dilutes attention and misses both local bugs and integration issues. The fix is a deliberate decomposition: a per-file local pass first, then a separate cross-file integration pass. Two focused passes beat one unfocused pass.
# Decompose review into focused passes
for f in changed_files:
review_local(f) # pass 1: per-file, focused
review_cross_file(changed_files) # pass 2: integration concernsCoordinators Decompose Adaptively
In a hub-and-spoke multi-agent system, the coordinator decomposes the goal, delegates to subagents, then aggregates. This is adaptive decomposition at the orchestration layer — the coordinator decides how to split based on the request.
Critical: subagents do not inherit the coordinator's conversation history. Every subagent prompt must carry its full context explicitly. Multiple Task calls in one response run in parallel — useful when sub-problems are independent.
# Coordinator delegates with explicit, self-contained context
subagent_prompt = f"""You are researching ONE sub-topic.
Context (do not assume shared history):
Goal: {goal}
Sub-topic: {subtopic}
Constraints: {constraints}
Return findings with source citations."""Sessions: Resume, Fork, or Start Fresh
Adaptive work often spans sessions. Claude Code gives you --resume <name> to continue a named session and fork_session to branch from a shared point and explore alternatives in parallel.
But beware: resumed tool results can be stale if the codebase changed since. Sometimes a fresh session seeded with a structured summary beats resuming — you get current state plus the distilled context, without dragging outdated tool output along.
# Continue a named session
claude --resume refactor-auth
# Branch to explore an alternative path
# fork_session creates a sibling from the shared point
# When code moved on, prefer a fresh session + summary
claude -p "Here is a structured summary of prior work: ..."A Decision Checklist
Before you build, ask:
- Do I know the steps in advance, in a fixed order? -> fixed pipeline / prompt chaining.
- Does the next step depend on what earlier steps reveal? -> adaptive agentic loop.
- Mostly fixed with one uncertain stage? -> hybrid: chain the outer flow, embed an adaptive loop.
- Reviewing many files? -> decompose into per-file then cross-file passes.
And always: terminate adaptive loops on stop_reason, with iteration caps only as a safety net.
Quick Check
A scenario-based decision on choosing a decomposition strategy.
Recap
Key takeaways:
- Fixed pipeline (prompt chaining) for known, ordered steps — predictable, debuggable, cheap.
- Adaptive decomposition for open-ended investigations where the next step depends on what you discover.
- Hybrid is common: a fixed outer flow with an adaptive stage embedded.
- Always terminate adaptive loops on
stop_reason(end_turn); iteration caps are a safety net, never the primary stop. Never parse text for "done." - For multi-file review, decompose into a per-file pass then a separate cross-file pass.
- Subagents don't inherit history — pass context explicitly. Resume sessions carefully; a fresh session with a structured summary can beat stale resumed tool results.
用 AI 导师学习 Python — 免费
在浏览器中编写并运行真实代码,获得全天候 AI 导师的即时帮助,并在网页或应用中继续学习。
- 课程
- 26
- 课程
- 104
常见问题解答
「固定流程与自适应分解」课时是免费的吗?
是的 — 「固定流程与自适应分解」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 Claude Architect 课程的其余内容,请升级到 CoddyKit PRO。 Claude Architect 课程共包含 4 节课。
「固定流程与自适应分解」这节课中我会学到什么?
已知步骤使用提示词链;开放式任务使用自适应分解。 你通过在浏览器中直接运行的动手代码来练习 Claude Architect,全天候 AI 导师会在你学习这节课的过程中回答你的问题。
学习 Claude Architect 需要有经验吗?
无需任何先前经验。CoddyKit 上的 Claude Architect 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 1 节课,共 4 节。
「固定流程与自适应分解」课时需要多长时间?
大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。
我能在这节 Claude Architect 课中编写并运行代码吗?
能。每节 Claude Architect 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。
此课程中的所有课时
- 固定流程与自适应分解
- 多轮分解
- 会话管理
- 过时的上下文与重新开始