> ## Documentation Index
> Fetch the complete documentation index at: https://langchain-zh.cn/llms.txt
> Use this file to discover all available pages before exploring further.

# 快速开始

本快速入门演示如何使用 LangGraph 的图 API 或函数式 API 构建一个计算器智能体。

<Tip>
  **使用 AI 编程助手？**

  * 安装 [LangChain Docs MCP 服务器](/use-these-docs)，让你的智能体能够访问最新的 LangChain 文档和示例。
  * 安装 [LangChain Skills](https://github.com/langchain-ai/langchain-skills)，以提升你的智能体在 LangChain 生态系统任务上的表现。
</Tip>

* 如果你倾向于将智能体定义为节点和边的图，请[使用图 API](#use-the-graph-api)。
* 如果你倾向于将智能体定义为单个函数，请[使用函数式 API](#use-the-functional-api)。

有关概念性信息，请参阅[图 API 概述](/oss/javascript/langgraph/graph-api)和[函数式 API 概述](/oss/javascript/langgraph/functional-api)。

<Info>
  对于此示例，你需要设置一个 [Claude (Anthropic)](https://www.anthropic.com/) 账户并获取 API 密钥。然后，在你的终端中设置 `ANTHROPIC_API_KEY` 环境变量。
</Info>

<Tabs>
  <Tab title="使用图 API">
    ## 1. 定义工具和模型

    在此示例中，我们将使用 Claude Sonnet 4.5 模型，并定义用于加法、乘法和除法的工具。

    ```typescript theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
    import { ChatAnthropic } from "@langchain/anthropic";
    import { tool } from "@langchain/core/tools";
    import * as z from "zod";

    const model = new ChatAnthropic({
      model: "claude-sonnet-4-6",
      temperature: 0,
    });

    // 定义工具
    const add = tool(({ a, b }) => a + b, {
      name: "add",
      description: "将两个数字相加",
      schema: z.object({
        a: z.number().describe("第一个数字"),
        b: z.number().describe("第二个数字"),
      }),
    });

    const multiply = tool(({ a, b }) => a * b, {
      name: "multiply",
      description: "将两个数字相乘",
      schema: z.object({
        a: z.number().describe("第一个数字"),
        b: z.number().describe("第二个数字"),
      }),
    });

    const divide = tool(({ a, b }) => a / b, {
      name: "divide",
      description: "将两个数字相除",
      schema: z.object({
        a: z.number().describe("第一个数字"),
        b: z.number().describe("第二个数字"),
      }),
    });

    // 为 LLM 增强工具功能
    const toolsByName = {
      [add.name]: add,
      [multiply.name]: multiply,
      [divide.name]: divide,
    };
    const tools = Object.values(toolsByName);
    const modelWithTools = model.bindTools(tools);
    ```

    ## 2. 定义状态

    图的状态用于存储消息和 LLM 调用次数。

    <Tip>
      LangGraph 中的状态在智能体执行期间持续存在。

      `MessagesValue` 提供了一个内置的归约器用于追加消息。`llmCalls` 字段使用带有 `(x, y) => x + y` 的 `ReducedValue` 来累积计数。
    </Tip>

    ```typescript theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
    import {
      StateGraph,
      StateSchema,
      MessagesValue,
      ReducedValue,
      GraphNode,
      ConditionalEdgeRouter,
      START,
      END,
    } from "@langchain/langgraph";
    import { z } from "zod/v4";

    const MessagesState = new StateSchema({
      messages: MessagesValue,
      llmCalls: new ReducedValue(
        z.number().default(0),
        { reducer: (x, y) => x + y }
      ),
    });
    ```

    ## 3. 定义模型节点

    模型节点用于调用 LLM 并决定是否调用工具。

    ```typescript theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
    import { SystemMessage } from "@langchain/core/messages";

    const llmCall: GraphNode<typeof MessagesState> = async (state) => {
      const response = await modelWithTools.invoke([
        new SystemMessage(
          "你是一个乐于助人的助手，负责对一组输入执行算术运算。"
        ),
        ...state.messages,
      ]);
      return {
        messages: [response],
        llmCalls: 1,
      };
    };
    ```

    ## 4. 定义工具节点

    工具节点用于调用工具并返回结果。

    ```typescript theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
    import { AIMessage, ToolMessage } from "@langchain/core/messages";

    const toolNode: GraphNode<typeof MessagesState> = async (state) => {
      const lastMessage = state.messages.at(-1);

      if (lastMessage == null || !AIMessage.isInstance(lastMessage)) {
        return { messages: [] };
      }

      const result: ToolMessage[] = [];
      for (const toolCall of lastMessage.tool_calls ?? []) {
        const tool = toolsByName[toolCall.name];
        const observation = await tool.invoke(toolCall);
        result.push(observation);
      }

      return { messages: result };
    };
    ```

    ## 5. 定义结束逻辑

    条件边函数用于根据 LLM 是否进行了工具调用来路由到工具节点或结束。

    ```typescript theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
    const shouldContinue: ConditionalEdgeRouter<typeof MessagesState, "toolNode"> = (state) => {
      const lastMessage = state.messages.at(-1);

      // 在访问 tool_calls 之前检查它是否是 AIMessage
      if (!lastMessage || !AIMessage.isInstance(lastMessage)) {
        return END;
      }

      // 如果 LLM 进行了工具调用，则执行操作
      if (lastMessage.tool_calls?.length) {
        return "toolNode";
      }

      // 否则，我们停止（回复用户）
      return END;
    };
    ```

    ## 6. 构建并编译智能体

    智能体使用 [`StateGraph`](https://reference.langchain.com/javascript/langchain-langgraph/index/StateGraph) 类构建，并使用 [`compile`](https://reference.langchain.com/javascript/classes/_langchain_langgraph.index.StateGraph.html#compile) 方法编译。

    ```typescript theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
    const agent = new StateGraph(MessagesState)
      .addNode("llmCall", llmCall)
      .addNode("toolNode", toolNode)
      .addEdge(START, "llmCall")
      .addConditionalEdges("llmCall", shouldContinue, ["toolNode", END])
      .addEdge("toolNode", "llmCall")
      .compile();

    // 调用
    import { HumanMessage } from "@langchain/core/messages";
    const result = await agent.invoke({
      messages: [new HumanMessage("将 3 和 4 相加。")],
    });

    for (const message of result.messages) {
      console.log(`[${message.type}]: ${message.text}`);
    }
    ```

    <Tip>
      要了解如何使用 LangSmith 追踪你的智能体，请参阅 [LangSmith 文档](/langsmith/trace-with-langgraph)。
    </Tip>

    恭喜！你已经使用 LangGraph 图 API 构建了你的第一个智能体。

    <Accordion title="完整代码示例">
      ```typescript theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
      // 步骤 1: 定义工具和模型

      import { ChatAnthropic } from "@langchain/anthropic";
      import { tool } from "@langchain/core/tools";
      import * as z from "zod";

      const model = new ChatAnthropic({
        model: "claude-sonnet-4-6",
        temperature: 0,
      });

      // 定义工具
      const add = tool(({ a, b }) => a + b, {
        name: "add",
        description: "将两个数字相加",
        schema: z.object({
          a: z.number().describe("第一个数字"),
          b: z.number().describe("第二个数字"),
        }),
      });

      const multiply = tool(({ a, b }) => a * b, {
        name: "multiply",
        description: "将两个数字相乘",
        schema: z.object({
          a: z.number().describe("第一个数字"),
          b: z.number().describe("第二个数字"),
        }),
      });

      const divide = tool(({ a, b }) => a / b, {
        name: "divide",
        description: "将两个数字相除",
        schema: z.object({
          a: z.number().describe("第一个数字"),
          b: z.number().describe("第二个数字"),
        }),
      });

      // 为 LLM 增强工具功能
      const toolsByName = {
        [add.name]: add,
        [multiply.name]: multiply,
        [divide.name]: divide,
      };
      const tools = Object.values(toolsByName);
      const modelWithTools = model.bindTools(tools);
      ```

      ```typescript theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
      // 步骤 2: 定义状态

      import {
        StateGraph,
        StateSchema,
        MessagesValue,
        ReducedValue,
        GraphNode,
        ConditionalEdgeRouter,
        START,
        END,
      } from "@langchain/langgraph";
      import * as z from "zod";

      const MessagesState = new StateSchema({
        messages: MessagesValue,
        llmCalls: new ReducedValue(
          z.number().default(0),
          { reducer: (x, y) => x + y }
        ),
      });
      ```

      ```typescript theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
      // 步骤 3: 定义模型节点

      import { SystemMessage, AIMessage, ToolMessage } from "@langchain/core/messages";

      const llmCall: GraphNode<typeof MessagesState> = async (state) => {
        return {
          messages: [await modelWithTools.invoke([
            new SystemMessage(
              "你是一个乐于助人的助手，负责对一组输入执行算术运算。"
            ),
            ...state.messages,
          ])],
          llmCalls: 1,
        };
      };

      // 步骤 4: 定义工具节点

      const toolNode: GraphNode<typeof MessagesState> = async (state) => {
        const lastMessage = state.messages.at(-1);

        if (lastMessage == null || !AIMessage.isInstance(lastMessage)) {
          return { messages: [] };
        }

        const result: ToolMessage[] = [];
        for (const toolCall of lastMessage.tool_calls ?? []) {
          const tool = toolsByName[toolCall.name];
          const observation = await tool.invoke(toolCall);
          result.push(observation);
        }

        return { messages: result };
      };
      ```

      ```typescript theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
      // 步骤 5: 定义决定是否结束的逻辑
      import { ConditionalEdgeRouter, END } from "@langchain/langgraph";

      const shouldContinue: ConditionalEdgeRouter<typeof MessagesState, "toolNode"> = (state) => {
        const lastMessage = state.messages.at(-1);

        // 在访问 tool_calls 之前检查它是否是 AIMessage
        if (!lastMessage || !AIMessage.isInstance(lastMessage)) {
          return END;
        }

        // 如果 LLM 进行了工具调用，则执行操作
        if (lastMessage.tool_calls?.length) {
          return "toolNode";
        }

        // 否则，我们停止（回复用户）
        return END;
      };
      ```

      ```typescript theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
      // 步骤 6: 构建并编译智能体
      import { HumanMessage } from "@langchain/core/messages";
      import { StateGraph, START, END } from "@langchain/langgraph";

      const agent = new StateGraph(MessagesState)
        .addNode("llmCall", llmCall)
        .addNode("toolNode", toolNode)
        .addEdge(START, "llmCall")
        .addConditionalEdges("llmCall", shouldContinue, ["toolNode", END])
        .addEdge("toolNode", "llmCall")
        .compile();

      // 调用
      const result = await agent.invoke({
        messages: [new HumanMessage("将 3 和 4 相加。")],
      });

      for (const message of result.messages) {
        console.log(`[${message.type}]: ${message.text}`);
      }
      ```
    </Accordion>
  </Tab>

  <Tab title="使用函数式 API">
    ## 1. 定义工具和模型

    在此示例中，我们将使用 Claude Sonnet 4.5 模型，并定义用于加法、乘法和除法的工具。

    ```typescript theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
    import { ChatAnthropic } from "@langchain/anthropic";
    import { tool } from "@langchain/core/tools";
    import * as z from "zod";

    const model = new ChatAnthropic({
      model: "claude-sonnet-4-6",
      temperature: 0,
    });

    // 定义工具
    const add = tool(({ a, b }) => a + b, {
      name: "add",
      description: "将两个数字相加",
      schema: z.object({
        a: z.number().describe("第一个数字"),
        b: z.number().describe("第二个数字"),
      }),
    });

    const multiply = tool(({ a, b }) => a * b, {
      name: "multiply",
      description: "将两个数字相乘",
      schema: z.object({
        a: z.number().describe("第一个数字"),
        b: z.number().describe("第二个数字"),
      }),
    });

    const divide = tool(({ a, b }) => a / b, {
      name: "divide",
      description: "将两个数字相除",
      schema: z.object({
        a: z.number().describe("第一个数字"),
        b: z.number().describe("第二个数字"),
      }),
    });

    // 为 LLM 增强工具功能
    const toolsByName = {
      [add.name]: add,
      [multiply.name]: multiply,
      [divide.name]: divide,
    };
    const tools = Object.values(toolsByName);
    const modelWithTools = model.bindTools(tools);

    ```

    ## 2. 定义模型节点

    模型节点用于调用 LLM 并决定是否调用工具。

    ```typescript theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
    import { task, entrypoint } from "@langchain/langgraph";
    import { SystemMessage } from "@langchain/core/messages";

    const callLlm = task({ name: "callLlm" }, async (messages: BaseMessage[]) => {
      return modelWithTools.invoke([
        new SystemMessage(
          "你是一个乐于助人的助手，负责对一组输入执行算术运算。"
        ),
        ...messages,
      ]);
    });
    ```

    ## 3. 定义工具节点

    工具节点用于调用工具并返回结果。

    ```typescript theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
    import type { ToolCall } from "@langchain/core/messages/tool";

    const callTool = task({ name: "callTool" }, async (toolCall: ToolCall) => {
      const tool = toolsByName[toolCall.name];
      return tool.invoke(toolCall);
    });
    ```

    ## 4. 定义智能体

    ```typescript theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
    import { addMessages } from "@langchain/langgraph";
    import { type BaseMessage } from "@langchain/core/messages";

    const agent = entrypoint({ name: "agent" }, async (messages: BaseMessage[]) => {
      let modelResponse = await callLlm(messages);

      while (true) {
        if (!modelResponse.tool_calls?.length) {
          break;
        }

        // 执行工具
        const toolResults = await Promise.all(
          modelResponse.tool_calls.map((toolCall) => callTool(toolCall))
        );
        messages = addMessages(messages, [modelResponse, ...toolResults]);
        modelResponse = await callLlm(messages);
      }

      return messages;
    });

    // 调用
    import { HumanMessage } from "@langchain/core/messages";

    const result = await agent.invoke([new HumanMessage("将 3 和 4 相加。")]);

    for (const message of result) {
      console.log(`[${message.getType()}]: ${message.text}`);
    }
    ```

    <Tip>
      要了解如何使用 LangSmith 追踪你的智能体，请参阅 [LangSmith 文档](/langsmith/trace-with-langgraph)。
    </Tip>

    恭喜！你已经使用 LangGraph 函数式 API 构建了你的第一个智能体。

    <Accordion title="完整代码示例" icon="code">
      ```typescript theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
      import { ChatAnthropic } from "@langchain/anthropic";
      import { tool } from "@langchain/core/tools";
      import {
        task,
        entrypoint,
        addMessages,
      } from "@langchain/langgraph";
      import {
        SystemMessage,
        HumanMessage,
        type BaseMessage,
      } from "@langchain/core/messages";
      import type { ToolCall } from "@langchain/core/messages/tool";
      import * as z from "zod";

      // 步骤 1: 定义工具和模型

      const model = new ChatAnthropic({
        model: "claude-sonnet-4-6",
        temperature: 0,
      });

      // 定义工具
      const add = tool(({ a, b }) => a + b, {
        name: "add",
        description: "将两个数字相加",
        schema: z.object({
          a: z.number().describe("第一个数字"),
          b: z.number().describe("第二个数字"),
        }),
      });

      const multiply = tool(({ a, b }) => a * b, {
        name: "multiply",
        description: "将两个数字相乘",
        schema: z.object({
          a: z.number().describe("第一个数字"),
          b: z.number().describe("第二个数字"),
        }),
      });

      const divide = tool(({ a, b }) => a / b, {
        name: "divide",
        description: "将两个数字相除",
        schema: z.object({
          a: z.number().describe("第一个数字"),
          b: z.number().describe("第二个数字"),
        }),
      });

      // 为 LLM 增强工具功能
      const toolsByName = {
        [add.name]: add,
        [multiply.name]: multiply,
        [divide.name]: divide,
      };
      const tools = Object.values(toolsByName);
      const modelWithTools = model.bindTools(tools);

      // 步骤 2: 定义模型节点

      const callLlm = task({ name: "callLlm" }, async (messages: BaseMessage[]) => {
        return modelWithTools.invoke([
          new SystemMessage(
            "你是一个乐于助人的助手，负责对一组输入执行算术运算。"
          ),
          ...messages,
        ]);
      });

      // 步骤 3: 定义工具节点

      const callTool = task({ name: "callTool" }, async (toolCall: ToolCall) => {
        const tool = toolsByName[toolCall.name];
        return tool.invoke(toolCall);
      });

      // 步骤 4: 定义智能体

      const agent = entrypoint({ name: "agent" }, async (messages: BaseMessage[]) => {
        let modelResponse = await callLlm(messages);

        while (true) {
          if (!modelResponse.tool_calls?.length) {
            break;
          }

          // 执行工具
          const toolResults = await Promise.all(
            modelResponse.tool_calls.map((toolCall) => callTool(toolCall))
          );
          messages = addMessages(messages, [modelResponse, ...toolResults]);
          modelResponse = await callLlm(messages);
        }

        return messages;
      });

      // 调用

      const result = await agent.invoke([new HumanMessage("将 3 和 4 相加。")]);

      for (const message of result) {
        console.log(`[${message.type}]: ${message.text}`);
      }
      ```
    </Accordion>
  </Tab>
</Tabs>

***

<div className="source-links">
  <Callout icon="edit">
    [Edit this page on GitHub](https://github.com/langchain-ai/docs/edit/main/src/i18n\zh-CN\oss\langgraph\quickstart.mdx) or [file an issue](https://github.com/langchain-ai/docs/issues/new/choose).
  </Callout>

  <Callout icon="terminal-2">
    [Connect these docs](/use-these-docs) to Claude, VSCode, and more via MCP for real-time answers.
  </Callout>
</div>
