03 · Multi-Turn Conversation

One Session threaded through every call turns two stateless one-shots into a single conversation that remembers.

Part 4 of 91 Microsoft Agent Framework Go — Every Lesson

What this lesson demonstrates

On its own, every RunText call is stateless — the model sees only the message you hand it. A Session threads the earlier turns back into the next call, so a follow-up like “now add emojis to the joke” actually refers to the joke told a moment ago. The agent builder here is byte-for-byte the one from 01_hello_agent; the only new ingredient is agent.WithSession. Multi-turn is purely additive.

The code

Create a session once, then pass it to every call:

session, err := a.CreateSession(ctx)
// ...

// Turn 1: set up the joke.
resp, err := a.RunText(ctx, "Tell me a joke about a pirate.", agent.WithSession(session)).Collect()

// Turn 2: "the joke" refers back to turn 1 — only works because the same
// session is threaded through.
resp, err = a.RunText(ctx,
    "Now add some emojis to the joke and tell it in the voice of a pirate's parrot.",
    agent.WithSession(session),
).Collect()

What to notice

How it maps to Azure AI Foundry

With a session, each request to Foundry carries the prior turns as context, not just the latest message. The model resolves references like “the joke” because it literally receives the earlier exchange again. This is client-side conversation history managed by the SDK — the session holds the transcript and replays it on every POST /responses, which is why serializing the session is enough to carry a conversation across process boundaries later.

Run it

go run ./tutorial/01-get-started/03_multi_turn

Expected: a pirate joke, then the same joke re-told with emojis in a parrot’s voice — proof turn 2 saw turn 1. Offline, TestNewJoker_Wiring checks the agent name and TestCreateSession_Offline confirms the session round-trips a stored value — no network. The two-turn live conversation is TestMultiTurn_Live, gated behind AF_LIVE=1.


Next: 04 · Memory

Sources & References

The Go SDK this lesson exercises
Sessions and multi-turn conversation state