Loop、Graph 與 Harness:AI 智能體如何實際運作
Loop vs Graph vs Harness: How AI Agents Actually Work
Loop 讓模型選擇智能體下一步,Graph 由程式按預設流程控制路徑,而 Harness 則提供編排、工具、記憶、規則及日誌等運作所需組件。文章以延誤訂單及退款為例,指出實際系統通常可用 Graph 管理業務流程,並在需要推理的步驟內運行 Loop。
If you're starting to build AI agents, you'll hear three words a lot: loop, graph and harness.
They sound complicated. They're not. In this article I'll explain all three with sticky-note diagrams. No code needed yet: that comes in Part 2.
We'll use one example the whole way through:
A customer asks: "Where's my order #123? If it's late, I want a refund."
Part 1: Two ways to control an agent
Every agent has to answer one question at every step:
What happens next, and who decides?
There are two answers: the loop and the graph.
🔁 The Loop: "Here are the tools. Figure it out."
The model goes around a circle:
- Think: the model reads the request and picks the next step
-
Call a tool: for example
getOrder(123)orgetTracking(123) - Observe: the result goes back to the model
- Done? If not, go around again. If yes, send the answer.
For our customer, the model might check the order, see it's 5 days late, check tracking, see it arrives today, and decide a refund isn't needed. You never wrote an if/else. The model chose the steps.
That's the strength of a loop, and also the risk: it could have refunded straight away.
Building blocks of a loop:
| Block | What it does |
|---|---|
| The loop itself | Keeps calling the model until it says "done" |
| Tool executor | Runs the function the model asked for and feeds back the result |
| Stop condition | Final answer, or a maximum number of turns |
| Context manager | Keeps the conversation history from growing too big |
| Guardrails | Limits what the model can do alone ("ask before refunding") |
👉 The model picks the path. You design the environment.
🗺️ The Graph: "Here's the flowchart. Follow it."
Same customer, but this time you draw the path:
Start → Look up order → Is it late?
No → LLM writes a tracking reply → End
Yes → LLM drafts a refund offer → Manager approves → End
The model never decides to refund. Your code decides, and a manager must approve. The model only writes the friendly text inside the boxes.
Building blocks of a graph:
| Block | What it does |
|---|---|
| State | One shared object every step reads and writes (orderId, isLate, reply) |
| Nodes | The boxes: an LLM call or plain code |
| Edges | The arrows: "after A, go to B" |
| Conditional edges | Routing: isLate ? refund : tracking
|
| Checkpoints | Save progress after each step, so you can pause or resume |
| Human-in-the-loop | Pause for a person to approve |
👉 You pick the path. The model works inside the boxes.
Which one should you use?
| Loop | Graph | |
|---|---|---|
| Who decides the next step? | The model | Your code |
| Flexible on new kinds of tasks | ✅ High | ❌ Only the paths you drew |
| Predictable and auditable | ❌ Harder | ✅ Easy |
| Upfront design work | Low | High |
| Best for | Debugging, research, open-ended assistants | Payments, approvals, compliance |
In real systems you usually use both: a graph for the business process, with loops running inside the steps that need real reasoning.
My rule of thumb:
Build the loop first. Then ask: "Which decision would I be nervous letting the AI make alone?" Turn that decision into a graph edge.
Part 2: So what is a "harness"?
The model by itself can only take text in and give text out. It can't run tools, remember anything or stop itself. Something has to do all of that around it.
That something is the harness.
Think of a very smart new employee on day one:
- The model is the new hire. They can read, think and write very well. But they have no laptop and no logins, and they forget everything after each conversation.
-
The harness is everything the company gives them so they can work:
- Orchestration: decides the next step. This is where the loop or graph from Part 1 lives.
-
Tools: a laptop and system logins, which in code means functions like
getOrder()andrefund() - Memory: the ticket history, so they remember what the customer already said
- Rules: company policy, such as "a refund over $100 needs approval"
- Logs: an audit trail of every action
Agent = Model + Harness
Harness = orchestration (loop, graph or both) + tools + memory + rules + logs
A common shortcut is "the harness is the loop". That's too narrow. The loop or graph is the skeleton, but tools, context, memory and guardrails are where most of the real engineering happens. The same model in two different harnesses can behave like two completely different products.
Cheat sheet
- Model: the brain. Rented.
- Harness: everything around the model that turns it into an agent.
-
Orchestration: the part of the harness that decides the next step.
- Loop: the model decides. "Here are the tools, figure it out."
- Graph: your code decides. "Here's the flowchart, follow it."
Loop = tools + a circle + a stop sign
Graph = state + boxes + arrows
Agent = model + harness
Which approach are you using in your agents right now: loop, graph or both? Let me know in the comments 👇
Next in this series: we'll build this harness in real Java code with Spring AI, then look at who builds it for you on Azure.
Code & Learn
來源:DEV Community · dev.to


