This article is published in English.
Building an AI Agent from Scratch: Patterns, ReAct, and LangGraph
Learn the core concepts behind AI agents—planning, tool use, reflection, and the ReAct pattern—and how LangChain and LangGraph fit into building one manually.
AI systems are no longer limited to answering isolated questions. With AI agents, a model can plan a sequence of actions, call on external tools, evaluate what comes back, and carry out tasks that require several steps to finish.
What Makes Something an AI Agent?
A standard AI model reacts to a single prompt and produces a single response. An AI agent goes further. It can:
- Figure out what the user actually wants
- Break that goal into a sequence of actions
- Choose and invoke appropriate tools
- Look at what those tools return
- Keep working until the job is actually finished
Think of a virtual travel planner putting together a two-day trip to Kandy. It has to gather information from multiple sources and then assemble that information into a workable itinerary rather than just describing one in the abstract.
What an Agentic Workflow Looks Like
Many real tasks are too complicated to handle in a single pass. An agentic workflow deals with this by splitting the task into distinct phases.
Typical phases include:
- Planning
- Researching
- Writing
- Reviewing
- Refining
Structuring the work this way lets the agent build on the output of earlier phases and keep sharpening its final response.
Recurring Patterns in Agent Design
There are several established patterns used when designing AI agents:
- Planning: splitting one large task into a set of smaller ones.
- Tool use: reaching out to external resources like calculators, search engines, or databases.
- Reflection: checking the quality of a draft answer and refining it.
- Multi-agent communication: letting several agents collaborate on the same problem.
- Memory: retaining useful details for later steps or future sessions.
These patterns give agents the ability to tackle problems that are more involved than a single prompt-response exchange.
Where LangChain and LangGraph Fit In
LangChain supplies the components needed to build applications powered by language models, including prompt templates, tool integrations, and connectors to different models.
LangGraph is what ties individual actions together into a coherent chain. It's especially useful when your agent needs loops, persistent memory, tool calls, or a point where a human steps in.
To summarize the difference:
- LangChain supplies the parts.
- LangGraph orchestrates how they run.
Understanding the ReAct Pattern
The ReAct pattern merges reasoning with acting.
An agent following this pattern cycles through:
- Reasoning about what to do next
- Executing an action
- Checking the outcome
- Reasoning about the following step
This loop keeps running until the agent is confident it can produce a final answer.
The ReAct cycle ties together thinking, acting, observing, and delivering the final result.
Constructing a Basic Agent Manually
Consider building a minimal agent equipped with two tools:
- One that looks up a dog breed's typical weight
- One that performs addition
Picture a user posing a question along these lines: how much would a Border Collie and a Scottish Terrier weigh if you added their weights together.
Faced with that request, the agent works through a short sequence:
- Look up the Border Collie's weight: 37 lb
- Look up the Scottish Terrier's weight: 20 lb
- Call the calculator with
37 + 20 - Report the total: 57 lb
This walkthrough illustrates how an agent chains together multiple tool calls instead of just guessing at an answer directly.
Dividing Work Between the Model and the Runtime
The language model and the surrounding Python code each handle separate parts of the job.
The LLM is responsible for:
- Interpreting what's being asked
- Deciding which action comes next
- Picking the right tool for that action
- Composing the final reply
The Python runtime takes care of:
- Actually running the chosen tools
- Keeping track of results
- Managing the loop that drives the agent
- Feeding tool outputs back into the model
In short, the model decides what needs to happen, and the Python code carries it out.
Turning the Loop Into an Automated Process
Instead of triggering each tool call by hand, you can automate the entire agent loop. The system checks whether the model wants to call a tool:
- If it does, that tool gets executed.
- The output is passed back into the model.
- Once no further tools are needed, the agent returns the final answer.
Automating the loop this way turns the agent into something reusable across many different user requests, not just a one-off script.
Main Points to Remember
- AI agents solve tasks by working through a sequence of steps.
- Agentic workflows rely on planning, tool calls, observation, and repeated refinement.
- Common design patterns cover planning, tool use, reflection, multi-agent coordination, and memory.
- LangChain provides the building blocks for AI applications.
- LangGraph manages how those blocks connect into a working process.
- ReAct links together reasoning, acting, and observing results.
- The LLM makes decisions while the Python runtime handles execution.
- An automated agent loop keeps calling tools until the task is complete.
These fundamentals lay the groundwork for building more sophisticated agents with LangGraph going forward.
The next stage in AI isn't just producing better answers — it's building systems capable of taking the right actions on their own.