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
Installlanggraph, langchain, and langchain-anthropic:
.env file:
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.- Use the Graph API if you prefer to define your agent as a graph of nodes and edges.
- Use the Functional API if you prefer to define your agent as a single function.
- Use the Graph API
- Use the Functional API
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: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 withStateGraph 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: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:Full code example
Full code example
Next steps
You now have a working calculator agent. Continue with one of the following subjects:- Learn the APIs: Choosing between Graph and Functional APIs, then the Build checklist for the Graph API or the Build checklist for the Functional API.
- Add persistence: Use a checkpointer so runs can pause and resume across sessions.
- Add human-in-the-loop: Pause with interrupts for approval or edits.
- Stream results: Follow streaming for tokens and state updates.
- Go deeper: Thinking in the Graph API, Thinking in the Functional API, and Workflows and agents.
- Deploy: Host the agent with LangSmith Deployment.
- Evaluate: Test behavior with LangSmith evaluation.
Connect these docs to your agent of choice via MCP for real-time answers.

