AI Hints
Home›LangGraph Tutorial›Models and Messages
LangGraph · Lesson 25 of 120

Models in Nodes

Calling an LLM.

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

A node is just a function, so it can do anything, including calling a language model. That's where LangGraph gets interesting. Model calls become steps in a process you control.

Models in Nodes
From the video: Models in Nodes
  • Nodes can call models
  • Model calls become steps
  • A process you control

A blurb writer

We create a Gemini model with LangChain's init chat model. The write blurb node asks it for a one sentence blurb, using the title from the state. Then a second node, a plain function, adds the price. Run it. The model writes the blurb, and the next node finishes the shelf card.

model_node.pypython
from typing import TypedDict
from langchain.chat_models import init_chat_model
from langgraph.graph import StateGraph, START, END

model = init_chat_model("google_genai:gemini-flash-lite-latest", temperature=0)

class State(TypedDict):
    title: str
    blurb: str
    card: str

def write_blurb(s):
    reply = model.invoke(f"Write a one-sentence shop blurb for the novel {s['title']}.")
    return {"blurb": reply.text.strip()}
def make_card(s):
    return {"card": f"{s['title'].upper()} | £9.99\n{s['blurb']}"}

b = StateGraph(State); b.add_sequence([write_blurb, make_card]); b.add_edge(START, "write_blurb")
print(b.compile().invoke({"title": "Dune"})["card"])
Output
DUNE | £9.99
On the unforgiving desert planet of Arrakis, a young noble thrust into an interstellar
power struggle must embrace a dangerous destiny to control the universe's most precious
resource.

Mixing models and code

Graphs work best when models do what they're good at, like writing and judging, and plain code does the rest, like prices, maths, and lookups. Each step is visible, testable, and replaceable.

keyvalue
Modelswriting, judging
Codeprices, maths, lookups
Each stepvisible
Swapmodels or code freely

Summary

Nodes can call models, so model calls become steps in your graph. Mix model steps with plain code steps. Next, keeping a chat history in the state.

Key takeaways
  • model.invoke in a node
  • Return the text
  • Code does the rest
Next up: Messages in State — chat history