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 json
2
3import httpx
4from kern.agent import Agent
5from kern.db.sqlite import SqliteDb
6from kern.models.openai import OpenAIResponses
7from kern.tools import tool
8from kern.utils import pprint
9from rich.console import Console
10from rich.prompt import Prompt
11
12console = Console()
13
14
15# This tool will require user confirmation before execution
16@tool(requires_confirmation=True)
17def get_top_hackernews_stories(num_stories: int) -> str:
18 """Fetch top stories from Hacker News.
19
20 Args:
21 num_stories (int): Number of stories to retrieve
22
23 Returns:
24 str: JSON string containing story details
25 """
26 # Fetch top story IDs
27 response = httpx.get("https://hacker-news.firebaseio.com/v0/topstories.json")
28 story_ids = response.json()
29
30 # Yield story details
31 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)
41
42
43agent = 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)
49
50run_response = agent.run("Fetch the top 2 hackernews stories.")
51
52for requirement in run_response.active_requirements:
53 if requirement.needs_confirmation:
54 # Ask for confirmation
55 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 )
63
64 # Confirm or reject the requirement
65 if message == "n":
66 requirement.reject()
67 else:
68 requirement.confirm()
69
70
71run_response = agent.continue_run(
72 run_id=run_response.run_id,
73 requirements=run_response.requirements,
74)
75
76# 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# )
81
82pprint.pprint_run_response(run_response)

Set up your virtual environment

1uv venv --python 3.12
2source .venv/bin/activate
1uv venv --python 3.12
2.venv\Scripts\activate

Install dependencies

1uv pip install -U kern-ai openai httpx rich

Export 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