How to access LangGraph state values and include them in the system prompt of a React agent node

I’m building a LangGraph where one of the nodes is a React agent. The agent is created like this:

agent_node = create_agent(
    model=llm,
    prompt=system_prompt,
    tools=tools,
    debug=True,
    name="react_new_agent"
)

My state looks like this:

class BasicChatState(TypedDict):
    user_query: Optional[str]
    messages: Annotated[list, add_messages]
    instructions: Optional[str]

In an earlier node (rag_node), I add some text to state["instructions"].

Now, in the agent_node, I want to access this instructions value from the state and pass it along with the system prompt (so the agent system prompt is dynamically extended with the instructions).

How can I do this in LangGraph ?

prompt in create_react_agent() can be a callable/runnable, you can try like below

def get_prompt(state: State):
    instructions = state["instructions"]
    instructions_message = HumanMessage(content=f"{instructions}.")
    system_message = SystemMessage(content=f"You are a helpful assistant.")
    return [system_message, instructions_message]

agent1 = create_react_agent(
    model = llm,
    tools = [add_numbers],
    prompt = get_prompt
)

Hi @Najiya

@heisenberg-7 is right. Just small version notes (jic you are using one of the older versions):

Version notes

@heisenberg-7
@pawel-twardziak
Thank you both for your help! I was able to find a different approach to get the desired result. Here’s the solution I came up with:

 def agent_node(state: BasicChatState) -> BasicChatState:
            instruction = state.get('instructions')
            dynamic_prompt = f"""{system_prompt}

            Additional instructions: {instruction}
            """
            agent = create_agent(
                model=llm,
                prompt=dynamic_prompt,
                tools=tools,
                debug=True,
                name="react_new_agent"
            )
            return agent