There are many use cases where you may wish for your node to have a custom retry policy, for example if you are calling an API, querying a database, or calling an LLM, etc.
Sign up for LangSmith to quickly spot issues and improve the performance of your LangGraph projects. LangSmith lets you use trace data to debug, test, and monitor your LLM apps built with LangGraph — read more about how to get started here.
In order to configure the retry policy, you have to pass the retry parameter to the add_node. The retry parameter takes in a RetryPolicy named tuple object. Below we instantiate a RetryPolicy object with the default parameters:
Lastly, we can pass RetryPolicy objects when we call the add_node function. In the example below we pass two different retry policies to each of our nodes:
importoperatorimportsqlite3fromtypingimportAnnotated,Sequencefromtyping_extensionsimportTypedDictfromlangchain_anthropicimportChatAnthropicfromlangchain_core.messagesimportBaseMessagefromlanggraph.graphimportEND,StateGraph,STARTfromlangchain_community.utilitiesimportSQLDatabasefromlangchain_core.messagesimportAIMessagedb=SQLDatabase.from_uri("sqlite:///:memory:")model=ChatAnthropic(model_name="claude-2.1")classAgentState(TypedDict):messages:Annotated[Sequence[BaseMessage],operator.add]defquery_database(state):query_result=db.run("SELECT * FROM Artist LIMIT 10;")return{"messages":[AIMessage(content=query_result)]}defcall_model(state):response=model.invoke(state["messages"])return{"messages":[response]}# Define a new graphbuilder=StateGraph(AgentState)builder.add_node("query_database",query_database,retry=RetryPolicy(retry_on=sqlite3.OperationalError),)builder.add_node("model",call_model,retry=RetryPolicy(max_attempts=5))builder.add_edge(START,"model")builder.add_edge("model","query_database")builder.add_edge("query_database",END)graph=builder.compile()