run_demo_loop(
agent: Agent[Any],
*,
stream: bool = True,
context: TContext | None = None,
max_turns: int | None = DEFAULT_MAX_TURNS,
) -> None
Run a simple REPL loop with the given agent.
This utility allows quick manual testing and debugging of an agent from the
command line. Conversation state is preserved across turns. When a tool needs
approval, enter y or yes to approve that call. Enter exit or
quit at either prompt to stop the loop.
Parameters:
| Name |
Type |
Description |
Default |
agent
|
Agent[Any]
|
The starting agent to run.
|
required
|
stream
|
bool
|
Whether to stream the agent output.
|
True
|
context
|
TContext | None
|
Additional context information to pass to the runner.
|
None
|
max_turns
|
int | None
|
Maximum number of turns for the runner to iterate. Pass None to disable
the turn limit.
|
DEFAULT_MAX_TURNS
|
Source code in src/agents/repl.py
| async def run_demo_loop(
agent: Agent[Any],
*,
stream: bool = True,
context: TContext | None = None,
max_turns: int | None = DEFAULT_MAX_TURNS,
) -> None:
"""Run a simple REPL loop with the given agent.
This utility allows quick manual testing and debugging of an agent from the
command line. Conversation state is preserved across turns. When a tool needs
approval, enter ``y`` or ``yes`` to approve that call. Enter ``exit`` or
``quit`` at either prompt to stop the loop.
Args:
agent: The starting agent to run.
stream: Whether to stream the agent output.
context: Additional context information to pass to the runner.
max_turns: Maximum number of turns for the runner to iterate. Pass ``None`` to disable
the turn limit.
"""
current_agent = agent
input_items: list[TResponseInputItem] = []
while True:
user_input = _read_input(" > ")
if user_input is None:
break
if not user_input.strip():
continue
input_items.append({"role": "user", "content": user_input})
run_input: list[TResponseInputItem] | RunState[Any] = input_items
result: RunResult | RunResultStreaming
while True:
run_context = None if isinstance(run_input, RunState) else context
if stream:
result = Runner.run_streamed(
current_agent, run_input, context=run_context, max_turns=max_turns
)
await _print_stream(result)
else:
result = await Runner.run(
current_agent, run_input, context=run_context, max_turns=max_turns
)
if result.final_output is not None:
print(result.final_output)
if not result.interruptions:
break
state = result.to_state()
for item in result.interruptions:
details = {
key: value
for key in ("server_label", "action", "environment", "operation", "operations")
if (value := get_mapping_or_attr(item.raw_item, key)) is not None
}
if item.tool_namespace is not None:
details["namespace"] = item.tool_namespace
if (arguments := item.arguments) is not None:
details["arguments"] = arguments
print(f"\n{item.agent.name!r} requested {item.name!r}: {details!r}")
answer = _read_input("Approve this tool call? [y/N] ")
if answer is None:
return
if answer.strip().lower() in {"y", "yes"}:
state.approve(item)
else:
state.reject(item)
run_input = state
current_agent = result.last_agent
input_items = result.to_input_list()
|