Confirmation Toolkit

Human-in-the-Loop: Adding User Confirmation to Tool Calls.

1"""
2Confirmation Toolkit
3=============================
4
5Human-in-the-Loop: Adding User Confirmation to Tool Calls.
6"""
7
8from kern.agent import Agent
9from kern.db.sqlite import SqliteDb
10from kern.models.openai import OpenAIResponses
11from kern.tools.websearch import WebSearchTools
12from kern.utils import pprint
13from rich.console import Console
14from rich.prompt import Prompt
15
16console = Console()
17
18# ---------------------------------------------------------------------------
19# Create Agent
20# ---------------------------------------------------------------------------
21agent = Agent(
22 model=OpenAIResponses(id="gpt-5-mini"),
23 tools=[WebSearchTools(requires_confirmation_tools=["web_search"])],
24 markdown=True,
25 db=SqliteDb(db_file="tmp/confirmation_required_toolkit.db"),
26)
27
28# ---------------------------------------------------------------------------
29# Run Agent
30# ---------------------------------------------------------------------------
31if __name__ == "__main__":
32 run_response = agent.run("What is the current stock price of Apple?")
33 if run_response.is_paused: # Or agent.run_response.is_paused
34 for requirement in run_response.active_requirements:
35 if requirement.needs_confirmation:
36 # Ask for confirmation
37 console.print(
38 f"Tool name [bold blue]{requirement.tool_execution.tool_name}({requirement.tool_execution.tool_args})[/] requires confirmation."
39 )
40 message = (
41 Prompt.ask(
42 "Do you want to continue?", choices=["y", "n"], default="y"
43 )
44 .strip()
45 .lower()
46 )
47
48 if message == "n":
49 requirement.reject()
50 else:
51 requirement.confirm()
52
53 run_response = agent.continue_run(
54 run_id=run_response.run_id,
55 requirements=run_response.requirements,
56 )
57 pprint.pprint_run_response(run_response)

Run the Example

1# Clone and setup repo
2git clone https://github.com/kern-ai/kern.git
3cd kern/cookbook/02_agents/10_human_in_the_loop
4
5# Create and activate virtual environment
6./scripts/demo_setup.sh
7source .venvs/demo/bin/activate
8
9python confirmation_toolkit.py