Building AI Agents from Scratch: LangGraph Architecture and Practical Guide

A comprehensive guide to AI agent architecture using LangGraph, LangChain, and MCP.
This article explores the core principles of AI agents and their development practices, analyzing leading products like OpenAI Deep Research, Zhipu's ChengPian, and Manus. It details the complete technology stack for building enterprise-grade agents — including LangGraph + LangChain for orchestration, MCP for tool integration, Ollama for local model deployment, and prompt engineering techniques.
What Is an AI Agent?
In today's rapidly evolving AI landscape, "AI Agent" has become one of the hottest technical concepts. Yet many people's understanding of it remains superficial. According to industry consensus, an AI agent is a system or program capable of autonomously perceiving its environment, making decisions, and executing actions.
In fact, the concept of AI agents is not new — its academic roots trace back to the 1990s. MIT professor Rodney Brooks' "behavior-based AI" and the definition of "rational agents" by Stuart Russell and Peter Norvig in their classic textbook Artificial Intelligence: A Modern Approach laid the theoretical foundation for today's AI agents. Russell defined an agent as "an entity that perceives its environment through sensors and acts upon it through actuators," and proposed four agent architectures: simple reflex, model-based, goal-based, and utility-based. Today's LLM-driven AI agents are essentially an advanced implementation of utility-based agents, where the "utility function" is approximated by the reasoning capabilities of large language models.
At its core, an agent is still a program written in code, but what fundamentally distinguishes it from traditional programs is its "autonomy." Take an intelligent customer service bot as an example: when a user asks a question that never appeared in the training data, the agent can autonomously determine how to handle it based on the current context and execute the appropriate action — all without human intervention.

Tech giants like IBM and NVIDIA describe agents similarly: they are not simple Q&A systems, but autonomous systems with a closed-loop capability of perception-decision-execution. This closed loop is known in control theory and robotics as the "Sense-Act Loop." In traditional robotics, perception relies on physical sensors (cameras, LiDAR, etc.), while in AI agents, perception is generalized to understanding digital information such as text, API responses, and web content. The decision-making component is handled by the reasoning capabilities of large language models, and the execution component is realized through tool use. The key to this loop is "feedback" — the agent can adjust subsequent decisions based on execution results, forming an adaptive iterative optimization process. This "intelligent-like" characteristic is precisely where the name "agent" comes from.
Comparative Analysis of Leading AI Agent Products
OpenAI Deep Research and Zhipu's ChengPian
OpenAI's Deep Research is a quintessential agent product focused on complex reasoning tasks. Its domestic counterpart is Zhipu ChatGLM's "ChengPian" (橙篇), whose tagline "Think while you work" perfectly captures the agent's operating mode.

ChengPian's core capability lies in thinking and reasoning while simultaneously retrieving data — autonomously operating a browser to fetch information from the web, bringing the acquired knowledge back to continue deeper analysis, resulting in more thorough research outcomes. It works by installing a browser plugin that the agent controls to read web pages and extract information.
This "think + act" loop is academically known as the ReAct (Reasoning + Acting) paradigm, proposed by Princeton University and the Google Brain team in 2022. The core idea of ReAct is to have the large language model alternate between reasoning (Reasoning Trace) and acting (Action), obtaining observation results (Observation) after each action, and then continuing to reason based on those observations. This paradigm outperforms pure reasoning (Chain-of-Thought) or pure action approaches on complex tasks, because reasoning helps the model plan and adjust action strategies, while the external information obtained through actions can correct hallucinations and knowledge gaps in reasoning. This is the key feature that distinguishes AI agents from traditional chatbots — traditional AI can only answer questions based on existing knowledge, while agents can proactively acquire new information to support decision-making.
Manus: A General-Purpose Agent Closer to AGI
Manus represents a more advanced stage of agent development — it can not only browse the web but also perform complex system-level operations on remote computers.

Take analyzing Tesla stock as an example. Manus's workflow is impressive: it remotely boots up a computer and executes Shell commands on it. This means it can:
- Read and create files
- Write code
- Deploy and run code
- Perform various OS management operations

