匠人学院 JR Academy
Skip to article
Engineering handbook
AI Agent Engineering Handbook

Start with tool calls. Understand how an agent acts, reads observations and stops when something fails. Verify each boundary with complete code and offline tests.

5 chapters·16 guides·Open access
LangGraph: State Machines and Loops
On this page

LangGraph for Agent State and Control Flow

A graph makes the agent's next step explicit: continue, retry, request review or finish. It is useful when the workflow needs stateful branches rather than a single prompt-response call.

#State, nodes and edges

State contains the objective, relevant observations, artifacts and current status. A node reads state and returns updates. An edge selects the next node, either unconditionally or through routing logic.

For a coding assistant, a useful graph is:

text
read task -> inspect files -> propose patch -> validate patch | pass -> review -> finish | fail -> revise, if budget remains | exhausted -> report blocker

The diagram is a design example. A box named “validate” must execute a real check; a model saying the code looks correct does not replace tests.

#Make the loop bounded

Store the attempt count and check it in the router. Add a global time and usage budget as well as a graph recursion limit. Do not let a failing quality check cause unlimited regeneration.

Use typed state and clear node contracts. If several nodes update the same field concurrently, define how updates combine. Keep a failure distinct from a successful node that returned an empty result.

#Pause and resume safely

A checkpointer records graph state. An in-memory implementation is for development; restart recovery needs durable storage. Authorize access to the thread before loading its state.

Pause before consequential actions with the proposed operation visible to the reviewer. Resume with the intended decision, then recheck any data that may have changed. An external operation can succeed before checkpointing fails, so action handlers still need idempotency and reconciliation.

For a deterministic, runnable graph example without a model provider, see the LangGraph tutorial.

Official Graph API↗

System Design

Core system design concepts and practical case studies

Learn the trade-offs and patterns that matter in technical interviews.

Open System Design →

FAQ

Which language should I use?
Python and TypeScript both work. Start with the language you can debug; the first exercise uses Python 3.10+ and its standard library.
Does MCP require a specific model?
No. MCP connects applications to tools and context services. Compatibility depends on the host client, transport, protocol version and authorisation; model tool-calling support is a separate check.
Does learning agents guarantee employment?
No. Use a small project to demonstrate requirements analysis, permissions, testing and maintenance. Employment outcomes depend on the role market and your actual skills.

Done reading? Next steps