We can simply use the command-update method to do this. This is the basic example how it can be implemented.
from langgraph.types import Command
@tool
def send_otp(
phone_number: str,
tool_call_id: Annotated[str, InjectedToolCallId],
) -> Command:
"""Send OTP to the (already validated) phone number."""
logger.info("-----------------------------------send_otp-----------------------------------")
formatted_number = phone_number
otp_code = OTPService().generate_otp()
# In production, actually send SMS here:
# OTPService().send_otp(formatted_number, otp_code)
payload = {
"otp": otp_code, # stored in state; avoid showing to user in production
"phoneNumber": formatted_number,
"message": f"OTP sent successfully to {formatted_number}. Please check your phone.",
}
logger.info(f"Send OTP tool payload: {payload}")
return Command(
update={
"otp": otp_code,
"phoneNumber": formatted_number,
"messages": [
ToolMessage(content=json.dumps(payload), tool_call_id=tool_call_id)
],
}
)
Refer to this documentation to understand more: https://langchain-ai.github.io/langgraph/how-tos/tool-calling/#short-term-memory