create_agent
create_agent compiles a full agent runtime on LangGraph. Give it a model and tools — it handles the reasoning loop, tool dispatch, middleware, checkpointing, and stopping conditions. This article covers when to use it, what each parameter does, how the loop costs money, and how it fails.
Quick Reference
- →from langchain.agents import create_agent
- →Returns a CompiledStateGraph — supports .invoke(), .stream(), checkpointing, HITL
- →Agent loop: model reasons → calls tools → observes results → repeats until done
- →Stops when model emits final text (no tool calls) or recursion_limit is reached
- →Use model string: create_agent('anthropic:claude-sonnet-4-6', tools=[...])
- →Add checkpointer + thread_id for persistent multi-turn memory
- →Middleware wraps every iteration — use it for trimming, PII, guardrails
What create_agent Builds
create_agent compiles a LangGraph state machine into a CompiledStateGraph. It alternates between two nodes — a model node (calls the LLM) and a tools node (executes tool calls) — until the model stops making tool calls. Because the output is a CompiledStateGraph, you get .invoke(), .stream(), checkpointing, middleware hooks, and human-in-the-loop support without any extra wiring. The function signature has 13 parameters; most calls use 3.
The loop runs until the LLM decides it has enough information to respond