Choosing your agent loop: LangGraph vs OpenAI Agents SDK vs Claude Agent SDK vs plain code

Every agent runs the same small loop: build the context, ask the model, run the tools it asked for, and go round again. LangGraph, the OpenAI Agents SDK, the Claude Agent SDK and a plain while-loop are four ways to own that loop, each with real gifts and real costs. Pick by the job, then put the loop behind a port so you can change your mind.

A new agent project often starts with a long argument about frameworks. LangGraph or the OpenAI Agents SDK? The Claude Agent SDK, or just write it ourselves? People pick a side early and defend it for months.

The argument matters less than it feels. Under all of them sits the same small loop, and in a working agent that loop is a small part of the code. The checks, the state, the traces and the cost meter are the rest, as we argued in the model is 10%.

Still, the loop is a real choice, and each option has real gifts and real costs. This post compares them fairly, as of October 2026. These products change fast, so check the details below against their current documentation before you rely on them.

Four ways to make the trip

Think of the loop as getting from a question to an answer. There are four ways to make the trip.

A bus on a fixed routeLangGraph: a graphyou draw the stopsand the roads betweenA relay raceOpenAI Agents SDK: handoffsone agent passesthe baton to the nextA car with a driverClaude Agent SDKthe driver and thetools come includedA bicycle you buildPlain code: your while-loopevery part is yours,and so is every repairroute 4toolbox in the boot
Four shapes of loop. They all get you there; they differ in who drives and who fixes it.

Our tutorial compares a few more. Pydantic AI is a typed Python framework whose agent you can step through one step at a time. The Anthropic tool runner is a thin helper: the loop with hooks, still yours. Microsoft Agent Framework suits .NET or Azure-first teams.

The same five steps, whoever drives

Whichever you choose, the loop does the same job.

Build the contextwhat the model reads1Ask the modelone call2Read the answerwords or tool requests?3Check, then runthe tools it asked for4Stop or go rounddone, or out of budget?5go round: the tool results go into the next contextthe same five steps in LangGraph, either SDK, or your own while-loopStop rules, checked every tripsay, 12 trips, 60 seconds, $0.50
What every agent loop does. The frameworks differ only in who writes these five boxes, and where you can reach in.
  1. Build the context: the instructions, the conversation so far, and the tools this person may use.
  2. Ask the model for its next step.
  3. Read the answer. Is it words, or a request to call tools?
  4. Check, then run the tools it asked for, and add the results to the context.
  5. Stop or go round. Stop if the model answered in words, or if a budget ran out.

The budgets are the part teams forget. The tutorial's agent stops after 12 trips, 60 seconds, $0.50 or 24 tool calls, whichever comes first. The question to ask of any framework is simple: where would my checks and budgets sit, and can my tests reach them?

What each one gives you

LangGraph gives you the most structure for long, stateful work. A checkpointer saves the state after each step, so a run can stop, wait and resume, and Postgres is one of the stores it can use (see just use Postgres). An interrupt pauses the graph for a person to approve. It streams results as they happen and works with any model. Our tutorial's verdict is that tests can check it node by node, but full tracing comes mainly with LangSmith, the tracing service from the same company.

The OpenAI Agents SDK gives you a small set of ideas: agents, handoffs, guardrails that check input and output, and sessions that remember the conversation. Tracing is switched on by default and goes to OpenAI's dashboard, and you can add your own exporters. It streams, and it can call other providers' models. Our own builds don't use it, so we have not measured it the way we measured the others.

The Claude Agent SDK gives you the most out of the box: built-in tools, subagents, hooks that run before and after each tool, permission checks, and support for MCP servers. MCP, the Model Context Protocol, is a standard way to plug tools into an agent. It is the quickest way to an agent that works on files and code. Our tutorial rates it best for coding, operations and research agents.

A plain loop gives you nothing for free, and complete control. Every check sits at an exact line that a unit test can reach, and it works with any model. You write state, checkpoints and streaming yourself.

What each one costs you

There are three costs, and they are easy to miss in a demo.

Lock-in. Our tutorial rates the Claude Agent SDK's lock-in as high, because it is Claude-only. LangGraph's is medium, because your agent is shaped like its graph and its best tracing is LangSmith. A plain loop has none.

