Why it matters
Agents that skip lifecycle steps leak resources, lose state, and behave unpredictably. Getting the pattern right is foundational.
The architecture
Init phase: construct Agent instance with tools, model reference, and configuration. Connect to LLM (auth, endpoint). Initialize memory/session backends.
Turn phase: receive user message, add to session context, invoke model, dispatch tool calls, receive tool results, feed back to model, produce final response.
How it works end to end
Runner is the orchestrator. It receives user messages, calls the agent, handles tool dispatch, and streams responses back. Runner manages the loop and error handling.
Callbacks and interceptors let you observe or modify agent behavior at each lifecycle step: log tool calls, add rate limiting, insert human approval.
Shutdown: flush session state, close model connections, release tool resources. Missing shutdown can leak memory or corrupt state.