v0.8.0
Key Changes
Human-in-the-Loop (HITL)
The human-in-the-loop (HITL) flow enables your agents to pause execution until a person approves or rejects sensitive tool calls. Tools declare when they need approval, run results surface pending approvals as interruptions, and RunState lets you serialize and resume runs after decisions are made.
import asyncio
from agents import Agent, Runner, RunState, function_tool
@function_tool
async def get_weather(city: str) -> str:
return f"The weather in {city} is sunny"
# This function requires approval
@function_tool(needs_approval=True)
async def get_temperature(city: str) -> str:
return f"The temperature in {city} is 20° Celsius"
agent = Agent(
name="Weather Assistant",
instructions="You are a helpful weather assistant. Answer questions about weather and temperature using the available tools.",
tools=[get_weather, get_temperature],
)
async def main():
result = await Runner.run(agent, "What is the weather and temperature in Oakland?")
has_interruptions = len(result.interruptions) > 0
while has_interruptions:
state = result.to_state()
# Process each interruption
for interruption in result.interruptions:
print("\nTool call details:")
print(f" Agent: {interruption.agent.name}")
print(f" Tool: {interruption.name}")
print(f" Arguments: {interruption.arguments}")
confirmed = await confirm("\nDo you approve this tool call?")
if confirmed:
print(f"✓ Approved: {interruption.name}")
state.approve(interruption)
else:
print(f"✗ Rejected: {interruption.name}")
state.reject(interruption)
# Resume execution with the updated state
print("\nResuming agent execution...")
result = await Runner.run(agent, state)
has_interruptions = len(result.interruptions) > 0
print(result.final_output)