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 asyncio
2import json
3
4import httpx
5from kern.agent import Agent
6from kern.db.sqlite import SqliteDb
7from kern.models.openai import OpenAIResponses
8from kern.tools import tool
9from kern.utils import pprint
10from rich.console import Console
11from rich.prompt import Prompt
12
13console = Console()
14
15
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(db_file="tmp/confirmation_required_mixed_tools.db"),
48)
49
50run_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 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 if message == "n":
65 requirement.reject()
66 else:
67 requirement.confirm()
68
69
70run_response = asyncio.run(agent.acontinue_run(run_response=run_response))
71# Or
72# run_response = asyncio.run(agent.acontinue_run(run_id=run_response.run_id))
73
74pprint.pprint_run_response(run_response)
75
76
77# Or for simple debug flow
78# asyncio.run(agent.aprint_response("Fetch the top 2 hackernews stories"))

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_mixed_tools.py