AI Agent Development for Beginners: Chains or Graphs First?
This article explains why chains and graphs are not competing abstractions in AI agent development but layers with different responsibilities. It covers the limits of linear chains, when graph-based orchestration becomes necessary, a four-stage learning path from a minimal model call to engineered systems, and the key roles of state design, controllability, observability, cost, and latency.
1. The Real Problem: A Misread “Either/Or”
Beginners researching AI agent development often encounter two claims. One emphasizes chaining multiple processing steps in sequence to complete complex tasks. The other emphasizes using a “graph” to describe nodes and transitions, enabling loops, branching, and human intervention. The question then becomes whether you must choose between two abstractions.
In reality, they do not sit at the same level. The first is closer to “how to organize a single call and data transformation.” The second is closer to “how to organize the overall execution flow of a task.” Treating them as mutually exclusive is the most common misconception at the beginner stage. The right question is: what capabilities does my task need in terms of data shape and control flow, and which abstraction should carry them?
2. From Chains to Graphs: Why the Abstraction Evolved
Chain-based abstractions became popular because they are extremely intuitive: several processing units are connected end to end, the output of one is the input of the next, data flows in one direction, with no backtracking and no conditional jumps. For flows like “retrieve, assemble prompt, call model, parse result,” chains have the lowest expression cost and are the easiest to debug, because there is only one execution path.
However, single-direction chains start to struggle when a task has the following characteristics:
- It needs loops: for example, repeatedly rewriting and retrieving based on validation results until a stop condition is met.
- It needs branching: for example, taking different paths based on intent classification, or deciding whether to escalate based on confidence.
- It needs human confirmation: pausing during execution and continuing after external input.
- It needs shared state: multiple steps read and write the same context rather than only passing data point to point.
- It needs recovery: resuming from a certain position after an interruption instead of rerunning from the start.
The graph abstraction is designed for these scenarios: nodes carry computation, edges carry transition conditions, and state is explicitly maintained between nodes. It is important to note that a graph is not a replacement for a chain but a superset of it—a graph with no branches or back edges is semantically equivalent to a chain. Therefore, there is no hard constraint on which one to learn first, but there is a practical consideration of which intuition to build first for the greatest benefit.
3. Responsibility Boundaries of the Three Abstractions
To avoid conceptual confusion, common abstractions can be divided by responsibility as follows:
- Call encapsulation layer: responsible for interacting with the model, handling prompt templates, message roles, parameters, and response parsing. It is the common foundation for all upper-level orchestration.
- Linear orchestration layer: the chain. It combines multiple steps into a reusable unit and suits deterministic processing with a single path.
- State orchestration layer: the graph or state machine. It handles nodes, edges, shared state, and execution position, and suits flows with loops, branching, pausing, and recovery.
The three are nested layer by layer rather than competing with each other. Ignore the first layer, and upper-level orchestration has nothing to build on. Know only the first layer, and when facing complex flows you will force conditional logic into prompts, making the logic uncontrollable and untestable.
4. Recommended Learning Path
For readers starting from zero, proceed through four stages: concepts, linear, state, and engineering. Each stage uses a runnable minimal system as its acceptance criterion.
Stage 1: Build call intuition. Write a minimal loop by hand: construct a prompt, make a call, parse structured output, and handle retries on failure. The goal here is not a framework but understanding the uncertainty of model output and why structured constraints are needed.
Stage 2: Master linear composition. Break the loop above into two or three steps and combine them with a chain, for example “organize material, extract key points, generate conclusion.” Deliberately practice input/output contract design, defining the fields and types of each step.
Stage 3: Move into state orchestration. Introduce a task that requires a loop, such as “generate, validate, revise,” express nodes and back edges with a graph structure, and set a maximum iteration count to prevent infinite loops. The following code is illustrative and expresses structure only, not any specific implementation:
# Pseudocode: minimal graph structure for generate-validate-revisegraph = Graph(state=DraftState) # shared state: draft, review comments, iteration countgraph.add_node("draft", generate) # generation nodegraph.add_node("review", validate) # validation nodegraph.add_node("revise", revise) # revision nodegraph.add_edge(START, "draft")graph.add_edge("draft", "review")graph.add_conditional_edges( # conditional edge: continue revising or finish"review",route=lambda s: "revise" if s.need_fix and s.iter < 3 else END)graph.add_edge("revise", "review") # back edge forms the loopapp = graph.compile()
Stage 4: Engineering. Add logging and tracing, timeouts and retries, concurrency and rate limiting, and cost and quota control, turning “it runs” into “it is operable.”
5. State Design Is the Dividing Line
Beginners often focus on the number of nodes, but the real difficulty lies in state. State design must answer four questions: which fields are shared context and which are temporary variables internal to a node; which fields need to accumulate (such as message history) and which need to be overwritten (such as the latest review comments); whether state needs to be persisted to support interruption and recovery; and how to trim state when it grows, to avoid unlimited context growth that drives up cost.
A practical rule is to keep state as flat and serializable as possible, with single-purpose fields; avoid putting non-serializable objects into state; and explicitly record iteration counts and termination reasons in loop flows for easier troubleshooting.
6. Controllability and Observability
For flows with loops and branches, the biggest risk is unpredictable behavior. Engineering should constrain this in three ways. First, set hard limits structurally, such as maximum iteration count and maximum node execution time. Second, require key nodes to output structured results so programs can make decisions rather than relying on natural language. Third, record the source and target of each transition, state snapshots, and elapsed time to form a replayable execution trace. Without the third point, problem diagnosis relies entirely on guesswork.
7. Cost, Latency, and Capacity
Multiple rounds of loops mean more calls, which amplify cost and latency. Practical approaches include: pushing deterministic logic down into ordinary code rather than model calls; not upgrading to a stronger model for tasks a smaller model can handle; reusing cacheable intermediate results; and relying on general cloud capabilities in the architecture, such as using message queues for peak shaving and asynchronous decoupling, object storage for intermediate artifacts, container platforms for elastic scaling, and logging services with monitoring and alerting to establish a baseline. If you use relevant general capabilities from Baidu AI Cloud, follow the same order of “design contracts first, then connect infrastructure” to avoid binding to implementation details too early.
8. Conclusion
Back to the original question: there is no need to choose between chains and graphs. Chains are essential fundamentals, and graphs are a necessary extension for complex flows; both share the same call foundation. For beginners, the optimal strategy is to first build intuition about model uncertainty with a minimal loop, then master contract design with linear composition, then understand state and transitions with a small task involving a loop, and finally add observability and cost governance. The acceptance criterion for this path is always clear: every step can run, be tested, and be diagnosed independently.
