Generative AI and Agentic AILangChain 1.4 · LangGraph 1.2 · Python 3.12 or 3.13
Dashboard
0%

Messages and prompts

Messages are the objects a chat model reads and writes: each one has a role (system, human, AI or tool), content and metadata, and a list of them is the conversation you send with invoke.

Last updated: 08 Oct, 2026 · LangChain 1.4 · LangGraph 1.2

The chat loop in Your first LLM app sent a bare string every time. A string is the simplest input, and a list of messages is how a program gives the model instructions, history and tool results.

A list of messages goes into model.invoke: a SystemMessage that says how the model should behave, a HumanMessage, an earlier AIMessage and a new HumanMessage. The model returns one AIMessage. A ChatPromptTemplate builds the list from a template and values.

Sending a text prompt

Messages, and text prompts against message prompts · from the Complete Agentic AI Course video · 1:07:00 to 1:11:35

The video defines a message as the basic unit of context for a model in LangChain: it carries a role, which says what kind of message it is, content, and metadata. The input to the model is a human message and its output is an AI message. A text prompt is a plain string, which the video uses when the request is single and standalone, needs no conversation history and should take minimal code. A string passed to invoke is turned into a human message internally.

ExampleAPI keyRun on Groq
from langchain.chat_models import init_chat_model

model = init_chat_model("groq:openai/gpt-oss-120b", temperature=0)
reply = model.invoke("What is artificial intelligence? Answer in one sentence.")

print(type(reply).__name__)
print(reply.text)

The reply is an AIMessage with a one-sentence answer, as the prompt asked. The string went to the model as a single human message.

Reading what a message carries

A message holds its type (the role), its content and optional metadata. A HumanMessage accepts a name and an id, and an AIMessage from a model also carries usage_metadata and response_metadata, which Your first LLM app printed.

ExampleNo model call
from langchain.messages import HumanMessage

message = HumanMessage(content="Hello!", name="alice", id="msg_123")
print(message.type, message.name, message.id)
print(message.content)

The type is human, the role of the message. The name and id are labels you can attach, for example to tell two users apart in one conversation or to find a message again later.

Sending a list of messages

Message prompts: system, human and AI messages · from the Complete Agentic AI Course video · 1:11:36 to 1:15:28

A message prompt is a list of message objects. The video names four kinds: a system message tells the model how to behave and gives context for the interaction, a human message is user input, an AI message is a response the model produced, and a tool message carries the output of a tool call. It imports the first three from langchain.messages and builds a list with a system message, You are a poetry expert, and a human message that asks for a poem on artificial intelligence.

python
from langchain.messages import HumanMessage, SystemMessage

messages = [
    SystemMessage("You are a poetry expert. Reply with a poem of four lines."),
    HumanMessage("Write a poem on artificial intelligence."),
]

The page adds a four-line limit to the system message so that the reply stays short. The video's poem came from a model that Groq has retired, so the reply below is from a fresh run.

ExampleAPI keyRun on Groq
from langchain.chat_models import init_chat_model
from langchain.messages import HumanMessage, SystemMessage

model = init_chat_model("groq:openai/gpt-oss-120b", temperature=0)
messages = [
    SystemMessage("You are a poetry expert. Reply with a poem of four lines."),
    HumanMessage("Write a poem on artificial intelligence."),
]
print(model.invoke(messages).text)

The system message set the role and the format. The reply is a four-line poem with no introduction, an instruction the human message never contained.

Giving the model a conversation history

The video ends this part by saying that a list can also hold AI messages, as part of a conversation history. The model has no memory of earlier calls, so a history is the earlier messages that you put in the list. An AIMessage you build yourself works exactly like one the model returned.

ExampleAPI keyRun on Groq
from langchain.chat_models import init_chat_model
from langchain.messages import AIMessage, HumanMessage

model = init_chat_model("groq:openai/gpt-oss-120b", temperature=0)
messages = [
    HumanMessage("Hi, my name is Priya."),
    AIMessage("Nice to meet you, Priya. How can I help?"),
    HumanMessage("What is my name?"),
]
print(model.invoke(messages).text)

The model answered Your name is Priya. The name appears only in the first message of the list, so the model could answer because the history was part of the request. The AIMessage was written by hand, and the model treated it as its own earlier reply.

Passing a tool result back with ToolMessage

A model that wants a tool replies with an AIMessage whose tool_calls list names the tool, its arguments and an id. The program runs the tool and answers with a ToolMessage that carries the result and the same id in tool_call_id, which is how the model matches the result to its request. Tool calling builds the whole loop; here the two messages are written by hand.

ExampleAPI keyRun on Groq
from langchain.chat_models import init_chat_model
from langchain.messages import AIMessage, HumanMessage, ToolMessage

model = init_chat_model("groq:openai/gpt-oss-120b", temperature=0)
messages = [
    HumanMessage("What is the weather in San Francisco?"),
    AIMessage(content="", tool_calls=[{"name": "get_weather", "args": {"location": "San Francisco"}, "id": "call_123"}]),
    ToolMessage(content="Sunny, 22 C", tool_call_id="call_123"),
]
print(model.invoke(messages).text)

The model turned the tool result into a sentence: sunny, about 22 degrees. It called no function. The result arrived as a ToolMessage whose tool_call_id, call_123, matches the id in the tool_calls of the AIMessage before it.

Building messages from a template

When the same prompt is used many times with different values, a prompt template holds the messages with slots in curly braces, and invoke fills them. ChatPromptTemplate.from_messages takes a list of (role, text) pairs. Joining it to the model with |, as in LangChain essentials, gives a chain that takes the values and returns a reply.

ExampleAPI keyRun on Groq
from langchain.chat_models import init_chat_model
from langchain_core.prompts import ChatPromptTemplate

model = init_chat_model("groq:openai/gpt-oss-120b", temperature=0)
prompt = ChatPromptTemplate.from_messages([
    ("system", "You are a {role}. Answer in one sentence."),
    ("human", "{question}"),
])
values = {"role": "pirate", "question": "Why is the sky blue?"}

for message in prompt.invoke(values).to_messages():
    print(message.type, "|", message.content)
print((prompt | model).invoke(values).text)

The loop printed the two messages the template produced: the system message with {role} filled in as pirate, and the human message with the question. prompt | model sent those two messages and returned a one-sentence answer in the voice of a pirate.

Text prompt vs message list vs template

Text promptList of messagesPrompt template
InputOne stringA list of message objectsA dictionary of values
Sent to the model asOne human messageThe list as writtenA list built from the template
Roles and historyNoYesYes, as written in the template
Use it forA single standalone requestInstructions, history and tool resultsThe same prompt with changing values

Where you use messages

  • System prompts: fix the role, tone and rules of an assistant once, in the first message.
  • Chat history: the message list is the conversation, and A chatbot with memory grows and trims it.
  • Agents: tool calls and tool results travel as AIMessage and ToolMessage in the same list.
Watch out. A ToolMessage must carry the tool_call_id of the call it answers, which you copy from the AIMessage. The call above succeeded with a matching id; never make one up, because agents with several tool calls in one reply depend on the id to tell the results apart.
Try it yourself
  • Change the system message in the poem example to You are a pirate and compare the poem.
  • Delete the AIMessage from the history example and run it again. Does the model still know the name?
  • Add a second slot, {language}, to the template and pass it in values.

You understood something today that you didn't yesterday.