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
