Multi Turn

An Agent is stateless — two runs are strangers. What carries history from one turn to the next is an AgentSession.

Part 3 of 72 Microsoft Agent Framework Python — Every Lesson

What this lesson demonstrates

agent.run("A") then agent.run("B") are two unrelated calls: the model never sees “A” while answering “B”. The lesson proves this with a stateless demo (the second turn has amnesia), then fixes it by threading one AgentSession through both runs so turn two can see turn one. The agent stays stateless; the session is the memory.

The code

Create a session once, pass it to every run in the conversation:

session = agent.create_session()          # an in-memory conversation buffer
await agent.run("My name is Pratik and I bank with HDFC.", session=session)
r = await agent.run(
    "Draft a one-line message to my bank's support desk, signed with my name.",
    session=session,
)
print(f"Agent: {r}")

Without session=, the same two turns produce an agent that has no idea who “me” is or which bank.

What to notice

How it maps to Azure AI Foundry

The session accumulates the message list that gets sent to the Foundry Responses API on each call. Foundry itself is stateless per request; the SDK’s session is what replays prior turns so the model has context. Same FoundryChatClient + AzureCliCredential.

Run it

uv run tutorial/01-get-started/03_multi_turn.py

Expected: the stateless run forgets the bank; the stateful run remembers name and bank. Needs Foundry creds and az login.


Next: Memory

Sources & References

The Python SDK this lesson exercises
Maintaining conversation history with sessions