Learning. A graph framework is a new way of thinking about code. An SDK is a new set of rules about what runs when. Everyone on your team already knows how a while loop works.

Hidden prompts. Every framework sends the model text you didn't write: descriptions of built-in tools, handoff tools, output-format reminders. That text uses tokens, and it changes when you upgrade. Log the exact request your agent sends and read it at least once.

There is a fourth cost for agents that touch money. In the tutorial's comparison, tests can reach every step of a plain loop, but only partly reach the Claude Agent SDK's, because the loop is hidden inside it.

Pick this when

PickWhenWatch for
Plain code loopa wrong step moves money or changes records, and every step must be testedyou build state, streaming and checkpoints
LangGraphthe work is a known flow with branches and pauses for approvalthe graph model to learn; best tracing is LangSmith
OpenAI Agents SDKa few specialist agents pass one conversation between themhandoff prompts you didn't write; tracing goes to OpenAI unless you change it
Claude Agent SDKinternal agents that work on files, code, a shell or the webClaude only; tests can't reach every step
Pydantic AIsmall typed agents, or as the model layer under your own loopfor bigger agents, step the loop yourself

If you'd rather not read a table, ask three questions in order.

1. Can a wrong step move moneyor change someone's records?yesPlain code loopchecks at exact placesno2. Is the work a known flowwith pauses for a person?yesLangGrapha graph with checkpointsno3. Does it work on files, codeor the web, for your own team?yesClaude Agent SDKtools and context includednoStart with a plain loopadd handoffs (the OpenAI Agents SDK's idea) only if you really have specialists passing work
Three questions, asked in order. Whatever you pick, put it behind the port.

The tutorial's own decision follows the first question. It writes the loop itself and uses Pydantic AI only to call the model, so the budgets and checks sit at exact places a test can reach. It says it would pick LangGraph for workflow-shaped agents and Agent Framework for a .NET team.

The loop is the small part. Put it behind a port.

A port is a fixed shape that a part must fit, like a socket on the wall. The rest of the agent calls the port and never knows which part is plugged in.

THE HARNESS: STAYS THE SAMERouterStateChecksTracesCost meterThe loop portrun(turn) gives back a resultPlain codeabout 400 lines: builtPydantic AIadapter: builtLangGraphadapter: plannedClaude Agent SDKadapter: plannedsame tests for every plug; swapping is a line of configuration
The loop as a plug-in part. The reference agent runs its own loop, and can run Pydantic AI's instead without the rest noticing.

The reference agent writes its own loop, with no framework, so that each layer of the harness is visible. Its harness has one call for a turn. A second module answers the same call with Pydantic AI owning the steps, chosen by one line of configuration. The router, the waits and the state don't know which loop ran.

Adapters for LangGraph and the Claude Agent SDK are planned, not built. Each must pass the same test cases unchanged, in at most 150 lines of adapter per port, without changing the harness to suit the framework.

The reference agent's notes put it plainly: if you can't say where your step budget, loop detection and cost ceiling live after adopting a framework, the framework has adopted you.

What goes wrong

  1. Choosing by demo. The framework that builds the quickest demo is not always the one you can test and run for a year.
  2. Checks inside the framework's callbacks only. When the framework changes how its callbacks work, your rules move with it. Keep the rules in your own code and call them from the hooks.
  3. No budgets. Some frameworks have a step limit, but none knows your money limit. A loop with no stop rule will find a way to loop.
  4. Never reading the real request. The hidden prompt grows with each upgrade, and nobody notices until the bill does.
  5. The whole agent written in the framework's shapes. Then swapping the loop means rewriting the agent.

Seven questions to ask before you choose

  1. If a wrong step happens, does it cost money or change someone's data?
  2. Where exactly would my step, time and money budgets sit?
  3. Can a unit test reach the check that runs before each tool call?
  4. Does it work with the models I may need next year, not just this one?
  5. Where do my traces go by default, and can I send them to my own store?
  6. Can a run pause for hours waiting for a person, and survive a restart?
  7. What text does it send to the model that I didn't write?

Choose the loop that fits the job, keep it behind a port, and spend the time you saved on the other 90%.