{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# How to create a ReAct agent from scratch (Functional API)\n", "\n", "!!! info \"Prerequisites\"\n", " This guide assumes familiarity with the following:\n", " \n", " - [Chat Models](https://python.langchain.com/docs/concepts/chat_models)\n", " - [Messages](https://python.langchain.com/docs/concepts/messages)\n", " - [Tool Calling](https://python.langchain.com/docs/concepts/tool_calling/)\n", " - [Entrypoints](../../concepts/functional_api/#entrypoint) and [Tasks](../../concepts/functional_api/#task)\n", "\n", "This guide demonstrates how to implement a ReAct agent using the LangGraph [Functional API](../../concepts/functional_api).\n", "\n", "The ReAct agent is a [tool-calling agent](../../concepts/agentic_concepts/#tool-calling-agent) that operates as follows:\n", "\n", "1. Queries are issued to a chat model;\n", "2. If the model generates no [tool calls](../../concepts/agentic_concepts/#tool-calling), we return the model response.\n", "3. If the model generates tool calls, we execute the tool calls with available tools, append them as [tool messages](https://python.langchain.com/docs/concepts/messages/) to our message list, and repeat the process.\n", "\n", "This is a simple and versatile set-up that can be extended with memory, human-in-the-loop capabilities, and other features. See the dedicated [how-to guides](../../how-tos/#prebuilt-react-agent) for examples.\n", "\n", "## Setup\n", "\n", "First, let's install the required packages and set our API keys:" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "%%capture --no-stderr\n", "%pip install -U langgraph langchain-openai" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "import getpass\n", "import os\n", "\n", "\n", "def _set_env(var: str):\n", " if not os.environ.get(var):\n", " os.environ[var] = getpass.getpass(f\"{var}: \")\n", "\n", "\n", "_set_env(\"OPENAI_API_KEY\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "
Set up LangSmith for better debugging
\n", "\n", " 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 aps built with LangGraph — read more about how to get started in the docs. \n", "
\n", "