Guardrails, sessions, and tracing
Guardrails, sessions, and tracing
Module nine built three things around the loop: checks at the seams, memory between requests, and a record of what happened. The Agents SDK ships all three. Guardrails are functions that run on the input before the agent starts, or on the output before it is returned, and can stop the run. Sessions load the conversation before a run and save it after. Tracing records every model call, tool call, handoff, and guardrail in a run so you can open it and see what the agent did. None of this replaces the thinking from module nine; it replaces the plumbing.
A guardrail is an async function decorated with @input_guardrail or @output_guardrail. It returns a GuardrailFunctionOutput with two fields: output_info, which is whatever you want recorded, and tripwire_triggered, which stops the run when true. A guardrail can be a regular expression, a Pydantic check, or a whole second agent.
Continue reading
Pro unlocks every video lesson, the full notes and runnable code across the Python with AI course, from the language itself to agents, MCP, Langfuse and deployment.