{
  "cells": [
    {
      "cell_type": "markdown",
      "id": "51466c8d-8ce4-4b3d-be4e-18fdbeda5f53",
      "metadata": {},
      "source": [
        "# How to view and update past graph state\n",
        "\n",
        "Once you start [checkpointing](../persistence.ipynb) your graphs, you can easily **get** or **update** the state of the agent at any point in time. This permits a few things:\n",
        "\n",
        "1. You can surface a state during an interrupt to a user to let them accept an action.\n",
        "2. You can **rewind** the graph to reproduce or avoid issues.\n",
        "3. You can **modify** the state to embed your agent into a larger system, or to let the user better control its actions.\n",
        "\n",
        "The key methods used for this functionality are:\n",
        "\n",
        "- [get_state](https://langchain-ai.github.io/langgraph/reference/graphs/#langgraph.graph.graph.CompiledGraph.get_state): fetch the values from the target config\n",
        "- [update_state](https://langchain-ai.github.io/langgraph/reference/graphs/#langgraph.graph.graph.CompiledGraph.update_state): apply the given values to the target state\n",
        "\n",
        "**Note:** this requires passing in a checkpointer.\n",
        "\n",
        "Below is a quick example."
      ]
    },
    {
      "cell_type": "markdown",
      "id": "7cbd446a-808f-4394-be92-d45ab818953c",
      "metadata": {},
      "source": [
        "## Setup\n",
        "\n",
        "First we need to install the packages required"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 1,
      "id": "af4ce0ba-7596-4e5f-8bf8-0b0bd6e62833",
      "metadata": {},
      "outputs": [],
      "source": [
        "%%capture --no-stderr\n%pip install --quiet -U langgraph langchain_anthropic"
      ]
    },
    {
      "cell_type": "markdown",
      "id": "0abe11f4-62ed-4dc4-8875-3db21e260d1d",
      "metadata": {},
      "source": [
        "Next, we need to set API keys for Anthropic (the LLM we will use)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 2,
      "id": "c903a1cf-2977-4e2d-ad7d-8b3946821d89",
      "metadata": {},
      "outputs": [
        {
          "name": "stdin",
          "output_type": "stream",
          "text": [
            "ANTHROPIC_API_KEY:  ········\n"
          ]
        }
      ],
      "source": [
        "import getpass\nimport os\n\n\ndef _set_env(var: str):\n    if not os.environ.get(var):\n        os.environ[var] = getpass.getpass(f\"{var}: \")\n\n\n_set_env(\"ANTHROPIC_API_KEY\")"
      ]
    },
    {
      "cell_type": "markdown",
      "id": "f0ed46a8-effe-4596-b0e1-a6a29ee16f5c",
      "metadata": {},
      "source": [
        "Optionally, we can set API key for [LangSmith tracing](https://smith.langchain.com/), which will give us best-in-class observability."
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 3,
      "id": "95e25aec-7c9f-4a63-b143-225d0e9a79c3",
      "metadata": {},
      "outputs": [],
      "source": [
        "os.environ[\"LANGCHAIN_TRACING_V2\"] = \"true\"\n_set_env(\"LANGCHAIN_API_KEY\")"
      ]
    },
    {
      "cell_type": "markdown",
      "id": "e36f89e5",
      "metadata": {},
      "source": [
        "## Build the agent\n",
        "\n",
        "We can now build the agent. We will build a relatively simple ReAct-style agent that does tool calling. We will use Anthropic's models and a fake tool (just for demo purposes)."
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 4,
      "id": "f5319e01",
      "metadata": {},
      "outputs": [],
      "source": [
        "# Set up the tool\n",
        "from langchain_anthropic import ChatAnthropic\n",
        "from langchain_core.tools import tool\n",
        "from langgraph.graph import MessagesState, START\n",
        "from langgraph.prebuilt import ToolNode\n",
        "from langgraph.graph import END, StateGraph\n",
        "from langgraph.checkpoint.memory import MemorySaver\n",
        "\n",
        "\n",
        "@tool\n",
        "def search(query: str):\n",
        "    \"\"\"Call to surf the web.\"\"\"\n",
        "    # This is a placeholder for the actual implementation\n",
        "    # Don't let the LLM know this though 😊\n",
        "    return [\n",
        "        \"It's sunny in San Francisco, but you better look out if you're a Gemini 😈.\"\n",
        "    ]\n",
        "\n",
        "\n",
        "tools = [search]\n",
        "tool_node = ToolNode(tools)\n",
        "\n",
        "# Set up the model\n",
        "\n",
        "model = ChatAnthropic(model=\"claude-3-5-sonnet-20240620\")\n",
        "model = model.bind_tools(tools)\n",
        "\n",
        "\n",
        "# Define nodes and conditional edges\n",
        "\n",
        "\n",
        "# Define the function that determines whether to continue or not\n",
        "def should_continue(state):\n",
        "    messages = state[\"messages\"]\n",
        "    last_message = messages[-1]\n",
        "    # If there is no function call, then we finish\n",
        "    if not last_message.tool_calls:\n",
        "        return \"end\"\n",
        "    # Otherwise if there is, we continue\n",
        "    else:\n",
        "        return \"continue\"\n",
        "\n",
        "\n",
        "# Define the function that calls the model\n",
        "def call_model(state):\n",
        "    messages = state[\"messages\"]\n",
        "    response = model.invoke(messages)\n",
        "    # We return a list, because this will get added to the existing list\n",
        "    return {\"messages\": [response]}\n",
        "\n",
        "\n",
        "# Define a new graph\n",
        "workflow = StateGraph(MessagesState)\n",
        "\n",
        "# Define the two nodes we will cycle between\n",
        "workflow.add_node(\"agent\", call_model)\n",
        "workflow.add_node(\"action\", tool_node)\n",
        "\n",
        "# Set the entrypoint as `agent`\n",
        "# This means that this node is the first one called\n",
        "workflow.add_edge(START, \"agent\")\n",
        "\n",
        "# We now add a conditional edge\n",
        "workflow.add_conditional_edges(\n",
        "    # First, we define the start node. We use `agent`.\n",
        "    # This means these are the edges taken after the `agent` node is called.\n",
        "    \"agent\",\n",
        "    # Next, we pass in the function that will determine which node is called next.\n",
        "    should_continue,\n",
        "    # Finally we pass in a mapping.\n",
        "    # The keys are strings, and the values are other nodes.\n",
        "    # END is a special node marking that the graph should finish.\n",
        "    # What will happen is we will call `should_continue`, and then the output of that\n",
        "    # will be matched against the keys in this mapping.\n",
        "    # Based on which one it matches, that node will then be called.\n",
        "    {\n",
        "        # If `tools`, then we call the tool node.\n",
        "        \"continue\": \"action\",\n",
        "        # Otherwise we finish.\n",
        "        \"end\": END,\n",
        "    },\n",
        ")\n",
        "\n",
        "# We now add a normal edge from `tools` to `agent`.\n",
        "# This means that after `tools` is called, `agent` node is called next.\n",
        "workflow.add_edge(\"action\", \"agent\")\n",
        "\n",
        "# Set up memory\n",
        "memory = MemorySaver()\n",
        "\n",
        "# Finally, we compile it!\n",
        "# This compiles it into a LangChain Runnable,\n",
        "# meaning you can use it as you would any other runnable\n",
        "\n",
        "# We add in `interrupt_before=[\"action\"]`\n",
        "# This will add a breakpoint before the `action` node is called\n",
        "app = workflow.compile(checkpointer=memory)"
      ]
    },
    {
      "cell_type": "markdown",
      "id": "2a1b56c5-bd61-4192-8bdb-458a1e9f0159",
      "metadata": {},
      "source": [
        "## Interacting with the Agent\n",
        "\n",
        "We can now interact with the agent. Let's ask it for the weather in SF.\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 5,
      "id": "cfd140f0-a5a6-4697-8115-322242f197b5",
      "metadata": {},
      "outputs": [
        {
          "name": "stdout",
          "output_type": "stream",
          "text": [
            "================================\u001b[1m Human Message \u001b[0m=================================\n",
            "\n",
            "Use the search tool to look up the weather in SF\n",
            "==================================\u001b[1m Ai Message \u001b[0m==================================\n",
            "\n",
            "[{'text': \"Certainly! I'll use the search tool to look up the weather in San Francisco for you. Let me do that right away.\", 'type': 'text'}, {'id': 'toolu_01Bpq6yiKqk9moPuGYKdLr8r', 'input': {'query': 'weather in San Francisco'}, 'name': 'search', 'type': 'tool_use'}]\n",
            "Tool Calls:\n",
            "  search (toolu_01Bpq6yiKqk9moPuGYKdLr8r)\n",
            " Call ID: toolu_01Bpq6yiKqk9moPuGYKdLr8r\n",
            "  Args:\n",
            "    query: weather in San Francisco\n",
            "=================================\u001b[1m Tool Message \u001b[0m=================================\n",
            "Name: search\n",
            "\n",
            "[\"It's sunny in San Francisco, but you better look out if you're a Gemini \\ud83d\\ude08.\"]\n",
            "==================================\u001b[1m Ai Message \u001b[0m==================================\n",
            "\n",
            "Based on the search results, I can provide you with information about the weather in San Francisco:\n",
            "\n",
            "The current weather in San Francisco is sunny. This is great news for residents and visitors who want to enjoy outdoor activities or explore the city.\n",
            "\n",
            "However, there's an interesting and somewhat humorous addition to the weather report. It mentions, \"but you better look out if you're a Gemini 😈.\" This appears to be a playful reference to astrology, suggesting that Geminis might have some challenges despite the good weather. Of course, this is not a scientific weather prediction and is likely just a fun addition to the report.\n",
            "\n",
            "To summarize:\n",
            "1. The weather in San Francisco is currently sunny.\n",
            "2. It's a good day for outdoor activities.\n",
            "3. There's a playful astrological warning for Geminis, but this shouldn't be taken seriously in terms of actual weather conditions.\n",
            "\n",
            "Is there anything else you'd like to know about the weather in San Francisco or any other location?\n"
          ]
        }
      ],
      "source": [
        "from langchain_core.messages import HumanMessage\n\nconfig = {\"configurable\": {\"thread_id\": \"1\"}}\ninput_message = HumanMessage(content=\"Use the search tool to look up the weather in SF\")\nfor event in app.stream({\"messages\": [input_message]}, config, stream_mode=\"values\"):\n    event[\"messages\"][-1].pretty_print()"
      ]
    },
    {
      "cell_type": "markdown",
      "id": "1c38c505-6cee-427f-9dcd-493a2ade7ebb",
      "metadata": {},
      "source": [
        "## Checking history\n",
        "\n",
        "Let's browse the history of this thread, from start to finish."
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 6,
      "id": "8578a66d-6489-4e03-8c23-fd0530278455",
      "metadata": {},
      "outputs": [
        {
          "name": "stdout",
          "output_type": "stream",
          "text": [
            "StateSnapshot(values={'messages': []}, next=('__start__',), config={'configurable': {'thread_id': '1', 'thread_ts': '1ef355ac-b80d-6e18-bfff-c903ebc4bdfd'}}, metadata={'source': 'input', 'step': -1, 'writes': {'messages': [HumanMessage(content='Use the search tool to look up the weather in SF')]}}, created_at='2024-06-28T14:29:14.932371+00:00', parent_config=None)\n",
            "--\n",
            "StateSnapshot(values={'messages': [HumanMessage(content='Use the search tool to look up the weather in SF', id='9558b3e7-fa30-4e8b-9587-d58ae3491577')]}, next=('agent',), config={'configurable': {'thread_id': '1', 'thread_ts': '1ef355ac-b810-60dc-8000-a9c67d8cc5e0'}}, metadata={'source': 'loop', 'step': 0, 'writes': None}, created_at='2024-06-28T14:29:14.933257+00:00', parent_config=None)\n",
            "--\n",
            "StateSnapshot(values={'messages': [HumanMessage(content='Use the search tool to look up the weather in SF', id='9558b3e7-fa30-4e8b-9587-d58ae3491577'), AIMessage(content=[{'text': \"Certainly! I'll use the search tool to look up the weather in San Francisco for you. Let me do that right away.\", 'type': 'text'}, {'id': 'toolu_01Bpq6yiKqk9moPuGYKdLr8r', 'input': {'query': 'weather in San Francisco'}, 'name': 'search', 'type': 'tool_use'}], response_metadata={'id': 'msg_011ae64fY2jEcfS8kgrt4Fn9', 'model': 'claude-3-5-sonnet-20240620', 'stop_reason': 'tool_use', 'stop_sequence': None, 'usage': {'input_tokens': 363, 'output_tokens': 81}}, id='run-cfef25ca-d1be-4e79-8798-3bb9a7002287-0', tool_calls=[{'name': 'search', 'args': {'query': 'weather in San Francisco'}, 'id': 'toolu_01Bpq6yiKqk9moPuGYKdLr8r'}], usage_metadata={'input_tokens': 363, 'output_tokens': 81, 'total_tokens': 444})]}, next=('action',), config={'configurable': {'thread_id': '1', 'thread_ts': '1ef355ac-c6b1-6028-8001-82bd095f9a87'}}, metadata={'source': 'loop', 'step': 1, 'writes': {'agent': {'messages': [AIMessage(content=[{'text': \"Certainly! I'll use the search tool to look up the weather in San Francisco for you. Let me do that right away.\", 'type': 'text'}, {'id': 'toolu_01Bpq6yiKqk9moPuGYKdLr8r', 'input': {'query': 'weather in San Francisco'}, 'name': 'search', 'type': 'tool_use'}], response_metadata={'id': 'msg_011ae64fY2jEcfS8kgrt4Fn9', 'model': 'claude-3-5-sonnet-20240620', 'stop_reason': 'tool_use', 'stop_sequence': None, 'usage': {'input_tokens': 363, 'output_tokens': 81}}, id='run-cfef25ca-d1be-4e79-8798-3bb9a7002287-0', tool_calls=[{'name': 'search', 'args': {'query': 'weather in San Francisco'}, 'id': 'toolu_01Bpq6yiKqk9moPuGYKdLr8r'}], usage_metadata={'input_tokens': 363, 'output_tokens': 81, 'total_tokens': 444})]}}}, created_at='2024-06-28T14:29:16.467180+00:00', parent_config=None)\n",
            "--\n",
            "StateSnapshot(values={'messages': [HumanMessage(content='Use the search tool to look up the weather in SF', id='9558b3e7-fa30-4e8b-9587-d58ae3491577'), AIMessage(content=[{'text': \"Certainly! I'll use the search tool to look up the weather in San Francisco for you. Let me do that right away.\", 'type': 'text'}, {'id': 'toolu_01Bpq6yiKqk9moPuGYKdLr8r', 'input': {'query': 'weather in San Francisco'}, 'name': 'search', 'type': 'tool_use'}], response_metadata={'id': 'msg_011ae64fY2jEcfS8kgrt4Fn9', 'model': 'claude-3-5-sonnet-20240620', 'stop_reason': 'tool_use', 'stop_sequence': None, 'usage': {'input_tokens': 363, 'output_tokens': 81}}, id='run-cfef25ca-d1be-4e79-8798-3bb9a7002287-0', tool_calls=[{'name': 'search', 'args': {'query': 'weather in San Francisco'}, 'id': 'toolu_01Bpq6yiKqk9moPuGYKdLr8r'}], usage_metadata={'input_tokens': 363, 'output_tokens': 81, 'total_tokens': 444}), ToolMessage(content='[\"It\\'s sunny in San Francisco, but you better look out if you\\'re a Gemini \\\\ud83d\\\\ude08.\"]', name='search', id='b5aa87cb-335a-4ee0-8809-381e33d0f02e', tool_call_id='toolu_01Bpq6yiKqk9moPuGYKdLr8r')]}, next=('agent',), config={'configurable': {'thread_id': '1', 'thread_ts': '1ef355ac-c6ba-63a8-8002-48d076c3c4b7'}}, metadata={'source': 'loop', 'step': 2, 'writes': {'action': {'messages': [ToolMessage(content='[\"It\\'s sunny in San Francisco, but you better look out if you\\'re a Gemini \\\\ud83d\\\\ude08.\"]', name='search', id='b5aa87cb-335a-4ee0-8809-381e33d0f02e', tool_call_id='toolu_01Bpq6yiKqk9moPuGYKdLr8r')]}}}, created_at='2024-06-28T14:29:16.470958+00:00', parent_config=None)\n",
            "--\n",
            "StateSnapshot(values={'messages': [HumanMessage(content='Use the search tool to look up the weather in SF', id='9558b3e7-fa30-4e8b-9587-d58ae3491577'), AIMessage(content=[{'text': \"Certainly! I'll use the search tool to look up the weather in San Francisco for you. Let me do that right away.\", 'type': 'text'}, {'id': 'toolu_01Bpq6yiKqk9moPuGYKdLr8r', 'input': {'query': 'weather in San Francisco'}, 'name': 'search', 'type': 'tool_use'}], response_metadata={'id': 'msg_011ae64fY2jEcfS8kgrt4Fn9', 'model': 'claude-3-5-sonnet-20240620', 'stop_reason': 'tool_use', 'stop_sequence': None, 'usage': {'input_tokens': 363, 'output_tokens': 81}}, id='run-cfef25ca-d1be-4e79-8798-3bb9a7002287-0', tool_calls=[{'name': 'search', 'args': {'query': 'weather in San Francisco'}, 'id': 'toolu_01Bpq6yiKqk9moPuGYKdLr8r'}], usage_metadata={'input_tokens': 363, 'output_tokens': 81, 'total_tokens': 444}), ToolMessage(content='[\"It\\'s sunny in San Francisco, but you better look out if you\\'re a Gemini \\\\ud83d\\\\ude08.\"]', name='search', id='b5aa87cb-335a-4ee0-8809-381e33d0f02e', tool_call_id='toolu_01Bpq6yiKqk9moPuGYKdLr8r'), AIMessage(content='Based on the search results, I can provide you with information about the weather in San Francisco:\\n\\nThe current weather in San Francisco is sunny. This is great news for residents and visitors who want to enjoy outdoor activities or explore the city.\\n\\nHowever, there\\'s an interesting and somewhat humorous addition to the weather report. It mentions, \"but you better look out if you\\'re a Gemini 😈.\" This appears to be a playful reference to astrology, suggesting that Geminis might have some challenges despite the good weather. Of course, this is not a scientific weather prediction and is likely just a fun addition to the report.\\n\\nTo summarize:\\n1. The weather in San Francisco is currently sunny.\\n2. It\\'s a good day for outdoor activities.\\n3. There\\'s a playful astrological warning for Geminis, but this shouldn\\'t be taken seriously in terms of actual weather conditions.\\n\\nIs there anything else you\\'d like to know about the weather in San Francisco or any other location?', response_metadata={'id': 'msg_01NWeLrkQRLiGsVsxnepzq3p', 'model': 'claude-3-5-sonnet-20240620', 'stop_reason': 'end_turn', 'stop_sequence': None, 'usage': {'input_tokens': 486, 'output_tokens': 217}}, id='run-09f1b7d5-50ec-4f00-a31b-c4dec858b312-0', usage_metadata={'input_tokens': 486, 'output_tokens': 217, 'total_tokens': 703})]}, next=(), config={'configurable': {'thread_id': '1', 'thread_ts': '1ef355ac-f0f5-6942-8003-b79974988738'}}, metadata={'source': 'loop', 'step': 3, 'writes': {'agent': {'messages': [AIMessage(content='Based on the search results, I can provide you with information about the weather in San Francisco:\\n\\nThe current weather in San Francisco is sunny. This is great news for residents and visitors who want to enjoy outdoor activities or explore the city.\\n\\nHowever, there\\'s an interesting and somewhat humorous addition to the weather report. It mentions, \"but you better look out if you\\'re a Gemini 😈.\" This appears to be a playful reference to astrology, suggesting that Geminis might have some challenges despite the good weather. Of course, this is not a scientific weather prediction and is likely just a fun addition to the report.\\n\\nTo summarize:\\n1. The weather in San Francisco is currently sunny.\\n2. It\\'s a good day for outdoor activities.\\n3. There\\'s a playful astrological warning for Geminis, but this shouldn\\'t be taken seriously in terms of actual weather conditions.\\n\\nIs there anything else you\\'d like to know about the weather in San Francisco or any other location?', response_metadata={'id': 'msg_01NWeLrkQRLiGsVsxnepzq3p', 'model': 'claude-3-5-sonnet-20240620', 'stop_reason': 'end_turn', 'stop_sequence': None, 'usage': {'input_tokens': 486, 'output_tokens': 217}}, id='run-09f1b7d5-50ec-4f00-a31b-c4dec858b312-0', usage_metadata={'input_tokens': 486, 'output_tokens': 217, 'total_tokens': 703})]}}}, created_at='2024-06-28T14:29:20.899258+00:00', parent_config=None)\n",
            "--\n"
          ]
        }
      ],
      "source": [
        "all_states = []\nfor state in app.get_state_history(config):\n    print(state)\n    all_states.append(state)\n    print(\"--\")"
      ]
    },
    {
      "cell_type": "markdown",
      "id": "0ec41c37-7c09-4cc7-8475-bf373fe66584",
      "metadata": {},
      "source": [
        "## Replay a state\n",
        "\n",
        "We can go back to any of these states and restart the agent from there! Let's go back to right before the tool call gets executed."
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 7,
      "id": "02250602-8c4a-4fb5-bd6c-d0b9046e8699",
      "metadata": {},
      "outputs": [],
      "source": [
        "to_replay = all_states[2]"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 8,
      "id": "21e7fc18-6fd9-4e11-a84b-e0325c9640c8",
      "metadata": {},
      "outputs": [
        {
          "data": {
            "text/plain": [
              "{'messages': [HumanMessage(content='Use the search tool to look up the weather in SF', id='9558b3e7-fa30-4e8b-9587-d58ae3491577'),\n",
              "  AIMessage(content=[{'text': \"Certainly! I'll use the search tool to look up the weather in San Francisco for you. Let me do that right away.\", 'type': 'text'}, {'id': 'toolu_01Bpq6yiKqk9moPuGYKdLr8r', 'input': {'query': 'weather in San Francisco'}, 'name': 'search', 'type': 'tool_use'}], response_metadata={'id': 'msg_011ae64fY2jEcfS8kgrt4Fn9', 'model': 'claude-3-5-sonnet-20240620', 'stop_reason': 'tool_use', 'stop_sequence': None, 'usage': {'input_tokens': 363, 'output_tokens': 81}}, id='run-cfef25ca-d1be-4e79-8798-3bb9a7002287-0', tool_calls=[{'name': 'search', 'args': {'query': 'weather in San Francisco'}, 'id': 'toolu_01Bpq6yiKqk9moPuGYKdLr8r'}], usage_metadata={'input_tokens': 363, 'output_tokens': 81, 'total_tokens': 444})]}"
            ]
          },
          "execution_count": 8,
          "metadata": {},
          "output_type": "execute_result"
        }
      ],
      "source": [
        "to_replay.values"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 9,
      "id": "d4b01634-0041-4632-8d1f-5464580e54f5",
      "metadata": {},
      "outputs": [
        {
          "data": {
            "text/plain": [
              "('action',)"
            ]
          },
          "execution_count": 9,
          "metadata": {},
          "output_type": "execute_result"
        }
      ],
      "source": [
        "to_replay.next"
      ]
    },
    {
      "cell_type": "markdown",
      "id": "29da43ea-9295-43e2-b164-0eb28d96749c",
      "metadata": {},
      "source": [
        "To replay from this place we just need to pass its config back to the agent. Notice that it just resumes from right where it left all - making a tool call."
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 10,
      "id": "e986f94f-706f-4b6f-b3c4-f95483b9e9b8",
      "metadata": {},
      "outputs": [
        {
          "name": "stdout",
          "output_type": "stream",
          "text": [
            "{'messages': [ToolMessage(content='[\"It\\'s sunny in San Francisco, but you better look out if you\\'re a Gemini \\\\ud83d\\\\ude08.\"]', name='search', tool_call_id='toolu_01Bpq6yiKqk9moPuGYKdLr8r')]}\n",
            "{'messages': [AIMessage(content='Based on the search results, I can provide you with information about the weather in San Francisco:\\n\\nThe current weather in San Francisco is sunny. This is great news for residents and visitors who want to enjoy outdoor activities or explore the city.\\n\\nHowever, there\\'s an interesting and somewhat humorous addition to the weather report. It mentions, \"but you better look out if you\\'re a Gemini 😈.\" This appears to be a playful reference to astrology, suggesting that Geminis might have some challenges despite the good weather. Of course, this is not a scientific weather prediction and is likely just a fun addition to the weather report.\\n\\nTo summarize:\\n1. The weather in San Francisco is currently sunny.\\n2. It\\'s a good day for outdoor activities.\\n3. There\\'s a playful astrological reference for Geminis, but this shouldn\\'t be taken as actual weather information.\\n\\nIs there anything else you\\'d like to know about the weather in San Francisco or any other location?', response_metadata={'id': 'msg_01S4uzzxbGwvsk1vfoLJAD7Z', 'model': 'claude-3-5-sonnet-20240620', 'stop_reason': 'end_turn', 'stop_sequence': None, 'usage': {'input_tokens': 486, 'output_tokens': 214}}, id='run-570bd10f-4007-4b33-8c29-44b9a19b6978-0', usage_metadata={'input_tokens': 486, 'output_tokens': 214, 'total_tokens': 700})]}\n"
          ]
        }
      ],
      "source": [
        "for event in app.stream(None, to_replay.config):\n    for v in event.values():\n        print(v)"
      ]
    },