Skip to main content
This quickstart demonstrates how to build a calculator agent using the LangGraph Graph API or the Functional API.
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 langgraph, langchain, and langchain-anthropic:
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.

1. Choose the API

Both paths build the same calculator agent: the model can call tools, then the agent calls the model again until it returns a final answer. Pick a tab. Create one file. Add the code from each step below the previous step. Not sure which to pick? See Choosing between Graph and Functional APIs. For conceptual information, see Graph API overview and Functional API overview.

Build the LangGraph Graph API calculator quickstart

2. 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:

3. 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.The Annotated type with operator.add appends new messages to the list instead of replacing it.

4. Define model node

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

5. Define tool node

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

6. 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:

7. Build and compile the agent

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

8. 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:

9. 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: