AI Hints
Home›LangGraph Tutorial›Running and Drawing Graphs
LangGraph · Lesson 8 of 120

Compile and Invoke

Running a graph.

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

The builder is a blueprint. Compile turns it into a graph you can run. A compiled graph is a LangChain runnable, so it has invoke, batch, and stream, just like a model or a chain.

Compile and Invoke
From the video: Compile and Invoke
  • Builder is a blueprint
  • Compile makes it runnable
  • invoke, batch, stream

Invoke and batch

We compile a small graph that writes a shelf label for a book. Invoke runs it once, and returns the final state. Batch runs it for a list of inputs. Run it. One label from invoke, then three from batch, in order.

invoke.pypython
from typing import TypedDict
from langgraph.graph import StateGraph, START, END

class State(TypedDict):
    title: str
    label: str

def make_label(state):
    return {"label": f"[ {state['title'].upper()} ]"}

builder = StateGraph(State)
builder.add_node(make_label)
builder.add_edge(START, "make_label"); builder.add_edge("make_label", END)
graph = builder.compile()

print(graph.invoke({"title": "Dune"})["label"])
for result in graph.batch([{"title": t} for t in ["Emma", "Circe", "The Hobbit"]]):
    print(result["label"])
Output
[ DUNE ]
[ EMMA ]
[ CIRCE ]
[ THE HOBBIT ]

What invoke returns

Invoke returns the whole final state, as a dictionary. Pick out the keys you need. Input keys you don't set are simply missing, until a node writes them. And because the graph is a runnable, async versions like a invoke work too.

keyvalue
Returnsthe final state
Pickthe keys you need
batchmany inputs
Asyncainvoke, abatch

Summary

Compile turns the builder into a runnable graph. Invoke runs it once, batch runs it many times. Next, drawing a graph to see its shape.

Key takeaways
  • builder.compile()
  • graph.invoke
  • graph.batch
Next up: Drawing the Graph — see its shape