{"id":17712008,"url":"https://langchain-ai.github.io/langgraph/","last_synced_at":"2025-03-13T20:32:03.484Z","repository":{"id":187457675,"uuid":"676672661","full_name":"langchain-ai/langgraph","owner":"langchain-ai","description":"Build resilient language agents as graphs.","archived":false,"fork":false,"pushed_at":"2025-03-01T03:37:42.000Z","size":480083,"stargazers_count":9555,"open_issues_count":108,"forks_count":1570,"subscribers_count":71,"default_branch":"main","last_synced_at":"2025-03-01T09:40:47.780Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"https://langchain-ai.github.io/langgraph/","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/langchain-ai.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":"CONTRIBUTING.md","funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":"security.md","support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2023-08-09T18:33:12.000Z","updated_at":"2025-03-01T08:53:58.000Z","dependencies_parsed_at":null,"dependency_job_id":"d3066150-f4a9-428d-b0de-bcb70237a338","html_url":"https://github.com/langchain-ai/langgraph","commit_stats":{"total_commits":2947,"total_committers":112,"mean_commits":26.3125,"dds":0.5941635561588056,"last_synced_commit":"10d46acc60d19db426b6508b8b81de96aa1bab6d"},"previous_names":["langchain-ai/permchain","langchain-ai/langgraph"],"tags_count":248,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/langchain-ai%2Flanggraph","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/langchain-ai%2Flanggraph/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/langchain-ai%2Flanggraph/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/langchain-ai%2Flanggraph/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/langchain-ai","download_url":"https://codeload.github.com/langchain-ai/langgraph/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":243478364,"owners_count":20297242,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-10-25T09:00:38.868Z","updated_at":"2025-03-13T20:32:03.475Z","avatar_url":"https://github.com/langchain-ai.png","language":"Python","funding_links":[],"categories":["**Section 4** : LangChain Features, Usage, and Comparisons","🤝 Agent Frameworks \u0026 MCP","Agentic AI","Coding Tools for Economists","Agent Ecosystem","🌐 Resources \u0026 Tools","Agent 框架","🛠️ Tools \u0026 Projects","🤖 AI Agents \u0026 Automation","Miscellaneous","Excluded / Boundary Cases","🛠️ Builders"],"sub_categories":["**Macro and Micro-orchestration**","Agent Frameworks","AI Agent Frameworks","Frameworks","Documentation","Development Frameworks","Agent Frameworks \u0026 Libraries","AI Agents \u0026 Automation","`agent-builder` — Excluded by Design","🧠 Agentic \u0026 GenAI App Frameworks"],"readme":"# 🦜🕸️LangGraph\n\n![Version](https://img.shields.io/pypi/v/langgraph)\n[![Downloads](https://static.pepy.tech/badge/langgraph/month)](https://pepy.tech/project/langgraph)\n[![Open Issues](https://img.shields.io/github/issues-raw/langchain-ai/langgraph)](https://github.com/langchain-ai/langgraph/issues)\n[![Docs](https://img.shields.io/badge/docs-latest-blue)](https://langchain-ai.github.io/langgraph/)\n\n⚡ Building language agents as graphs ⚡\n\n\u003e [!NOTE]\n\u003e Looking for the JS version? See the [JS repo](https://github.com/langchain-ai/langgraphjs) and the [JS docs](https://langchain-ai.github.io/langgraphjs/).\n\n## Overview\n\n[LangGraph](https://langchain-ai.github.io/langgraph/) is a library for building\nstateful, multi-actor applications with LLMs, used to create agent and multi-agent\nworkflows. Check out an introductory tutorial [here](https://langchain-ai.github.io/langgraph/tutorials/introduction/).\n\n\nLangGraph is inspired by [Pregel](https://research.google/pubs/pub37252/) and [Apache Beam](https://beam.apache.org/). The public interface draws inspiration from [NetworkX](https://networkx.org/documentation/latest/). LangGraph is built by LangChain Inc, the creators of LangChain, but can be used without LangChain.\n\n### Why use LangGraph?\n\nLangGraph powers [production-grade agents](https://www.langchain.com/built-with-langgraph), trusted by Linkedin, Uber, Klarna, GitLab, and many more. LangGraph provides fine-grained control over both the flow and state of your agent applications. It implements a central [persistence layer](https://langchain-ai.github.io/langgraph/concepts/persistence/), enabling features that are common to most agent architectures:\n\n- **Memory**: LangGraph persists arbitrary aspects of your application's state,\nsupporting memory of conversations and other updates within and across user\ninteractions;\n- **Human-in-the-loop**: Because state is checkpointed, execution can be interrupted\nand resumed, allowing for decisions, validation, and corrections at key stages via\nhuman input.\n\nStandardizing these components allows individuals and teams to focus on the behavior\nof their agent, instead of its supporting infrastructure.\n\nThrough [LangGraph Platform](#langgraph-platform), LangGraph also provides tooling for\nthe development, deployment, debugging, and monitoring of your applications.\n\nLangGraph integrates seamlessly with\n[LangChain](https://python.langchain.com/docs/introduction/) and\n[LangSmith](https://docs.smith.langchain.com/) (but does not require them).\n\nTo learn more about LangGraph, check out our first LangChain Academy\ncourse, *Introduction to LangGraph*, available for free\n[here](https://academy.langchain.com/courses/intro-to-langgraph).\n\n### LangGraph Platform\n\n[LangGraph Platform](https://langchain-ai.github.io/langgraph/concepts/langgraph_platform) is infrastructure for deploying LangGraph agents. It is a commercial solution for deploying agentic applications to production, built on the open-source LangGraph framework. The LangGraph Platform consists of several components that work together to support the development, deployment, debugging, and monitoring of LangGraph applications: [LangGraph Server](https://langchain-ai.github.io/langgraph/concepts/langgraph_server) (APIs), [LangGraph SDKs](https://langchain-ai.github.io/langgraph/concepts/sdk) (clients for the APIs), [LangGraph CLI](https://langchain-ai.github.io/langgraph/concepts/langgraph_cli) (command line tool for building the server), and [LangGraph Studio](https://langchain-ai.github.io/langgraph/concepts/langgraph_studio) (UI/debugger).\n\nSee deployment options [here](https://langchain-ai.github.io/langgraph/concepts/deployment_options/)\n(includes a free tier).\n\nHere are some common issues that arise in complex deployments, which LangGraph Platform addresses:\n\n- **Streaming support**: LangGraph Server provides [multiple streaming modes](https://langchain-ai.github.io/langgraph/concepts/streaming) optimized for various application needs\n- **Background runs**: Runs agents asynchronously in the background\n- **Support for long running agents**: Infrastructure that can handle long running processes\n- **[Double texting](https://langchain-ai.github.io/langgraph/concepts/double_texting)**: Handle the case where you get two messages from the user before the agent can respond\n- **Handle burstiness**: Task queue for ensuring requests are handled consistently without loss, even under heavy loads\n\n## Installation\n\n```shell\npip install -U langgraph\n```\n\n## Example\n\nLet's build a tool-calling [ReAct-style](https://langchain-ai.github.io/langgraph/concepts/agentic_concepts/#react-implementation) agent that uses a search tool!\n\n```shell\npip install langchain-anthropic\n```\n\n```shell\nexport ANTHROPIC_API_KEY=sk-...\n```\n\nOptionally, we can set up [LangSmith](https://docs.smith.langchain.com/) for best-in-class observability.\n\n```shell\nexport LANGSMITH_TRACING=true\nexport LANGSMITH_API_KEY=lsv2_sk_...\n```\n\nThe simplest way to create a tool-calling agent in LangGraph is to use `create_react_agent`:\n\n\u003cdetails open\u003e\n  \u003csummary\u003eHigh-level implementation\u003c/summary\u003e\n\n```python\nfrom langgraph.prebuilt import create_react_agent\nfrom langgraph.checkpoint.memory import MemorySaver\nfrom langchain_anthropic import ChatAnthropic\nfrom langchain_core.tools import tool\n\n# Define the tools for the agent to use\n@tool\ndef search(query: str):\n    \"\"\"Call to surf the web.\"\"\"\n    # This is a placeholder, but don't tell the LLM that...\n    if \"sf\" in query.lower() or \"san francisco\" in query.lower():\n        return \"It's 60 degrees and foggy.\"\n    return \"It's 90 degrees and sunny.\"\n\n\ntools = [search]\nmodel = ChatAnthropic(model=\"claude-3-5-sonnet-latest\", temperature=0)\n\n# Initialize memory to persist state between graph runs\ncheckpointer = MemorySaver()\n\napp = create_react_agent(model, tools, checkpointer=checkpointer)\n\n# Use the agent\nfinal_state = app.invoke(\n    {\"messages\": [{\"role\": \"user\", \"content\": \"what is the weather in sf\"}]},\n    config={\"configurable\": {\"thread_id\": 42}}\n)\nfinal_state[\"messages\"][-1].content\n```\n```\n\"Based on the search results, I can tell you that the current weather in San Francisco is:\\n\\nTemperature: 60 degrees Fahrenheit\\nConditions: Foggy\\n\\nSan Francisco is known for its microclimates and frequent fog, especially during the summer months. The temperature of 60°F (about 15.5°C) is quite typical for the city, which tends to have mild temperatures year-round. The fog, often referred to as \"Karl the Fog\" by locals, is a characteristic feature of San Francisco\\'s weather, particularly in the mornings and evenings.\\n\\nIs there anything else you\\'d like to know about the weather in San Francisco or any other location?\"\n```\n\nNow when we pass the same \u003ccode\u003e\"thread_id\"\u003c/code\u003e, the conversation context is retained via the saved state (i.e. stored list of messages)\n\n```python\nfinal_state = app.invoke(\n    {\"messages\": [{\"role\": \"user\", \"content\": \"what about ny\"}]},\n    config={\"configurable\": {\"thread_id\": 42}}\n)\nfinal_state[\"messages\"][-1].content\n```\n\n```\n\"Based on the search results, I can tell you that the current weather in New York City is:\\n\\nTemperature: 90 degrees Fahrenheit (approximately 32.2 degrees Celsius)\\nConditions: Sunny\\n\\nThis weather is quite different from what we just saw in San Francisco. New York is experiencing much warmer temperatures right now. Here are a few points to note:\\n\\n1. The temperature of 90°F is quite hot, typical of summer weather in New York City.\\n2. The sunny conditions suggest clear skies, which is great for outdoor activities but also means it might feel even hotter due to direct sunlight.\\n3. This kind of weather in New York often comes with high humidity, which can make it feel even warmer than the actual temperature suggests.\\n\\nIt's interesting to see the stark contrast between San Francisco's mild, foggy weather and New York's hot, sunny conditions. This difference illustrates how varied weather can be across different parts of the United States, even on the same day.\\n\\nIs there anything else you'd like to know about the weather in New York or any other location?\"\n```\n\u003c/details\u003e\n\n\u003e [!TIP]\n\u003e LangGraph is a **low-level** framework that allows you to implement any custom agent\narchitectures. Click on the low-level implementation below to see how to implement a\ntool-calling agent from scratch.\n\n\u003cdetails\u003e\n\u003csummary\u003eLow-level implementation\u003c/summary\u003e\n\n```python\nfrom typing import Literal\n\nfrom langchain_anthropic import ChatAnthropic\nfrom langchain_core.tools import tool\nfrom langgraph.checkpoint.memory import MemorySaver\nfrom langgraph.graph import END, START, StateGraph, MessagesState\nfrom langgraph.prebuilt import ToolNode\n\n\n# Define the tools for the agent to use\n@tool\ndef search(query: str):\n    \"\"\"Call to surf the web.\"\"\"\n    # This is a placeholder, but don't tell the LLM that...\n    if \"sf\" in query.lower() or \"san francisco\" in query.lower():\n        return \"It's 60 degrees and foggy.\"\n    return \"It's 90 degrees and sunny.\"\n\n\ntools = [search]\n\ntool_node = ToolNode(tools)\n\nmodel = ChatAnthropic(model=\"claude-3-5-sonnet-latest\", temperature=0).bind_tools(tools)\n\n# Define the function that determines whether to continue or not\ndef should_continue(state: MessagesState) -\u003e Literal[\"tools\", END]:\n    messages = state['messages']\n    last_message = messages[-1]\n    # If the LLM makes a tool call, then we route to the \"tools\" node\n    if last_message.tool_calls:\n        return \"tools\"\n    # Otherwise, we stop (reply to the user)\n    return END\n\n\n# Define the function that calls the model\ndef call_model(state: MessagesState):\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\nworkflow = StateGraph(MessagesState)\n\n# Define the two nodes we will cycle between\nworkflow.add_node(\"agent\", call_model)\nworkflow.add_node(\"tools\", tool_node)\n\n# Set the entrypoint as `agent`\n# This means that this node is the first one called\nworkflow.add_edge(START, \"agent\")\n\n# We now add a conditional edge\nworkflow.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)\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.\nworkflow.add_edge(\"tools\", 'agent')\n\n# Initialize memory to persist state between graph runs\ncheckpointer = 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# Note that we're (optionally) passing the memory when compiling the graph\napp = workflow.compile(checkpointer=checkpointer)\n\n# Use the agent\nfinal_state = app.invoke(\n    {\"messages\": [{\"role\": \"user\", \"content\": \"what is the weather in sf\"}]},\n    config={\"configurable\": {\"thread_id\": 42}}\n)\nfinal_state[\"messages\"][-1].content\n```\n\n\u003cb\u003eStep-by-step Breakdown\u003c/b\u003e:\n\n\u003cdetails\u003e\n\u003csummary\u003eInitialize the model and tools.\u003c/summary\u003e\n\u003cul\u003e\n  \u003cli\u003e\n    We use \u003ccode\u003eChatAnthropic\u003c/code\u003e as our LLM. \u003cstrong\u003eNOTE:\u003c/strong\u003e we need to make sure the model knows that it has these tools available to call. We can do this by converting the LangChain tools into the format for OpenAI tool calling using the \u003ccode\u003e.bind_tools()\u003c/code\u003e method.\n  \u003c/li\u003e\n  \u003cli\u003e\n    We define the tools we want to use - a search tool in our case. It is really easy to create your own tools - see documentation here on how to do that \u003ca href=\"https://python.langchain.com/docs/how_to/custom_tools/\"\u003ehere\u003c/a\u003e.\n  \u003c/li\u003e\n\u003c/ul\u003e\n\u003c/details\u003e\n\n\u003cdetails\u003e\n\u003csummary\u003eInitialize graph with state.\u003c/summary\u003e\n\n\u003cul\u003e\n    \u003cli\u003eWe initialize graph (\u003ccode\u003eStateGraph\u003c/code\u003e) by passing state schema (in our case \u003ccode\u003eMessagesState\u003c/code\u003e)\u003c/li\u003e\n    \u003cli\u003e\u003ccode\u003eMessagesState\u003c/code\u003e is a prebuilt state schema that has one attribute -- a list of LangChain \u003ccode\u003eMessage\u003c/code\u003e objects, as well as logic for merging the updates from each node into the state.\u003c/li\u003e\n\u003c/ul\u003e\n\u003c/details\u003e\n\n\u003cdetails\u003e\n\u003csummary\u003eDefine graph nodes.\u003c/summary\u003e\n\nThere are two main nodes we need:\n\n\u003cul\u003e\n    \u003cli\u003eThe \u003ccode\u003eagent\u003c/code\u003e node: responsible for deciding what (if any) actions to take.\u003c/li\u003e\n    \u003cli\u003eThe \u003ccode\u003etools\u003c/code\u003e node that invokes tools: if the agent decides to take an action, this node will then execute that action.\u003c/li\u003e\n\u003c/ul\u003e\n\u003c/details\u003e\n\n\u003cdetails\u003e\n\u003csummary\u003eDefine entry point and graph edges.\u003c/summary\u003e\n\nFirst, we need to set the entry point for graph execution - \u003ccode\u003eagent\u003c/code\u003e node.\n\nThen we define one normal and one conditional edge. Conditional edge means that the destination depends on the contents of the graph's state (\u003ccode\u003eMessagesState\u003c/code\u003e). In our case, the destination is not known until the agent (LLM) decides.\n\n\u003cul\u003e\n  \u003cli\u003eConditional edge: after the agent is called, we should either:\n    \u003cul\u003e\n      \u003cli\u003ea. Run tools if the agent said to take an action, OR\u003c/li\u003e\n      \u003cli\u003eb. Finish (respond to the user) if the agent did not ask to run tools\u003c/li\u003e\n    \u003c/ul\u003e\n  \u003c/li\u003e\n  \u003cli\u003eNormal edge: after the tools are invoked, the graph should always return to the agent to decide what to do next\u003c/li\u003e\n\u003c/ul\u003e\n\u003c/details\u003e\n\n\u003cdetails\u003e\n\u003csummary\u003eCompile the graph.\u003c/summary\u003e\n\n\u003cul\u003e\n  \u003cli\u003e\n    When we compile the graph, we turn it into a LangChain \n    \u003ca href=\"https://python.langchain.com/docs/concepts/runnables/\"\u003eRunnable\u003c/a\u003e, \n    which automatically enables calling \u003ccode\u003e.invoke()\u003c/code\u003e, \u003ccode\u003e.stream()\u003c/code\u003e and \u003ccode\u003e.batch()\u003c/code\u003e \n    with your inputs\n  \u003c/li\u003e\n  \u003cli\u003e\n    We can also optionally pass checkpointer object for persisting state between graph runs, and enabling memory, \n    human-in-the-loop workflows, time travel and more. In our case we use \u003ccode\u003eMemorySaver\u003c/code\u003e - \n    a simple in-memory checkpointer\n  \u003c/li\u003e\n\u003c/ul\u003e\n\u003c/details\u003e\n\n\u003cdetails\u003e\n\u003csummary\u003eExecute the graph.\u003c/summary\u003e\n\n\u003col\u003e\n  \u003cli\u003eLangGraph adds the input message to the internal state, then passes the state to the entrypoint node, \u003ccode\u003e\"agent\"\u003c/code\u003e.\u003c/li\u003e\n  \u003cli\u003eThe \u003ccode\u003e\"agent\"\u003c/code\u003e node executes, invoking the chat model.\u003c/li\u003e\n  \u003cli\u003eThe chat model returns an \u003ccode\u003eAIMessage\u003c/code\u003e. LangGraph adds this to the state.\u003c/li\u003e\n  \u003cli\u003eGraph cycles the following steps until there are no more \u003ccode\u003etool_calls\u003c/code\u003e on \u003ccode\u003eAIMessage\u003c/code\u003e:\n    \u003cul\u003e\n      \u003cli\u003eIf \u003ccode\u003eAIMessage\u003c/code\u003e has \u003ccode\u003etool_calls\u003c/code\u003e, \u003ccode\u003e\"tools\"\u003c/code\u003e node executes\u003c/li\u003e\n      \u003cli\u003eThe \u003ccode\u003e\"agent\"\u003c/code\u003e node executes again and returns \u003ccode\u003eAIMessage\u003c/code\u003e\u003c/li\u003e\n    \u003c/ul\u003e\n  \u003c/li\u003e\n  \u003cli\u003eExecution progresses to the special \u003ccode\u003eEND\u003c/code\u003e value and outputs the final state. And as a result, we get a list of all our chat messages as output.\u003c/li\u003e\n\u003c/ol\u003e\n\u003c/details\u003e\n\n\u003c/details\u003e\n\n## Documentation\n\n* [Tutorials](https://langchain-ai.github.io/langgraph/tutorials/): Learn to build with LangGraph through guided examples.\n* [How-to Guides](https://langchain-ai.github.io/langgraph/how-tos/): Accomplish specific things within LangGraph, from streaming, to adding memory \u0026 persistence, to common design patterns (branching, subgraphs, etc.), these are the place to go if you want to copy and run a specific code snippet.\n* [Conceptual Guides](https://langchain-ai.github.io/langgraph/concepts/high_level/): In-depth explanations of the key concepts and principles behind LangGraph, such as nodes, edges, state and more.\n* [API Reference](https://langchain-ai.github.io/langgraph/reference/graphs/): Review important classes and methods, simple examples of how to use the graph and checkpointing APIs, higher-level prebuilt components and more.\n* [LangGraph Platform](https://langchain-ai.github.io/langgraph/concepts/#langgraph-platform): LangGraph Platform is a commercial solution for deploying agentic applications in production, built on the open-source LangGraph framework.\n\n## Resources\n\n* [Built with LangGraph](https://www.langchain.com/built-with-langgraph): Hear how industry leaders use LangGraph to ship powerful, production-ready AI applications.\n\n## Contributing\n\nFor more information on how to contribute, see [here](https://github.com/langchain-ai/langgraph/blob/main/CONTRIBUTING.md).\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/langchain-ai.github.io%2Flanggraph%2F","html_url":"https://awesome.ecosyste.ms/projects/langchain-ai.github.io%2Flanggraph%2F","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/langchain-ai.github.io%2Flanggraph%2F/lists"}