Confirmation Required with Mixed Tools
This example demonstrates human-in-the-loop functionality where only some tools require user confirmation. The agent executes tools that don't require confirmation automatically and pauses only for tools that need approval.
Create a Python file
1import asyncio2import json34import httpx5from kern.agent import Agent6from kern.db.sqlite import SqliteDb7from kern.models.openai import OpenAIResponses8from kern.tools import tool9from kern.utils import pprint10from rich.console import Console11from rich.prompt import Prompt1213console = Console()141516@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(db_file="tmp/confirmation_required_mixed_tools.db"),48)4950run_response = asyncio.run(agent.arun("Fetch the top 2 hackernews stories"))51if run_response.is_paused:52 for 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 if message == "n":65 requirement.reject()66 else:67 requirement.confirm()686970run_response = asyncio.run(agent.acontinue_run(run_response=run_response))71# Or72# run_response = asyncio.run(agent.acontinue_run(run_id=run_response.run_id))7374pprint.pprint_run_response(run_response)757677# Or for simple debug flow78# 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_mixed_tools.py