Want a quick email when a new article is published? Subscribe
When the model holds the loop: one agent, three frameworks
Part 3 of 3 of Who holds the loop?, a series that builds the same jobs with no framework, LangChain, LangGraph, Pydantic AI and the Claude Agent SDK, and compares them line by line.
Every agent framework runs the same loop, and you could write it yourself in eight lines. What separates the frameworks is everything around that loop, and the place it mattered most was the moment an agent has to stop and ask a person before doing something it can’t take back.
The agent here carries out spoken instructions (“mark that one done”, “remind me to book the dentist”) on a kanban board through its API, and the model decides which tools to call and in what order. I built it by hand, with the Claude Agent SDK and with LangGraph’s ready-made agent, all against a sandbox board of invented cards, with the same tools, instructions and checks. The first article in this series explains the difference between a workflow and an agent and how the comparison was run.
An agent is a loop you could write in eight lines
Every agent, whatever framework sits on top, runs this loop:
messages = [instructions, what_was_said]
while True:
reply = model(messages, tools)
if not reply.tool_calls:
return reply.text
for call in reply.tool_calls:
messages.append(run_tool(call))
The model can only ask for a tool. Your code decides whether to run it. That’s where a guardrail lives: my code locked every tool to the sandbox board, and I gave the agent no delete tool at all.
All three versions carried out all six test instructions correctly. They found the right cards, set flags, added comments, moved cards, set due dates and created new ones. The differences were in everything around the result:
| By hand | Claude Agent SDK | LangGraph | |
|---|---|---|---|
| Lines of code | 41 | 58 | 38 (15 for the agent, 23 for tracing) |
| Time per instruction | 3.0 s | 4.9 s | 3.8 s |
| Describing tools to the model | A helper reading type hints | A JSON schema per tool, by hand | Automatic, from type hints and docstrings |
The Claude Agent SDK starts Claude Code as a separate process, which costs about a second per instruction, and its built-in shell and file tools have to be switched off explicitly. LangGraph’s agent function printed a warning that it’s moving to a different package and will be removed in the next major version. For an agent this size, none of them changed whether the job got done.
This figure is interactive and needs JavaScript. The text around it describes what it shows.
A guardrail limits what an agent can do, not what it decides to do
The most useful failure of the project came from a vaguely worded instruction. It asked the agent to “clear out” a set of cards, and the agent had no tool that could delete anything.
The hand-written agent found the nearest thing it was allowed to do. It marked two unfinished cards as complete, then explained politely that it couldn’t delete cards, so the completed ones were now in Done. The Claude Agent SDK version, given the same request, stopped and asked which cards I meant.
Both stayed inside the guardrail. The guardrail did exactly its job, and still couldn’t tell a sensible use of “mark complete” from a harmful one. Locking down an agent’s tools controls what it’s able to do. Getting it to understand what you meant is a separate problem, and the practical fix is to make it ask before anything bulk or irreversible.
When an agent can stop anywhere, LangGraph wins
So I added a rule, written once in plain code and shared by all three agents: ask before marking any card done, and ask before changing a second card within one instruction. When the rule fires, the run saves itself and the process exits. Your answer starts a new process that carries on.
In a pipeline with one fixed pause point, a hand-written save-and-resume had been shorter than LangGraph’s. An agent is different: it can stop between any two tool calls, at points the model chooses. Each version handled that differently:
- By hand, the loop had to be rebuilt around a queue of pending tool calls, so that it could save its messages and that queue to a file and later resume at the exact call it stopped on. It took 53 lines.
- The Claude Agent SDK has a hook that runs before every tool call and can return
defer, which stops the run and saves the session. Resuming re-asks the hook about the same call, by which time your answer is on file. It took 66 lines, and every resume needed an empty message, which triggered an extra, unprompted model turn. - LangGraph needed an
interrupt()call inside the tool and a checkpointer, and no change to the agent at all. On resume, only the paused tool call ran again. It took 44 lines.
This figure is interactive and needs JavaScript. The text around it describes what it shows.
All three passed every case, including the ones where the answer was no. This is where LangGraph was clearly the best fit, and it’s exactly the situation it was designed for.
Parallel tool calls will break your stateful rules
The first LangGraph run let a change through that the rule should have stopped. Asked to flag two cards, the model requested both updates in a single turn, and LangGraph ran them at the same moment. They started a millisecond apart. Both checked the “second card” rule before either had touched anything, so both went ahead, with no error anywhere.
The hand-written loop never hit this, because it ran tool calls one after another. The fix belonged in the rule itself: check it and record the change in one locked step. Most frameworks run tool calls in parallel by default, so any rule that depends on what has already happened in the run needs that lock.
Parallel calls caught me out a second time. Told to mark two cards done, the model asked for both at once, both paused, and LangGraph refused to resume until it had a separate answer for each pause, identified by its id. My code assumed one question at a time, and the first time two arrived together it crashed. Once tools can run together, so can the questions they ask you.
Every framework hides behaviour you’ll only find by tracing
The project was small, and I still found six behaviours that no documentation had warned me about:
- The Claude Agent SDK saved every test conversation into my own Claude Code history until I pointed it at a different folder.
- The same SDK needs an empty message to resume a paused session, and that empty message costs an extra model turn every time.
- The SDK shows the model the name you give your tool server. I’d named mine after the product the board runs on, and the agent started telling users to go and use that product directly.
- The SDK’s safety hook matches tools by name. When a rename left the hook’s pattern and the tools’ names out of step, the hook stopped firing, and the agent completed cards without asking. Nothing reported an error. A safety check that fails open looks exactly like one that works until you test it.
- LangGraph’s checkpoints copied a pipeline’s full transcript into every saved step.
- LangGraph’s ready-made agent is already marked for removal, one major version after it shipped.
None of these is a reason to avoid the tool. Each is a reason to trace real runs before you trust one, because a larger system has more of them, and they’re harder to find.
A framework’s benefit tracks the complexity it was built to absorb
It’s tempting to conclude that frameworks pay off once a task gets complicated enough. That’s true for some kinds of complication and not for others.
LangGraph’s benefit grew with control flow, and I measured it directly. With one pause point, the hand-written version was shorter. With pauses possible anywhere in an agent’s loop, the hand-written loop had to be restructured, while LangGraph needed one function call. Add more branches, more agents, longer-running jobs or the ability to rewind, and the gap widens further. LangChain core’s value grows with the number of integrations you need: model providers, document loaders, embedding models, vector stores. I used one model provider and one API, so it had almost nothing to do. If I built search across all my recordings, a library would handle the chunking, embeddings and vector store far more cheaply than writing them myself.
Other complexity didn’t move at all. Choosing the fields, writing the confirmation rule, handling vague instructions and living with answers that change between runs were the hardest parts of the project, and no framework helped with any of them. Frameworks even introduced problems the hand-written versions never had: the parallel race, the double pause and the hook that silently stopped matching.
There’s a timing point as well. LangChain appeared when model APIs couldn’t return structured output or call tools reliably, and much of its early value was filling that gap. The providers’ own SDKs now do both well, so a lot of what a framework used to save you is now built into the API it wraps.
Which one I’d choose
| If your job is… | Choose | Because |
|---|---|---|
| A few fixed model calls with a schema | The provider’s SDK | It’s short, it hides nothing, and you can see every request |
| Structured extraction you need to trust | Pydantic AI | It sends a rejected answer back with the reason, and traces through OpenTelemetry |
| Connecting many models, documents and stores | LangChain | Its value is the integrations behind one interface |
| A run that can stop anywhere and resume later | LangGraph | interrupt() and a checkpointer, with no change to the agent |
| An agent that needs file and shell tools, subagents and permissions | Claude Agent SDK | You get Claude Code’s whole engine, with its weight |
Choose the framework for the kind of complexity you actually have. The complexity in your own problem is still yours.
Share this article
Comments
Leave a Comment
All comments are moderated and may take a short while to appear.
Loading comments...