Confirmation Required with Run ID

This example demonstrates human-in-the-loop functionality using specific run IDs for session management. It shows how to continue agent execution with updated tools using run identifiers.

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.utils import pprint
8from rich.console import Console
9from rich.prompt import Prompt
10
11console = Console()
12
13
14@tool(requires_confirmation=True)
15def get_top_hackernews_stories(num_stories: int) -> str:
16 """Fetch top stories from Hacker News.
17
18 Args:
19 num_stories (int): Number of stories to retrieve
20
21 Returns:
22 str: JSON string containing story details
23 """
24 # Fetch top story IDs
25 response = httpx.get("https://hacker-news.firebaseio.com/v0/topstories.json")
26 story_ids = response.json()
27
28 # Yield story details
29 all_stories = []
30 for story_id in story_ids[:num_stories]:
31 story_response = httpx.get(
32 f"https://hacker-news.firebaseio.com/v0/item/{story_id}.json"
33 )
34 story = story_response.json()
35 if "text" in story:
36 story.pop("text", None)
37 all_stories.append(story)
38 return json.dumps(all_stories)
39
40
41agent = Agent(
42 model=OpenAIResponses(id="gpt-5.2"),
43 tools=[get_top_hackernews_stories],
44 markdown=True,
45 db=SqliteDb(session_table="test_session", db_file="tmp/example.db"),
46)
47
48run_response = agent.run("Fetch the top 2 hackernews stories.")
49if run_response.is_paused:
50 for requirement in run_response.active_requirements:
51 if requirement.needs_confirmation:
52 # Ask for confirmation
53 console.print(
54 f"Tool name [bold blue]{requirement.tool_execution.tool_name}({requirement.tool_execution.tool_args})[/] requires confirmation."
55 )
56 message = (
57 Prompt.ask("Do you want to continue?", choices=["y", "n"], default="y")
58 .strip()
59 .lower()
60 )
61
62 if message == "n":
63 requirement.reject()
64 else:
65 requirement.confirm()
66
67run_response = agent.continue_run(
68 run_id=run_response.run_id,
69 requirements=run_response.requirements,
70)
71
72pprint.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_with_run_id.py