Tool Confirmation Required
This example demonstrates how to implement human-in-the-loop functionality by requiring user confirmation before executing sensitive tool operations.
Create a Python file
1import json23import httpx4from kern.agent import Agent5from kern.db.sqlite import SqliteDb6from kern.models.openai import OpenAIResponses7from kern.tools import tool8from kern.utils import pprint9from rich.console import Console10from rich.prompt import Prompt1112console = Console()131415# This tool will require user confirmation before execution16@tool(requires_confirmation=True)17def get_top_hackernews_stories(num_stories: int) -> str:18 """Fetch top stories from Hacker News.1920 Args:21 num_stories (int): Number of stories to retrieve2223 Returns:24 str: JSON string containing story details25 """26 # Fetch top story IDs27 response = httpx.get("https://hacker-news.firebaseio.com/v0/topstories.json")28 story_ids = response.json()2930 # Yield story details31 all_stories = []32 for story_id in story_ids[:num_stories]:33 story_response = httpx.get(34 f"https://hacker-news.firebaseio.com/v0/item/{story_id}.json"35 )36 story = story_response.json()37 if "text" in story:38 story.pop("text", None)39 all_stories.append(story)40 return json.dumps(all_stories)414243agent = Agent(44 model=OpenAIResponses(id="gpt-5.2"),45 tools=[get_top_hackernews_stories],46 markdown=True,47 db=SqliteDb(session_table="test_session", db_file="tmp/example.db"),48)4950run_response = agent.run("Fetch the top 2 hackernews stories.")5152for requirement in run_response.active_requirements:53 if requirement.needs_confirmation:54 # Ask for confirmation55 console.print(56 f"Tool name [bold blue]{requirement.tool_execution.tool_name}({requirement.tool_execution.tool_args})[/] requires confirmation."57 )58 message = (59 Prompt.ask("Do you want to continue?", choices=["y", "n"], default="y")60 .strip()61 .lower()62 )6364 # Confirm or reject the requirement65 if message == "n":66 requirement.reject()67 else:68 requirement.confirm()697071run_response = agent.continue_run(72 run_id=run_response.run_id,73 requirements=run_response.requirements,74)7576# You can also pass the updated tools when continuing the run:77# run_response = agent.continue_run(78# run_id=run_response.run_id,79# updated_tools=run_response.tools,80# )8182pprint.pprint_run_response(run_response)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.py