Daily Tech Briefing
AI 科技速览

每天 5 分钟内学习 AI。获取最新的人工智能新闻,理解其重要性,并学习如何将其应用于您的工作。

AI 快讯
Machine Learning Mastery · 2026/7/20 11:27:18
Building Agentic Workflows in Python with LangGraph

Building Agentic Workflows in Python with LangGraph

AI 中文解读
核心亮点:LangGraph让Python开发者能像搭积木一样,轻松构建出能记住聊天上下文、调用外部工具、并且每一步决策都清晰可见的AI智能体工作流。 通俗解读:以前的AI助手经常“聊完就忘”,遇到复杂任务也只会硬答。LangGraph提供了一套“流程图”式的框架,开发者可以把AI模型、数据库查询等功能像拼图一样拼在一起,每一步做了什么、调用了什么工具都记录得明明白白。即使跨越多轮对话,AI也能记住之前说过的话,不会丢三落四。 实际影响:这项技术将让开发者更快地做出更聪明的AI应用,比如能连续处理多个步骤的客服机器人、能查询内部数据库的智能助理。对普通用户来说,未来用到的AI工具将更靠谱,不会聊着聊着就断片,也能解释自己是怎么得出结论的,用起来更放心。
Building Agentic Workflows in Python with LangGraph By Bala Priya C on July 20, 2026 in Artificial Intelligence 6 Share Post Share In this article, you will learn how to build a complete agentic workflow in Python with LangGraph, from a single model call to a tool-using agent with persistent conversation memory. Topics we will cover include: How state, nodes, and edges combine to define the execution flow of a LangGraph agent. How to register a tool and route the model’s tool calls through the graph’s reasoning loop. How a checkpointer persists conversation history across separate graph invocations. Let’s not waste any more time. Introduction Most AI agent setups handle the single-turn case well: take a question, call a model, and return an answer. The harder problems appear soon after that. An agent may need to query your database, remember the context from earlier messages, or give you visibility into exactly what the model decided and why. Solving those challenges without building custom plumbing for every use case is where many implementations begin to break down. LangGraph provides a clean structure for handling each of these problems. An agent is represented as a graph, where nodes are units of work, edges define what runs next, and a shared state object carries the complete message history through every step. The model runs inside a node, so every reasoning step, tool call, and response becomes part of the graph’s state. That makes the entire execution flow visible, inspectable, and available to any node that runs afterward. In this article, you’ll learn how to understand the state, node, and edge primitives that every LangGraph graph is built on; manage conversation history automatically with MessagesState; call a language model inside a node and connect it to a graph; register a tool and route tool calls back through the model; trace the complete message sequence to see exactly what the model does at each step; and persist conversations across separate invocations with a checkpointer. We’ll build the graph from the ground up, starting with the installation steps. Setting Up Install the required packages: pip install langgraph langchain-openai python-dotenv 1 pip install langgraph langchain-openai python-dotenv Then create a .env file in your project root with your OpenAI API key: OPENAI_API_KEY="your_key_here" 1 OPENAI_API_KEY="your_key_here" Load it at the top of your script before any LangChain or LangGraph imports: from dotenv import load_dotenv load_dotenv() 12 from dotenv import load_dotenvload_dotenv() python-dotenv reads the .env file and sets the key as an environment variable. Understanding State, Nodes, and Edges Every LangGraph graph is built from the following three components. Getting them right upfront saves confusion when the graph gets more complex. State is a TypedDict that acts as the shared memory for the entire graph. Every node reads from it and writes updates back to it. Nothing passes between nodes any other way. Fields you don’t update in a node stay unchanged; you only return what you want to modify. Nodes are plain Python functions. A node takes the current state as its argument and returns a dictionary of the fields it wants to update. Registering a function with add_node is what makes it part of the graph without the need for a special decorator or base class. If you pass just the function without a name string, LangGraph uses the function name automatically. Edges define execution order. add_edge(A, B) means: after node A finishes, run node B. add_conditional_edges means: after node A finishes, call a routing function and go wherever it points. Every graph needs START as its entry point and at least one path to END. By
分享
阅读原文