
    {
      "cell_type": "markdown",
      "id": "59910951-fae1-4475-8511-f622439b590d",
      "metadata": {},
      "source": [
        "## Branch off a past state\n",
        "\n",
        "Using LangGraph's checkpointing, you can do more than just replay past states. You can branch off previous locations to let the agent explore alternate trajectories or to let a user \"version control\" changes in a workflow.\n",
        "\n",
        "Let's show how to do this to edit the state at a particular point in time. Let's update the state to change the input to the tool"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 11,
      "id": "fbd5ad3b-5363-4ab7-ac63-b04668bc998f",
      "metadata": {},
      "outputs": [],
      "source": [
        "# Let's now get the last message in the state\n",
        "# This is the one with the tool calls that we want to update\n",
        "last_message = to_replay.values[\"messages\"][-1]\n",
        "\n",
        "# Let's now update the args for that tool call\n",
        "last_message.tool_calls[0][\"args\"] = {\"query\": \"current weather in SF\"}\n",
        "\n",
        "branch_config = app.update_state(\n",
        "    to_replay.config,\n",
        "    {\"messages\": [last_message]},\n",
        ")"
      ]
    },
    {
      "cell_type": "markdown",
      "id": "bced65eb-2158-43e6-a9e3-3b047c8d418e",
      "metadata": {},
      "source": [
        "We can then invoke with this new `branch_config` to resume running from here with changed state. We can see from the log that the tool was called with different input."
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 12,
      "id": "9a92d3da-62e2-45a2-8545-e4f6a64e0ffe",
      "metadata": {},
      "outputs": [
        {
          "name": "stdout",
          "output_type": "stream",
          "text": [
            "{'messages': [ToolMessage(content='[\"It\\'s sunny in San Francisco, but you better look out if you\\'re a Gemini \\\\ud83d\\\\ude08.\"]', name='search', tool_call_id='toolu_01Bpq6yiKqk9moPuGYKdLr8r')]}\n",
            "{'messages': [AIMessage(content=\"Based on the search results, I can provide you with information about the current weather in San Francisco (SF):\\n\\nThe weather in San Francisco is currently sunny. This means it's a clear day with plenty of sunshine, which is great for outdoor activities or simply enjoying the city's beautiful views.\\n\\nIt's worth noting that San Francisco's weather can be quite variable, even within the city itself, due to its unique geography and microclimates. While it's sunny now, it's always a good idea to be prepared for potential changes, as the city is known for its foggy conditions, especially in certain areas and during specific times of the day.\\n\\nThe search result also includes a playful reference to astrology, mentioning Geminis. However, this is likely just a humorous addition and not related to the actual weather conditions.\\n\\nIs there any specific information about the weather in San Francisco that you'd like to know more about, such as temperature, wind conditions, or forecast for the coming days?\", response_metadata={'id': 'msg_01AoFmmzZxbMLuu3npVXJKG7', 'model': 'claude-3-5-sonnet-20240620', 'stop_reason': 'end_turn', 'stop_sequence': None, 'usage': {'input_tokens': 486, 'output_tokens': 211}}, id='run-ccb9e786-c23e-4017-bf29-8405f17cec9f-0', usage_metadata={'input_tokens': 486, 'output_tokens': 211, 'total_tokens': 697})]}\n"
          ]
        }
      ],
      "source": [
        "for event in app.stream(None, branch_config):\n    for v in event.values():\n        print(v)"
      ]
    },