LangGraph, LlamaIndex, CrewAI and the OpenAI and Claude Agent SDKs structure how an agent runs. They manage the loop, state passing, tool routing, retries, memory and observability.
What the framework runs on each iteration
You describe the agent as a graph or state machine of roles and their interactions. The framework then drives the loop and decides when to call the model and when to call a tool. With a library like LangGraph you can define different paths depending on the input.
- The framework takes the current state and calls the model.
- It routes any tool call the model asks for.
- If a tool fails or returns something unexpected, it can catch the error, log it, and retry a set number of times or switch tools.
- It persists state between steps, so a pause or retry keeps context.
- It streams tokens and emits traces, so each step from input to output can be inspected.
Why agent control flow differs from a fixed DAG
A job orchestrator such as Airflow also manages workflows and state, and its control flow is fixed in advance. In an agent, the model's decisions change the path at runtime, so the framework manages a nondeterministic graph.
That flexibility is why something has to manage the loop. The traces the framework emits show which path a run took when an error appears mid-run.
Hand-rolled loop or framework
The Model Context Protocol (MCP) standardizes how tools are reached. It does not run the agent loop. Without a framework you write your own state handling, tool retries and checkpoints for human approval, and you get no built-in tracing.
Teams often begin with a hand-rolled loop. State and observability become painful as the agent grows, and they adopt a framework later. The framework adds a dependency and an abstraction to learn.
- Use a plain while-loop when the agent has one tool and a simple loop.
- Use a framework when you need state persistence, branching logic, multi-agent coordination or production tracing.
The framework supplies durability, retries, checkpoints and visibility, so you do not rebuild them for each agent.
Example scenario: tool routing causes timeouts
Suppose 30% of tasks time out after a routing problem. The figure is illustrative. Poor state tracking can produce redundant or misrouted requests that slow everything down.
- Check the logs for state transitions that look wrong or missing.
- Measure average response time per tool to find the slow one.
- Run the agent with a reduced tool set and compare response times.
The fix is to correct how task state is tracked, which adds some complexity. Monitor average response time and the count of redundant requests.