这是用户在 2025-8-7 9:53 为 https://docs.langchain.com/langgraph-platform/local-server#python-sdk-async 保存的双语快照页面,由 沉浸式翻译 提供双语支持。了解如何保存?
This guide shows you how to run a LangGraph application locally.
本指南将向您展示如何在本地运行 LangGraph 应用程序。

Prerequisites  先决条件

Before you begin, ensure you have the following:
开始之前,请确保您已具备以下条件:
  • An API key for LangSmith - free to sign up
    LangSmith 的 API 密钥 - 免费注册

1. Install the LangGraph CLI
1. 安装 LangGraph CLI

# Python >= 3.11 is required.

pip install --upgrade "langgraph-cli[inmem]"

2. Create a LangGraph app 🌱
2. 创建 LangGraph 应用 🌱

Create a new app from the new-langgraph-project-python template or new-langgraph-project-js template. This template demonstrates a single-node application you can extend with your own logic.
new-langgraph-project-python 模板或 new-langgraph-project-js 模板创建一个新应用。该模板展示了一个单节点应用,你可以通过自己的逻辑进行扩展。
langgraph new path/to/your/app --template new-langgraph-project-python
Additional templates If you use langgraph new without specifying a template, you will be presented with an interactive menu that will allow you to choose from a list of available templates.
额外的模板 如果您使用 langgraph new 而未指定模板,系统将显示一个交互式菜单,允许您从可用模板列表中选择。

3. Install dependencies  3. 安装依赖项

In the root of your new LangGraph app, install the dependencies in edit mode so your local changes are used by the server:
在您的 LangGraph 应用的根目录中,以 edit 模式安装依赖项,以便服务器使用您的本地更改:
cd path/to/your/app
pip install -e .

4. Create a .env file
4. 创建一个 .env 文件

You will find a .env.example in the root of your new LangGraph app. Create a .env file in the root of your new LangGraph app and copy the contents of the .env.example file into it, filling in the necessary API keys:
你会在新 LangGraph 应用的根目录下找到一个 .env.example 。在新 LangGraph 应用的根目录下创建一个 .env 文件,并将 .env.example 文件的内容复制到其中,填写必要的 API 密钥:
LANGSMITH_API_KEY=lsv2...

5. Launch LangGraph Server 🚀
5. 启动 LangGraph 服务器 🚀

Start the LangGraph API server locally:
在本地启动 LangGraph API 服务器:
langgraph dev
Sample output:  示例输出:
>    Ready!
>
>    - API: [http://localhost:2024](http://localhost:2024/)
>
>    - Docs: http://localhost:2024/docs
>
>    - LangGraph Studio Web UI: https://smith.langchain.com/studio/?baseUrl=http://127.0.0.1:2024
The langgraph dev command starts LangGraph Server in an in-memory mode. This mode is suitable for development and testing purposes. For production use, deploy LangGraph Server with access to a persistent storage backend. For more information, see Deployment options.
langgraph dev 命令以内存模式启动 LangGraph Server。此模式适用于开发和测试目的。在生产环境中,请部署具有持久化存储后端访问权限的 LangGraph Server。更多详情,请参阅部署选项。

6. Test your application in LangGraph Studio
6. 在 LangGraph Studio 中测试您的应用程序

LangGraph Studio is a specialized UI that you can connect to LangGraph API server to visualize, interact with, and debug your application locally. Test your graph in LangGraph Studio by visiting the URL provided in the output of the langgraph dev command:
LangGraph Studio 是一个专门的 UI,您可以通过它连接到 LangGraph API 服务器,以在本地可视化、交互和调试您的应用程序。通过访问 langgraph dev 命令输出中提供的 URL,在 LangGraph Studio 中测试您的图:
>    - LangGraph Studio Web UI: https://smith.langchain.com/studio/?baseUrl=http://127.0.0.1:2024
For a LangGraph Server running on a custom host/port, update the baseURL parameter.
对于在自定义主机/端口上运行的 LangGraph 服务器,更新 baseURL 参数。

7. Test the API  7. 测试 API

  1. Install the LangGraph Python SDK:
    安装 LangGraph Python SDK:
pip install langgraph-sdk
  1. Send a message to the assistant (threadless run):
    向助手发送消息(无线程运行):
from langgraph_sdk import get_client
import asyncio

client = get_client(url="http://localhost:2024")

async def main():
    async for chunk in client.runs.stream(
        None,  # Threadless run
        "agent", # Name of assistant. Defined in langgraph.json.
        input={
        "messages": [{
            "role": "human",
            "content": "What is LangGraph?",
            }],
        },
    ):
        print(f"Receiving new event of type: {chunk.event}...")
        print(chunk.data)
        print("\n\n")

asyncio.run(main())

Next steps  下一步

Now that you have a LangGraph app running locally, take your journey further by exploring deployment and advanced features:
现在您已经本地运行了一个 LangGraph 应用,通过探索部署和高级功能来进一步扩展您的旅程:
  • Deployment quickstart: Deploy your LangGraph app using LangGraph Platform.
    部署快速入门:使用 LangGraph 平台部署您的 LangGraph 应用。
  • LangGraph Platform overview: Learn about foundational LangGraph Platform concepts.
    LangGraph 平台概览:了解 LangGraph 平台的基础概念。
  • LangGraph Server API Reference: Explore the LangGraph Server API documentation.
    LangGraph 服务器 API 参考:探索 LangGraph 服务器 API 文档。
  • Python SDK Reference: Explore the Python SDK API Reference.
    Python SDK 参考:探索 Python SDK API 参考。
  • JS/TS SDK Reference: Explore the JS/TS SDK API Reference.
    JS/TS SDK 参考:探索 JS/TS SDK API 参考。