Follow-ups and recap behavior
Persistence alone is not enough. The supervisor and subagents have to actually read the history when classifying and answering. Without that, memory is just dead weight in state. We feed the last few turns into the supervisor prompt so follow-ups like "what about mine?" can be disambiguated.
def _format_history(messages: List[Dict[str, str]]) -> str:
if not messages:
return '(no prior turns)'
lines = []
for m in messages[-6:]: # cap context
role = m.get('role', 'user')
lines.append(f"{role}: {m.get('content','')}")
return '\n'.join(lines)A six-message window (three user turns, three assistant turns) is enough context for most follow-up questions without blowing up the supervisor prompt. Longer windows cost more tokens and diminishing returns. Pick a window, measure, adjust.
async def run_chat(...) -> Dict[str, Any]:
# ... load snapshot, build initial state, run astream ...
final_state: Dict[str, Any] = {}
async for event in graph.astream(initial, config=_thread_config(thread_id)):
for _node, node_state in event.items():
final_state.update(node_state or {})
answer = final_state.get('answer', '')
# Persist assistant turn into messages for the next call.
history.append({'role': 'assistant', 'content': answer})
graph.update_state(_thread_config(thread_id), {'messages': history})
return {
'thread_id': thread_id,
'route': final_state.get('route'),
'answer': answer,
'citations': final_state.get('citations', []) or [],
}After each run, we manually append the assistant turn to history and call update_state. The checkpointer writes the new messages list, so the next request starts with a complete conversation.
Token budget. The supervisor prompt runs on every single turn. Dragging the full history in makes every classification call slower and more expensive. Six messages is enough to disambiguate most follow-ups. For long conversations you eventually need a summarization step that compresses older turns into a running summary. Start with a window, add summarization when the window is no longer enough.
Checkpoint: Multi-agent system checkpoint
Loading practice…