In this how-to we'll create a simple ReAct agent app that can check the weather. The app consists of an agent (LLM) and tools. As we interact with the app, we will first call the agent (LLM) to decide if we should use tools. Then we will run a loop:
If the agent said to take an action (i.e. call tool), we'll run the tools and pass the results back to the agent
If the agent did not ask to run tools, we will finish (respond to the user)
Prebuilt Agent
Please note that here will we use a prebuilt agent. One of the big benefits of LangGraph is that you can easily create your own agent architectures. So while it's fine to start here to build an agent quickly, we would strongly recommend learning how to build your own agent so that you can take full advantage of LangGraph. Read this guide to learn how to create your own ReAct agent from scratch.
Now we can use the prebuilt createReactAgent function to setup our agent:
import{ChatOpenAI}from"@langchain/openai";import{tool}from'@langchain/core/tools';import{z}from'zod';import{createReactAgent}from"@langchain/langgraph/prebuilt";constmodel=newChatOpenAI({model:"gpt-4o",});constgetWeather=tool((input)=>{if(['sf','san francisco','san francisco, ca'].includes(input.location.toLowerCase())){return'It\'s 60 degrees and foggy.';}else{return'It\'s 90 degrees and sunny.';}},{name:'get_weather',description:'Call to get the current weather.',schema:z.object({location:z.string().describe("Location to get the weather for."),})})constagent=createReactAgent({llm:model,tools:[getWeather]});
Let's run the app with an input that needs a tool call
letinputs={messages:[{role:"user",content:"what is the weather in SF?"}]};letstream=awaitagent.stream(inputs,{streamMode:"values",});forawait(const{messages}ofstream){letmsg=messages[messages?.length-1];if(msg?.content){console.log(msg.content);}elseif(msg?.tool_calls?.length>0){console.log(msg.tool_calls);}else{console.log(msg);}console.log("-----\n");}
what is the weather in sf?-----[ { name: 'get_weather', args: { location: 'San Francisco, CA' }, type: 'tool_call', id: 'call_wfXCh5IhSp1C0Db3gaJWDbRP' }]-----It's 60 degrees and foggy.-----The weather in San Francisco is currently 60 degrees and foggy.-----
Now let's try a question that doesn't need tools
inputs={messages:[{role:"user",content:"who built you?"}]};stream=awaitagent.stream(inputs,{streamMode:"values",});forawait(const{messages}ofstream){letmsg=messages[messages?.length-1];if(msg?.content){console.log(msg.content);}elseif(msg?.tool_calls?.length>0){console.log(msg.tool_calls);}else{console.log(msg);}console.log("-----\n");}
who built you?-----I was developed by OpenAI, an AI research and deployment company.-----
Perfect! The agent correctly didn't call any tools and instead directly responded to the user.