liveBook · Manning

Run()

As explained in Build an AI Agent (From Scratch)

run() is an Agent method that manages the overall agent execution loop and returns its execution result.

run() is the Agent entry point: it creates or reuses an ExecutionContext, records the user input, drives repeated think–act steps, detects completion, and returns an AgentResult containing the output and execution trace. section 4.6.2

The execution loop coordinates:

  • Context creation or reuse, followed by an initial user Event. section 4.6.2
  • Repeated calls to step() until a final response appears or max_steps prevents an unbounded loop. section 4.6.2
  • Final-response detection by checking that an event contains neither tool calls nor tool results. section 4.6.2
  • Result extraction from the assistant message, exposed through result.output alongside result.context for debugging. section 4.6.2

Running a tool-using agent

An agent can solve a question with result = await agent.run("What is 1234 * 5678?"). Callers read the answer from result.output and inspect result.context.current_step or the full event trace when diagnosing behavior. section 4.6.2

Listing 4.18 Agent run() method
async def run(
    self, 
    user_input: str, 
    context: ExecutionContext = None
) -> AgentResult:
    # Create or reuse context
    if context is None:
        context = ExecutionContext()

    # Add user input as the first event
    user_event = Event(
        execution_id=context.execution_id,
        author="user",
        content=[Message(role="user", content=user_input)]
    )
    context.add_event(user_event)

    # Execute steps until completion or max steps reached
    while not context.final_result and context.current_step < self.max_steps:
        await self.step(context)

        # Check if the last event is a final response
        last_event = context.events[-1]
        if self._is_final_response(last_event):
            context.final_result = self._extract_final_result(last_event)

    return AgentResult(output=context.final_result, context=context)

The implementation shows how run() initializes context, appends input, invokes step(), checks completion, and returns AgentResult. section 4.6.2

Because workflows implement the same run() interface, sequential, parallel, and loop compositions can behave externally like a single agent. Transfers likewise delegate through run() while sharing context. section 9.3.1 section 9.5.5

How Build an AI Agent (From Scratch) covers it

How deep it goes
Explains context events, completion detection, result extraction, step limits, workflow composition, and transfer handling without detailing LLM internals.
Examples
Demonstrates arithmetic execution, execution-trace inspection, GAIA problem solving, nested workflows, and delegated agent transfer.
What it assumes
Assumes familiarity with Agent, tools, ExecutionContext, events, and step(); teaches the execution loop and result contract directly.
What you can do after
Apply it via an asynchronous Agent API to run questions, retrieve outputs, inspect traces, compose workflows, and delegate through shared context.
How it connects
Connects Agent, ExecutionContext, AgentResult, step(), tool calls, ReAct agents, workflows, and multi-agent transfer.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Build an AI Agent (From Scratch). The section links point to the parts of the book it draws on.