A TaskStep represents one instruction-bearing execution step within an overarching Task. In an LLMAgent processing loop, each sub-step involves LLM planning and may involve tool calling; its result helps determine what happens next. section 4.2.1
The Python model contains three fields:
- id_: a string identifier generated by default from a UUID. section 4.2.2
- task_id: the identifier linking the step to its overarching Task. section 4.2.2
- instruction: the instruction to execute for this step. Converting the TaskStep to a string returns this instruction. section 4.2.2
Listing 4.5 Implementing TaskStep, TaskStepResult and NextStepDecision
# llm_agents_from_scratch/data_structures/agent.py
… #A
… #B
class TaskStep(BaseModel):
"""Represents a step within a task and its own instruction.""" #C
id_: str = Field(
default_factory=lambda: str(uuid.uuid4())
)
task_id: str #D
instruction: str = Field(
description="The instruction for this step in the task.",
)
def __str__(self) -> str:
"""String representation of TaskStep."""
return self.instruction
class TaskStepResult(BaseModel):
"""The result of a task step execution."""
task_step_id: str
content: str
def __str__(self) -> str:
"""String representation of TaskStepResult."""
return self.content
class NextStepDecision(BaseModel):
"""Structured output used within TaskHandler.get_next_step()."""
kind: Literal["next_step", "final_result"] #E
content: str = Field(
description=(
"Content for the next step of the task execution. Or, the "
"the final result."
),
)The implementation defines TaskStep alongside its result and the structured decision used to select what happens next. section 4.2.2
TaskHandler.get_next_step() returns the first TaskStep using the Task’s instruction. After execution, run_step() returns a TaskStepResult containing the step identifier and result content. The handler uses the previous result to decide whether to return another TaskStep or a TaskResult. The loop also stops on conditions such as an irrecoverable error or a maximum step count. section 4.2.2 section 4.2.3
The handler records each sub-step’s chat history in a rollout, providing a log of execution. In supervised execution, an operator can inspect and modify a TaskStep before passing it to run_step(), then inspect the result before advancing. section 4.2.3 section 8.4.2
Supervised Hailstone task
For a sequence starting at 4, get_next_step(None) produces a TaskStep instructing the agent to use next_number until it reaches 1. The operator runs and inspects successive steps. Once get_next_step() returns a TaskResult, the operator can accept it with complete(); a TaskStep cannot be passed to complete(). section 8.4.2
