When you need a library
LangChain and LangGraph are libraries, that is, sets of ready code that a programmer connects to their own program. They have no interface with buttons: they do not work without a developer.
You need a library when the agent lives inside your system: in your own service, next to the database, with its own access rights. In such a setup a visual builder hits the limits of its own blocks.
LangChain fits a standard agent: a model, an instruction and tools. The LangChain developers describe an agent as a model that calls tools in a loop until the task is solved. Everything around the loop (the instruction, the tools and the middleware handlers) they call the harness.
In LangChain the model is set in one line, and the library supports many vendors: OpenAI, Anthropic, Google, open models through Ollama and others. For a business this is insurance: if a model vendor becomes unavailable or more expensive, the programmer changes a line and a package instead of rewriting the agent.
For complex tasks LangChain has a ready build called Deep Agents: the agent compresses a long context by itself, works with a virtual file system and launches helper agents. It is built on the same LangChain agents, so it still makes sense to start with them.
You need LangGraph when one process mixes fixed steps and steps where the model decides, when the process runs for a long time and must survive failures, and when a person must review the work. For beginners, the LangGraph documentation advises taking ready LangChain agents first.
In its review of 19 December 2024, Anthropic advises starting with a simple solution and adding complexity only when the simple one cannot cope. Sometimes this means you do not need an agent at all.
For a team of agents with roles there is a separate framework, CrewAI, which we covered on a neighboring page.
An agent as a graph of steps
According to the documentation, the first agent on LangChain takes four steps:
1. Check that Python 3.10 or newer is installed, and install the library: pip install -U langchain.
2. Install the model vendor’s package; they come separately. For open models on your own computer it is the langchain-ollama package: pip install -U langchain langchain-ollama. Ollama itself runs the model locally.
3. Describe the agent with the create_agent function: a model, a list of tools and a system prompt, that is, a standing instruction for the agent. In the documentation example, the local model is set by the string “ollama:devstral-2”.
4. Run the agent with the invoke method and pass it the user’s message. The answer is in the last message of the result.
When the ready loop is not enough, you describe the process as a graph in LangGraph. The graph is built from three parts:
1. State: a shared data structure, a snapshot of the current work. It usually holds the conversation and the fields the process needs.
2. Nodes: functions that read the state, do their work and return an update. A node can be a model call, a CRM query or a plain calculation.
3. Edges: rules for moving to the next node. They are fixed or conditional, that is, they depend on what is in the state.
4. Build the graph in the StateGraph class and compile it, and then you can run it. Install it with pip install -U langgraph.
A real example from a sales team. The “request review” node calls the model and writes the request type into the state. A conditional edge sends a return request to the node that checks the order in 1C, a Russian accounting system, and a price question straight to the reply node. Here the model decides only one thing: what type of request it is; code sets the rest of the path.
For a business, the value of a graph is control. Some steps are predictable and checkable, as in a normal program, and the model decides only where it is allowed to.
Memory and tools
A tool is a function the agent can call: find a client in the database, calculate a price, create a request. The model itself decides when to call a tool and which parameters to pass. This is how you make a tool in LangChain:
1. Write a Python function and put the @tool decorator above it. A decorator is a mark above a function that turns it into an agent tool.
2. Give the parameter types. According to the documentation they are required: the description of the input data is built from them.
3. Write the function’s docstring short and to the point. From it, the model understands what the tool is for and when to call it.
4. Pass the function in the tools list when you create the agent.
LangGraph has two kinds of memory. Short-term memory keeps the course of one conversation, and long-term memory keeps information between conversations: client preferences, facts and shared knowledge.
1. For short-term memory, connect a checkpointer, a keeper of state snapshots. In the documentation examples it is InMemorySaver, which holds data in the program’s memory.
2. With every call, pass thread_id, the conversation number. By it the agent continues the same dialog.
3. For long-term memory, connect a store, a storage that all conversations can see. In the documentation example it is InMemoryStore.
Keeping data in the program’s memory is fine for a test: after a restart everything is gone. In a working system the snapshots go to a database, and the documentation describes PostgresSaver for this.
A long history gets in the model’s way. The LangChain documentation warns that most models work worse on a long context, get distracted by outdated content and answer more slowly and at a higher cost. So old messages are trimmed or compressed.
You can make the agent’s risky actions wait for a person’s approval. LangChain has the HumanInTheLoopMiddleware handler for this:
1. Add HumanInTheLoopMiddleware to the middleware list when you create the agent.
2. In the interrupt_on parameter, list the tools that need a person’s decision: for example writing a file or a database query. Mark safe tools, such as reading, as False.
3. Connect a checkpointer: without it the agent cannot stop and resume work after the decision. For a working system the documentation advises storage in a database, for example AsyncPostgresSaver.
4. When the agent stops, a person chooses one of the decisions: approve (run as is), edit (change the parameters), reject (decline with an explanation) or respond (answer instead of the tool).
Plain model calls instead of a library
A library is not required. In its article on building agents, Anthropic advises developers to start with direct calls to the model’s API: many patterns fit into a few lines of code. API here means the way a program reaches the model over the network.
The same article warns about frameworks: they add layers of abstraction that hide what exactly went to the model and what came back. This makes errors harder to find.
The working rule is this. Write one model call with an instruction and a couple of tools directly. Take a library when you need a graph of steps, memory between conversations, resuming after a failure or a human review.
An example. An agent needs to read a client’s email, find the order and suggest a reply to a manager. This is one model call with two tools and a human check, and such code is shorter than setting up a library. You will need a graph and memory when the process stretches over days and several systems.
If a developer proposes a library, ask how they will see the requests to the model and the answers to them. Without that, every odd answer of the agent turns into a long search for the cause.
A standard agent is built from open blocks, and LangChain is one of them. If someone charges for such an agent as one-of-a-kind custom work, ask what the price is made of, and offer payment for a result by a metric, not for hours.