Visitar URL original
Quickstart - Docs by LangChain
Skip to main content
This quickstart demonstrates how to build a calculator agent with the LangGraph Graph API. Already have an agent and want to deploy it? See LangSmith Agent Server.
Using an AI coding assistant?
  • Install the LangChain Docs MCP servers to give your agent access to up-to-date LangChain documentation and examples.

    Connect LangChain docs MCP servers

  • Install LangChain Skills to improve your agent’s performance on LangChain ecosystem tasks.

    Install LangChain Skills

Prerequisites

Before you begin, make sure you have an API key from a model provider (e.g., Gemini, Anthropic, OpenAI). See chat model integrations for all available providers. If you use LangSmith Gateway, you can bring your own provider keys or use Gateway Credits to access models without a provider key.

Setup

Install @langchain/langgraph, @langchain/core, @langchain/anthropic, langchain, and zod:
This example uses Anthropic. Set your API key in the shell or a .env file:
To use another provider, see chat model integrations and adapt the model initialization in the next steps.

Build the agent

The model can call tools, then the agent calls the model again until it returns a final answer. Create one file. Add the code from each step below the previous step. For conceptual information, see the Graph API overview.

Build the LangGraph Graph API calculator quickstart

1. Define tools and model

This example uses the Claude Sonnet model and defines tools for addition, multiplication, and division. Create a file for the agent (agent.py or agent.ts). Add the following:

2. Define state

The graph’s state stores the messages and the number of LLM calls. Add the following below the tools and model:
State in LangGraph persists throughout the agent’s execution.MessagesValue appends messages with a built-in reducer. The llmCalls field uses a ReducedValue with (x, y) => x + y to accumulate the count.

3. Define model node

The model node calls the LLM and decides whether to call a tool. Add the following below the state schema:

4. Define tool node

The tool node calls the tools and returns the results. Add the following below the model node:

5. Define end logic

The conditional edge function routes to the tool node or to the end, depending on whether the LLM made a tool call. Add the following below the tool node:

6. Build and compile the agent

Add the following below the routing function. Build the graph with StateGraph and compile it with compile:

7. Set up LangSmith tracing

LangSmith shows tool calls and model responses as the agent runs. This step is optional. Sign up at smith.langchain.com, create an API key, and set these environment variables in the shell before you run the agent:

8. Run the agent

Add the following below the compiled graph:
Run the file:
You should see the human message, a tool call to add, the tool result 7, and a final assistant reply. The exact text varies by model, but the tool result is 7. To check your file, compare it with the full example:

Next steps

You now have a working calculator agent. Continue with one of the following subjects: