`run_step()` is an asynchronous `TaskHandler` method that executes one `TaskStep` and returns a `TaskStepResult`. In the agent’s processing loop, it works with `get_next_step()`: the latter uses the previous result to choose another step or return the overall `TaskResult`. The loop stops on completion, an irrecoverable error, or a specified step limit. section 4.2.3 section 4.5
A step execution maintains the context needed for subsequent decisions:
- Increment `step_counter` and construct a system message, using the current rollout when one exists. section 4.2.3
- Conduct a single-turn LLM chat interaction. If the response contains tool calls, execute them through the tool registry; the implementation uses `asyncio.gather()` for the calls. section 4.2.3
- Format the step’s chat history and append it to `rollout`, leaving a record of the work available to later steps. section 4.2.3
Listing 4.9 Implementing TaskHandler: run_step() — 2 of 3
# llm_agents_from_scratch/agent/llm_agent.py
… #A
class LLMAgent:
… #B
class TaskHandler(asyncio.Future):
… #C
async def run_step(self, step: TaskStep) -> TaskStepResult:
"""Run next step of a given task."""
… #D
if response_message.tool_calls: #E
async def _execute_tool_call( #F
tool_call: ToolCall,
) -> ToolCallResult:
if tool := (
self.llm_agent.tools_registry.get(
tool_call.tool_name,
)
):
… #G
return tool_call_result #H
tool_call_results = await asyncio.gather( #I
*[
_execute_tool_call(tc)
for tc in response_message.tool_calls
],
)
(
tool_messages,
another_response_message,
) = await self.llm_agent.llm.continue_chat_with_tool_results( #J
tool_call_results=tool_call_results,
chat_history=[
system_message,
user_message,
response_message,
],
tools=all_tools,
)The implementation excerpt shows tool-call handling within `run_step()`, including concurrent execution with `asyncio.gather()`. section 4.2.3
Supervised execution
For a Hailstone task starting at 4, an operator calls `await task_handler.get_next_step(None)`, inspects or modifies the returned `TaskStep`, then calls `await task_handler.run_step(step)`. The operator inspects the result and repeats until `get_next_step()` returns a `TaskResult`, which can be accepted with `complete()`. A `TaskStep` cannot be passed to `complete()`. section 8.4.2
The step is also an extension point. Recalled memories can be injected into its system message. Subagent and A2A agent catalogs disclose available agents, while their respective tools let the LLM dispatch work during tool-call execution. Remote A2A dispatch occurs within `run_step()` because tool calls execute there. section 7.5.2 section 9.2.3 section 10.2.3
