Streaming with External Execution of Tool
This example demonstrates how a team pauses when a member agent's tool requires external execution in streaming mode.
This example demonstrates how a team pauses when a member agent's tool requires external execution in streaming mode. The tool result is provided by the caller rather than being executed by the agent.
1"""Team HITL Streaming: Member agent tool with external execution.23This example demonstrates how a team pauses when a member agent's tool4requires external execution in streaming mode. The tool result is provided5by the caller rather than being executed by the agent.67Note: When streaming with member agents, use isinstance() with TeamRunPausedEvent8to distinguish the team's pause from member agent pauses.9"""1011import shlex12import subprocess1314from kern.agent import Agent15from kern.db.sqlite import SqliteDb16from kern.models.openai import OpenAIResponses17from kern.run.team import RunPausedEvent as TeamRunPausedEvent18from kern.team.team import Team19from kern.tools import tool20from kern.utils import pprint2122# ---------------------------------------------------------------------------23# Setup24# ---------------------------------------------------------------------------25db = SqliteDb(db_file="tmp/team_hitl_stream.db")262728# ---------------------------------------------------------------------------29# Tools30# ---------------------------------------------------------------------------31@tool(external_execution=True)32def run_shell_command(command: str) -> str:33 """Execute a shell command on the server.3435 Args:36 command (str): The shell command to execute37 """38 return subprocess.check_output(shlex.split(command)).decode("utf-8")394041# ---------------------------------------------------------------------------42# Create Members43# ---------------------------------------------------------------------------44ops_agent = Agent(45 name="Ops Agent",46 role="Handles server operations",47 model=OpenAIResponses(id="gpt-5.2-mini"),48 tools=[run_shell_command],49 db=db,50)515253# ---------------------------------------------------------------------------54# Create Team55# ---------------------------------------------------------------------------56team = Team(57 name="SRE Team",58 members=[ops_agent],59 model=OpenAIResponses(id="gpt-5.2-mini"),60 db=db,61)626364# ---------------------------------------------------------------------------65# Run Team66# ---------------------------------------------------------------------------67if __name__ == "__main__":68 for run_event in team.run("List the files in the current directory", stream=True):69 # Use isinstance to check for team's pause event (not the member agent's)70 if isinstance(run_event, TeamRunPausedEvent):71 print("Team paused - requires external execution")72 for req in run_event.active_requirements:73 if req.needs_external_execution:74 print(f" Tool: {req.tool_execution.tool_name}")75 print(f" Args: {req.tool_execution.tool_args}")7677 # Execute the tool externally78 result = run_shell_command.entrypoint(79 **req.tool_execution.tool_args80 )81 req.set_external_execution_result(result)8283 response = team.continue_run(84 run_id=run_event.run_id,85 session_id=run_event.session_id,86 requirements=run_event.requirements,87 stream=True,88 )89 pprint.pprint_run_response(response)Run the Example
1# Clone and setup repo2git clone https://github.com/kern-ai/kern.git3cd kern/cookbook/03_teams/human_in_the_loop45# Create and activate virtual environment6./scripts/demo_setup.sh7source .venvs/demo/bin/activate89python external_tool_execution_stream.py