Async Tool Confirmation Required
This example demonstrates how to implement human-in-the-loop functionality with async agents, requiring user confirmation before executing tool operations.
Create a Python file
1import asyncio2import json34import httpx5from kern.agent import Agent6from kern.models.openai import OpenAIResponses7from kern.tools import tool8from kern.utils import pprint9from rich.console import Console10from rich.prompt import Prompt1112console = Console()131415@tool(requires_confirmation=True)16def get_top_hackernews_stories(num_stories: int) -> str:17 """Fetch top stories from Hacker News.1819 Args:20 num_stories (int): Number of stories to retrieve2122 Returns:23 str: JSON string containing story details24 """25 # Fetch top story IDs26 response = httpx.get("https://hacker-news.firebaseio.com/v0/topstories.json")27 story_ids = response.json()2829 # Yield story details30 all_stories = []31 for story_id in story_ids[:num_stories]:32 story_response = httpx.get(33 f"https://hacker-news.firebaseio.com/v0/item/{story_id}.json"34 )35 story = story_response.json()36 if "text" in story:37 story.pop("text", None)38 all_stories.append(story)39 return json.dumps(all_stories)404142agent = Agent(43 model=OpenAIResponses(id="gpt-5.2"),44 tools=[get_top_hackernews_stories],45 markdown=True,46)4748run_response = asyncio.run(agent.arun("Fetch the top 2 hackernews stories"))49if run_response.is_paused:50 for requirement in run_response.active_requirements:51 if requirement.needs_confirmation:52 # Ask for confirmation53 console.print(54 f"Tool name [bold blue]{requirement.tool_execution.tool_name}({requirement.tool_execution.tool_args})[/] requires confirmation."55 )56 message = (57 Prompt.ask("Do you want to continue?", choices=["y", "n"], default="y")58 .strip()59 .lower()60 )6162 if message == "n":63 requirement.reject()64 else:65 requirement.confirm()666768run_response = asyncio.run(agent.acontinue_run(run_response=run_response))69# Or70# run_response = asyncio.run(agent.acontinue_run(run_id=run_response.run_id))7172pprint.pprint_run_response(run_response)737475# Or for simple debug flow76# asyncio.run(agent.aprint_response("Fetch the top 2 hackernews stories"))Set up your virtual environment
1uv venv --python 3.122source .venv/bin/activate1uv venv --python 3.122.venv\Scripts\activateInstall dependencies
1uv pip install -U kern-ai openai httpx richExport your OpenAI API key
1export OPENAI_API_KEY="your_openai_api_key_here"1$Env:OPENAI_API_KEY="your_openai_api_key_here"Run Agent
1python confirmation_required_async.py