Agents don't remember anything by default

Each run_sync call is independent, the agent has no memory of a previous call unless you give it one. This is the same statelessness you saw in the LangChain course: a model call is a pure function of the messages you send it, nothing is remembered server-side.

Carrying history forward

Every AgentRunResult exposes new_messages(), just the messages produced by that run, and all_messages(), the full history including whatever was passed in. Pass either into the next call's message_history= to continue the conversation.

from dotenv import load_dotenv
from pydantic_ai import Agent
load_dotenv()
agent = Agent("google:gemini-3.5-flash-lite")
def main() -> None:
conversation = [
"My favorite color is teal. Remember that.",
"What is my favorite color?",
"What did I just ask you?",
]
history = []
for user_input in conversation:
result = agent.run_sync(user_input, message_history=history)
print(f"User: {user_input}")
print(f"Agent: {result.output}\n")
history = result.all_messages()
print(f"Total messages accumulated: {len(history)}")

message_history accepts ModelMessage objects, the same typed representation of a request/response pair the SDK builds internally, you're not hand-assembling role/content dicts the way you might with a raw chat completions API.

Building a conversation loop

Reassigning history = result.all_messages() each turn, rather than new_messages(), is what keeps the whole conversation, not just the latest exchange, in context for the next call. This is the direct equivalent of managing a growing list of HumanMessage/AIMessage objects yourself in LangChain, just with the accumulation done for you by all_messages().

Checkpoint

  • run_sync calls are stateless by default; nothing carries over unless you pass message_history=.
  • result.new_messages() is just this turn's messages; result.all_messages() is the whole history so far.
  • A conversation loop reassigns history = result.all_messages() after every turn to keep the full context.

If anything here still feels unclear, ask before moving to Lesson 12, where an agent calls another agent as a tool.