AI Hints
Home›LangGraph Tutorial›Context and Async
LangGraph · Lesson 83 of 120

Async Graphs

Ainvoke.

⏱ 2 min read▶ Video at 1:29:19🎓 Free
Watch this lesson in the LangGraph courseWatch on YouTube ↗Full LangGraph course

Web servers handle many requests at once, using async code. LangGraph supports async nodes, and async methods, like a invoke and a stream. While one node waits for a network reply, other work can carry on.

Async Graphs
From the video: Async Graphs
  • Servers use async
  • Async nodes and methods
  • Others work while one waits

Async nodes in parallel

Both nodes are async functions, which await a one second delay, standing in for network calls. They run in parallel from start. We run the graph with await a invoke. Run it. Two one second calls finish in about one second in total, not two.

async_graph.pypython
import asyncio, operator, time
from typing import Annotated, TypedDict
from langgraph.graph import StateGraph, START

class State(TypedDict):
    found: Annotated[list[str], operator.add]

async def bath(s):
    await asyncio.sleep(1)          # e.g. a network call
    return {"found": ["Bath: 2"]}

async def bristol(s):
    await asyncio.sleep(1)
    return {"found": ["Bristol: 1"]}

b = StateGraph(State); b.add_node(bath); b.add_node(bristol)
b.add_edge(START, "bath"); b.add_edge(START, "bristol")
start = time.perf_counter(); out = asyncio.run(b.compile().ainvoke({"found": []}))
print(out["found"], f"in {time.perf_counter() - start:.1f}s")
Output
['Bath: 2', 'Bristol: 1'] in 1.0s

Async tips

Use async nodes when they call async libraries, like model a invoke. Use a invoke and a stream in async web servers. Don't mix in slow blocking calls, which hold everything up. And remember, parallel branches help even without async.

keyvalue
Async nodesawait network calls
Methodsainvoke, astream
Avoidblocking calls
ServersFastAPI and others

Summary

Async nodes and a invoke let a graph wait for many things at once. Use them in async servers. Next, a quiz on reliability.

Key takeaways
  • async def node
  • await graph.ainvoke
  • Don't block
Next up: Reliability Quiz — test yourself