State acts as short-term memory during a run. It holds dynamic data that can evolve during execution, such as values derived from tools or LLM outputs.
constCustomState=Annotation.Root({...MessagesAnnotation.spec,userName:Annotation<string>,});constagent=createReactAgent({// Other agent parameters...stateSchema:CustomState,})awaitagent.invoke({messages:"hi!",userName:"Jane"})
Turning on memory
Please see the memory guide for more details on how to enable memory. This is a powerful feature that allows you to persist the agent's state across multiple invocations.
Otherwise, the state is scoped only to a single agent run.
For context that spans across conversations or sessions, LangGraph allows access to long-term memory via a store. This can be used to read or update persistent facts (e.g., user profiles, preferences, prior interactions). For more, see the Memory guide.
Prompts define how the agent behaves. To incorporate runtime context, you can dynamically generate prompts based on the agent's state or config.
Common use cases:
Personalization
Role or goal customization
Conditional behavior (e.g., user is admin)
import{BaseMessageLike}from"@langchain/core/messages";import{RunnableConfig}from"@langchain/core/runnables";import{initChatModel}from"langchain/chat_models/universal";import{MessagesAnnotation}from"@langchain/langgraph";import{createReactAgent}from"@langchain/langgraph/prebuilt";constprompt=(state:typeofMessagesAnnotation.State,config:RunnableConfig):BaseMessageLike[]=>{constuserName=config.configurable?.userName;constsystemMsg=`You are a helpful assistant. Address the user as ${userName}.`;return[{role:"system",content:systemMsg},...state.messages];};constllm=awaitinitChatModel("anthropic:claude-3-7-sonnet-latest");constagent=createReactAgent({llm,tools:[getWeather],prompt});awaitagent.invoke({messages:"hi!"},{configurable:{userName:"John Smith"}});
import{BaseMessageLike}from"@langchain/core/messages";import{RunnableConfig}from"@langchain/core/runnables";import{initChatModel}from"langchain/chat_models/universal";import{Annotation,MessagesAnnotation}from"@langchain/langgraph";import{createReactAgent}from"@langchain/langgraph/prebuilt";constCustomState=Annotation.Root({...MessagesAnnotation.spec,userName:Annotation<string>,});constprompt=(state:typeofCustomState.State,):BaseMessageLike[]=>{constuserName=state.userName;constsystemMsg=`You are a helpful assistant. Address the user as ${userName}.`;return[{role:"system",content:systemMsg},...state.messages];};constllm=awaitinitChatModel("anthropic:claude-3-7-sonnet-latest");constagent=createReactAgent({llm,tools:[getWeather],prompt,stateSchema:CustomState,});awaitagent.invoke({messages:"hi!",userName:"John Smith"},);
import{RunnableConfig}from"@langchain/core/runnables";import{initChatModel}from"langchain/chat_models/universal";import{createReactAgent}from"@langchain/langgraph/prebuilt";import{tool}from"@langchain/core/tools";import{z}from"zod";constgetUserInfo=tool(async(input:Record<string,any>,config:RunnableConfig)=>{constuserId=config.configurable?.userId;returnuserId==="user_123"?"User is John Smith":"Unknown user";},{name:"get_user_info",description:"Look up user info.",schema:z.object({}),});constllm=awaitinitChatModel("anthropic:claude-3-7-sonnet-latest");constagent=createReactAgent({llm,tools:[getUserInfo],});awaitagent.invoke({messages:"look up user information"},{configurable:{userId:"user_123"}});
import{initChatModel}from"langchain/chat_models/universal";import{createReactAgent}from"@langchain/langgraph/prebuilt";import{Annotation,MessagesAnnotation,getCurrentTaskInput}from"@langchain/langgraph";import{tool}from"@langchain/core/tools";import{z}from"zod";constCustomState=Annotation.Root({...MessagesAnnotation.spec,userId:Annotation<string>(),});constgetUserInfo=tool(async(input:Record<string,any>,)=>{conststate=getCurrentTaskInput()astypeofCustomState.State;constuserId=state.userId;returnuserId==="user_123"?"User is John Smith":"Unknown user";},{name:"get_user_info",description:"Look up user info.",schema:z.object({})});constllm=awaitinitChatModel("anthropic:claude-3-7-sonnet-latest");constagent=createReactAgent({llm,tools:[getUserInfo],stateSchema:CustomState,});awaitagent.invoke({messages:"look up user information",userId:"user_123"});
Tools can modify the agent's state during execution. This is useful for persisting intermediate results or making information accessible to subsequent tools or prompts.
import{Annotation,MessagesAnnotation,LangGraphRunnableConfig,Command}from"@langchain/langgraph";import{tool}from"@langchain/core/tools";import{z}from"zod";import{ToolMessage}from"@langchain/core/messages";import{initChatModel}from"langchain/chat_models/universal";import{createReactAgent}from"@langchain/langgraph/prebuilt";constCustomState=Annotation.Root({...MessagesAnnotation.spec,userName:Annotation<string>(),// Will be updated by the tool});constgetUserInfo=tool(async(_input:Record<string,never>,config:LangGraphRunnableConfig):Promise<Command>=>{constuserId=config.configurable?.userId;if(!userId){thrownewError("Please provide a user id in config.configurable");}consttoolCallId=config.toolCall?.id;constname=userId==="user_123"?"John Smith":"Unknown user";// Return command to update statereturnnewCommand({update:{userName:name,// Update the message historymessages:[newToolMessage({content:"Successfully looked up user information",tool_call_id:toolCallId,}),],},});},{name:"get_user_info",description:"Look up user information.",schema:z.object({}),});constllm=awaitinitChatModel("anthropic:claude-3-7-sonnet-latest");constagent=createReactAgent({llm,tools:[getUserInfo],stateSchema:CustomState,});awaitagent.invoke({messages:"look up user information"},{configurable:{userId:"user_123"}});