
    {
      "cell_type": "markdown",
      "id": "511e319e-d10d-4b04-a4e0-fc4f3d87cb23",
      "metadata": {},
      "source": [
        "Alternatively, we could update the state to not even call a tool!"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 13,
      "id": "01abb480-df55-4eba-a2be-cf9372b60b54",
      "metadata": {},
      "outputs": [],
      "source": [
        "from langchain_core.messages import AIMessage\n",
        "\n",
        "# Let's now get the last message in the state\n",
        "# This is the one with the tool calls that we want to update\n",
        "last_message = to_replay.values[\"messages\"][-1]\n",
        "\n",
        "# Let's now get the ID for the last message, and create a new message with that ID.\n",
        "new_message = AIMessage(content=\"its warm!\", id=last_message.id)\n",
        "\n",
        "branch_config = app.update_state(\n",
        "    to_replay.config,\n",
        "    {\"messages\": [new_message]},\n",
        ")"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 14,
      "id": "1a7cfcd4-289e-419e-8b49-dfaef4f88641",
      "metadata": {},
      "outputs": [],
      "source": [
        "branch_state = app.get_state(branch_config)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 15,
      "id": "5198f9c1-d2d4-458a-993d-3caa55810b1e",
      "metadata": {},
      "outputs": [
        {
          "data": {
            "text/plain": [
              "{'messages': [HumanMessage(content='Use the search tool to look up the weather in SF', id='9558b3e7-fa30-4e8b-9587-d58ae3491577'),\n",
              "  AIMessage(content='its warm!', id='run-cfef25ca-d1be-4e79-8798-3bb9a7002287-0')]}"
            ]
          },
          "execution_count": 15,
          "metadata": {},
          "output_type": "execute_result"
        }
      ],
      "source": [
        "branch_state.values"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 16,
      "id": "5d89d55d-db84-4c2d-828b-64a29a69947b",
      "metadata": {},
      "outputs": [
        {
          "data": {
            "text/plain": [
              "()"
            ]
          },
          "execution_count": 16,
          "metadata": {},
          "output_type": "execute_result"
        }
      ],
      "source": [
        "branch_state.next"
      ]
    },
    {
      "cell_type": "markdown",
      "id": "cc168c90-a374-4280-a9a6-8bc232dbb006",
      "metadata": {},
      "source": [
        "You can see the snapshot was updated and now correctly reflects that there is no next step."
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "74a7a5ed-0c14-4883-a16b-d70aaf40f7ea",
      "metadata": {},
      "outputs": [],
      "source": [
        ""
      ]
    }
  ],
  "metadata": {
    "kernelspec": {
      "display_name": "Python 3 (ipykernel)",
      "language": "python",
      "name": "python3"
    },
    "language_info": {
      "codemirror_mode": {
        "name": "ipython",
        "version": 3
      },
      "file_extension": ".py",
      "mimetype": "text/x-python",
      "name": "python",
      "nbconvert_exporter": "python",
      "pygments_lexer": "ipython3",
      "version": "3.12.3"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 5
}