Throughout the analysis process, Manus autonomously decides and executes a series of operations, ultimately generating a visual dashboard website for Tesla stock analysis — a website entirely created by AI. Its core capability is essentially script execution: as long as the AI model can proactively use Shell, similar functionality can be achieved.
It's worth noting that Manus's Shell execution capability involves an important architectural decision: remote sandbox environments. To prevent AI-executed commands from damaging the host system, the industry typically uses containerization technologies (such as Docker) or virtual machines to create isolated execution environments. The agent has full OS privileges within the sandbox — it can install packages, write and run code, and manipulate the file system — but all these operations are confined within the sandbox. This design grants the agent powerful execution capabilities while ensuring security. Similar techniques are also employed by OpenAI's Code Interpreter and Anthropic's Computer Use.
Core Technology Stack for AI Agent Development
From the product examples above, we can see that developing an enterprise-grade AI agent requires the following core technical components:
Framework Layer: LangGraph + LangChain
LangChain was created by Harrison Chase in 2022, initially as a Python framework to simplify LLM application development, providing core abstractions such as Chain, Memory, and Tool integration. LangChain provides the foundational capabilities for interacting with large models, while LangGraph builds on top of it to implement state management and workflow orchestration, enabling agents to maintain contextual coherence across multi-step tasks.
However, as agent application complexity grew, linear Chain structures struggled to handle scenarios requiring conditional branching, loops, and parallel execution. LangGraph emerged to address this, based on the concepts of Directed Acyclic Graphs (DAG) and state machines, modeling agent workflows as graph structures — nodes represent processing steps, and edges represent state transition conditions. LangGraph supports persistent state, Human-in-the-Loop breakpoints, and streaming output, enabling developers to build production-grade agents with complex control flows. This combination is particularly well-suited for building complex agents that require "perception-decision-execution" loops.
Protocol Layer: MCP (Model Context Protocol)
The MCP architecture addresses the standardization of agent-to-tool interactions. MCP was open-sourced by Anthropic in late 2024, with its design inspired by LSP (Language Server Protocol) from software engineering. Just as LSP unified the communication protocol between code editors and programming language services, MCP aims to standardize the interaction between large models and external data sources and tools.
MCP adopts a client-server architecture: AI applications serve as MCP clients, while various tools and data sources serve as MCP servers. The protocol defines three core primitives — Resources (providing contextual data), Tools (executing operations), and Prompts (prompt templates). Whether it's browser control, file operations, or API calls, MCP provides a unified interface specification. MCP solves the previously common "M×N problem" where every AI application needed custom integration code for every tool, simplifying it to an "M+N problem" and dramatically reducing the complexity of tool integration. Currently, major vendors including OpenAI, Google, and Microsoft have all announced support for the MCP protocol, making it an emerging de facto industry standard.
Model Layer: Local Deployment with Ollama
Deploying large models locally through Ollama ensures data privacy while reducing API call costs. Ollama is an open-source local LLM runtime framework that supports running mainstream open-source models such as Llama, Mistral, Qwen, and Gemma on consumer-grade hardware. Under the hood, it's based on the llama.cpp project, using the GGUF quantization format to significantly reduce model VRAM requirements — for example, a 70B parameter model can run on approximately 40GB of VRAM after 4-bit quantization.
Ollama provides an OpenAI-compatible API interface, allowing developers to migrate applications built on the OpenAI API to local deployment with near-zero effort. For enterprise applications, local deployment is a necessary choice for production environments — it not only addresses data privacy compliance requirements (such as GDPR and classified protection standards) but also eliminates dependency risks on external APIs and significantly reduces costs in high-frequency call scenarios.
Engineering Layer: Prompt Engineering
Prompt Engineering is the bridge connecting human intent with model capabilities, having evolved from early empirical tricks into a systematic methodology. Core techniques include: Zero-shot prompting, Few-shot prompting, Chain-of-Thought (CoT) prompting, Self-Consistency, and Tree-of-Thought, among others.
In agent development, prompt engineering is particularly critical — System Prompts define the agent's role, capability boundaries, and behavioral norms; tool description prompts determine whether the model can correctly select and invoke tools; and Reflection Prompts guide the agent to self-evaluate and correct its own outputs. Well-crafted prompts can significantly improve an agent's decision quality and task completion rate. Companies like OpenAI and Anthropic have published official prompt engineering best practice guides, emphasizing the importance of structure, clarity, and iterative optimization.
Summary and Outlook
From ChengPian's "think while you work" to Manus's system-level operations, AI agents are evolving from simple conversational assistants into true digital workers. After understanding the core principle of "perception-decision-execution" behind them, developers can build agent applications with similar capabilities using the LangGraph + LangChain + MCP technology stack.
The key lies in enabling two core capabilities: first, information acquisition (browser control, API calls), and second, action execution (Shell scripts, code execution). Once these two capabilities are mastered, the application boundaries of agents will expand dramatically.
Key Takeaways
Related articles

Deep Dive into Microsoft's AI Security Tools: Does Performance Really Surpass the Competition?
Microsoft launches enterprise AI security tools claiming superior performance. This deep analysis examines core capabilities, ecosystem advantages, and risks to guide enterprise security decisions.

OpenAI Employee Shares How to Build an AI Work Operating System with Codex
OpenAI's Jason Liu shares how he uses ChatGPT Workbench and Codex to build an AI work OS: Chief of Staff automation, persistent threads, Skills/Plugins, browser control, and app-building methodology.

Learning AI Large Language Models: A Complete Roadmap from Beginner to Practitioner
A systematic breakdown of the AI LLM learning roadmap covering prompt engineering, AI Agent development, RAG knowledge bases, model fine-tuning, and hands-on projects for beginners.