Skip to content
AgenticAIWiki
  1. Learn
  2. Part 11
  3. 62. Long documents and large codebases
  4. 62.3

Lesson 62.3 · 3 of 3 · 4 min read · Reviewed 24 Nov 2026

Multi-turn state that lasts

By the end, you can

  • Keep state reliable across many turns.
  • Separate conversation from structured state.

A model can work out the current state from the conversation, but in long or messy conversations it sometimes gets it wrong: the user changed the date twice, then changed it back.

Make state explicit

Keep a structured record your code owns:

{"booking": {"city": "Lisbon", "check_in": "2026-03-12", "check_out": "2026-03-15",
             "guests": 2, "max_price_eur": 150, "status": "searching"}}
  • The agentAgent: A system where a model decides its own next steps, calling tools in a loop until the task is done. changes it only through a tool, such as update_booking(field, value).
  • Your code validates each change.
  • Every turn includes the current state, so the model doesn’t have to piece it together.

Benefits

  • Fewer mix-ups from corrections and reversals.
  • A clear audit trailAudit trail: A tamper-resistant record of who did what, when, and with which model and prompt version. of what changed and when.
  • Easy hand-off to a person or another agent: just pass the state.

This is the same idea as the case-facts block (60.4), applied to everything the task depends on.

Key points

  • In long interactions, keep important state in structured form, not only in the chat history.
  • Update state through tools so changes are explicit and logged.
  • Give the model the current state each turn, rather than making it reconstruct state from history.
Hands-on labRuns in the Playground

Keep booking state outside the chat

Hold a booking's current state in a validated record that changes only through a tool.

You will have: An update_booking() tool with validation and an audit log, and the final state printed as a block for the next turn.

Steps

  1. Run the starter: every change is accepted, including 12 guests and an unknown field.
  2. Reject unknown fields and guest counts outside 1 to 8.
  3. Reject a check-out date on or before the check-in date.
  4. Log each accepted change as (field, old, new) and print the state block the model will see next turn.

Done when

  • The final state has check_in 2026-03-12 and guests 2, after the user changed the date twice.
  • Exactly 6 changes are accepted and 2 are rejected with clear reasons.

Open in the Playground No API key needed.

Starter code (Python)
import json
# Keep booking state in a structured record your code owns, not only in the chat.
STATE = {"city": None, "check_in": None, "check_out": None, "guests": None, "status": "searching"}
LOG = []
ALLOWED = {"city", "check_in", "check_out", "guests"}

def update_booking(field, value):
    # TODO: reject unknown fields, guests outside 1..8, and check_out on or before check_in (ISO dates compare as strings)
    # TODO: record (field, old, new) in LOG for every accepted change
    STATE[field] = value
    return "ok"

turns = [("city", "Lisbon"), ("check_in", "2026-03-12"), ("check_out", "2026-03-15"), ("guests", 2),
         ("check_in", "2026-03-14"), ("check_in", "2026-03-12"), ("guests", 12), ("price", 150)]
for f, v in turns:
    print(f, v, "->", update_booking(f, v))
print(json.dumps(STATE))
Show a solution
import json
STATE = {"city": None, "check_in": None, "check_out": None, "guests": None, "status": "searching"}
LOG = []
ALLOWED = {"city", "check_in", "check_out", "guests"}

def update_booking(field, value):
    if field not in ALLOWED:
        return f"error: unknown field {field}"
    if field == "guests" and not (1 <= value <= 8):
        return "error: guests must be 1 to 8"
    ci = value if field == "check_in" else STATE["check_in"]
    co = value if field == "check_out" else STATE["check_out"]
    if ci and co and co <= ci:
        return "error: check_out must be after check_in"
    LOG.append((field, STATE[field], value))
    STATE[field] = value
    return "ok"

turns = [("city", "Lisbon"), ("check_in", "2026-03-12"), ("check_out", "2026-03-15"), ("guests", 2),
         ("check_in", "2026-03-14"), ("check_in", "2026-03-12"), ("guests", 12), ("price", 150)]
for f, v in turns:
    print(f, v, "->", update_booking(f, v))
print(json.dumps(STATE))
print("accepted changes:", len(LOG))
print("context for the next turn:", "<booking_state>" + json.dumps(STATE) + "</booking_state>")
Quick checkA booking assistant sometimes forgets which dates the user changed three turns ago. What's the best fix?
How sure are you?
Pick an answer and how sure you are

Finished the chapter? Take the chapter check.