Confirmation Required with Multiple Tools

This example demonstrates human-in-the-loop functionality with multiple tools that require confirmation. It shows how to handle user confirmation during tool execution and gracefully cancel operations based on user choice.

Create a Python file

1import json
2import httpx
3from kern.agent import Agent
4from kern.db.sqlite import SqliteDb
5from kern.models.openai import OpenAIResponses
6from kern.tools import tool
7from kern.tools.wikipedia import WikipediaTools
8from kern.utils import pprint
9from rich.console import Console
10from rich.prompt import Prompt
11
12console = Console()
13
14
15@tool(requires_confirmation=True)
16def get_top_hackernews_stories(num_stories: int) -> str:
17 """Fetch top stories from Hacker News.
18
19 Args:
20 num_stories (int): Number of stories to retrieve
21
22 Returns:
23 str: JSON string containing story details
24 """
25 # Fetch top story IDs
26 response = httpx.get("https://hacker-news.firebaseio.com/v0/topstories.json")
27 story_ids = response.json()
28
29 # Yield story details
30 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)
40
41
42agent = Agent(
43 model=OpenAIResponses(id="gpt-5.2"),
44 tools=[
45 get_top_hackernews_stories,
46 WikipediaTools(requires_confirmation_tools=["search_wikipedia"]),
47 ],
48 markdown=True,
49 db=SqliteDb(db_file="tmp/example.db"),
50)
51
52run_response = agent.run(
53 "Fetch 2 articles about the topic 'python'. You can choose which source to use, but only use one source."
54)
55while run_response.is_paused:
56 for requirement in run_response.active_requirements:
57 if requirement.needs_confirmation:
58 # Ask for confirmation
59 console.print(
60 f"Tool name [bold blue]{requirement.tool_execution.tool_name}({requirement.tool_execution.tool_args})[/] requires confirmation."
61 )
62 message = (
63 Prompt.ask("Do you want to continue?", choices=["y", "n"], default="y")
64 .strip()
65 .lower()
66 )
67
68 if message == "n":
69 requirement.reject(
70 "This is not the right tool to use. Use the other tool!"
71 )
72 else:
73 requirement.confirm()
74
75 run_response = agent.continue_run(
76 run_id=run_response.run_id,
77 requirements=run_response.requirements,
78 )
79 pprint.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_multiple_tools.py