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 server to give your agent access to up-to-date LangChain documentation and examples.
  • Install LangChain Skills to improve your agent’s performance on LangChain ecosystem tasks.
For conceptual information, see Graph API overview and Functional API overview.
For this example, you will need to set up a Claude (Anthropic) account and get an API key. Then, set the ANTHROPIC_API_KEY environment variable in your terminal.

1. Define tools and model

In this example, we’ll use the Claude Sonnet 4.5 model and define tools for addition, multiplication, and division.

2. Define state

The graph’s state is used to store the messages and the number of LLM calls.
State in LangGraph persists throughout the agent’s execution.The Annotated type with operator.add ensures that new messages are appended to the existing list rather than replacing it.

3. Define model node

The model node is used to call the LLM and decide whether to call a tool or not.

4. Define tool node

The tool node is used to call the tools and return the results.

5. Define end logic

The conditional edge function is used to route to the tool node or end based upon whether the LLM made a tool call.

6. Build and compile the agent

The agent is built using the StateGraph class and compiled using the compile method.
Trace and debug your agent with LangSmith. Follow the tracing quickstart to get set up. When ready for production, see Deploy for hosting options.We recommend you also set up LangSmith Engine which monitors your traces, detects issues, and proposes fixes.
Congratulations! You’ve built your first agent using the LangGraph Graph API.