Build a LangGraph AI Agent in Two Lines Using the Prebuilt Graph API
This tutorial shows how to set up a Python environment, install LangGraph, and use its high‑level prebuilt graph API—specifically create_react_agent—to quickly create a weather‑assistant AI agent with just two lines of code, illustrating the full tool‑calling workflow and ReACT loop.
This article continues a series on LangChain & LangGraph, focusing on the highest‑level prebuilt graph API to rapidly construct an AI agent.
LangGraph Environment Setup
First, create and activate a Conda environment named langgraphenv and install the required packages.
conda create -n langgraphenv python=3.12 # create env
conda activate langgraphenv
pip install langchain langgraph langchain
pip show langgraph # verify versionInstall the DeepSeek model components and OpenAI‑compatible tools:
pip install langchain-deepseek openaiPrebuilt Agent API Overview
LangGraph provides several high‑level templates:
create_react_agent : the most general template that lets the LLM automatically select and orchestrate any supplied tools, iteratively reasoning and acting until a final answer is produced.
create_tool_calling_agent : intended for performance debugging of a single tool.
create_openai_functions_agent and create_openai_tools_agent : build agents that issue OpenAI‑style function‑calling requests (compatible with non‑OpenAI models as well).
Weather Assistant Case Study
Define a custom weather‑query tool using pydantic to describe the input schema:
import requests
from langchain_core.tools import tool
from pydantic import BaseModel, Field
class WeatherQuery(BaseModel):
loc: str = Field(description="城市名称")
@tool(args_schema=WeatherQuery)
def get_weather(loc):
"""Query real‑time weather from Seniverse API.
:param loc: city name
:return: JSON object with weather details"""
url = "https://api.seniverse.com/v3/weather/now.json"
params = {
"key": "你的心知天气私钥",
"location": loc,
"language": "zh-Hans",
"unit": "c",
}
response = requests.get(url, params=params)
return response.json()['results'][0]['now']
print(f'''name: {get_weather.name}
description: {get_weather.description}
arguments: {get_weather.args}
''')Initialize the LLM (DeepSeek‑Chat) and assemble the tool list:
from langchain.chat_models import init_chat_model
model = init_chat_model(model='deepseek-chat', model_provider='deepseek', api_key='你的DeepSeek api_key')
tools = [get_weather]Create the ReACT agent and invoke it with a simple greeting:
from langgraph.prebuilt import create_react_agent
agent = create_react_agent(model=model, tools=tools)
response = agent.invoke({"messages": [{"role": "user", "content": "你好请你介绍一下你自己"}]})
print(response)Query the weather for Beijing, demonstrating the function‑calling loop:
response = agent.invoke({"messages": [{"role": "user", "content": "北京现在的天气如何?"}]})
print(response)The agent selects get_weather, supplies the city name, receives the API result, and returns a natural‑language answer, illustrating the ReACT cycle of think → act → observe → rethink → answer .
ReACT Graph Structure Overview
LangGraph builds workflows from Node s and Edge s, maintaining a mutable State that is updated after each tool execution. The loop proceeds as follows:
Start: receive user input.
Call model: pass current state to the LLM for reasoning.
Model decision: either produce a final answer or request a tool call.
Execute tool: run the selected function and obtain results.
Update state: incorporate tool output.
Loop: feed the updated state back to the model.
End: when the model returns a final answer, the graph terminates and the answer is returned to the user.
This abstraction lets developers express complex iterative reasoning with a single API call, dramatically accelerating agent development.
Conclusion
The prebuilt graph API enables the construction of a functional LangGraph agent with only two lines of code after environment setup. It supports custom tools, integrates seamlessly with LangChain’s extensive tool library, and offers fine‑grained control over tool usage for future extensions.
Fun with Large Models
Master's graduate from Beijing Institute of Technology, published four top‑journal papers, previously worked as a developer at ByteDance and Alibaba. Currently researching large models at a major state‑owned enterprise. Committed to sharing concise, practical AI large‑model development experience, believing that AI large models will become as essential as PCs in the future. Let's start experimenting now!
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
