The agent loop is implemented in src/smolagents/agents.py as a class hierarchy rooted at MultiStepAgent. Concrete subclasses are CodeAgent and ToolCallingAgent. The loop (_run_stream) cycles through ReAct steps: generate → parse action → execute → store observation → repeat until final_answer() is called or max_steps is reached.
Key design points for engineers:
-
Steps are yielded as a generator (
_run_stream), enabling both blocking (run()) and streaming (run(stream=True)) modes. -
A
planning_intervalparameter triggers an optional planning step that injects an explicit plan into the agent's context every N steps. -
final_answer_checksaccepts a list of validator callables that gate whether a proposed final answer is accepted. -
MultiStepAgentis abstract; subclasses must implementinitialize_system_prompt()and_step_stream(). -
RunResultis the return type whenreturn_full_result=True; it carries output, state, step list, token usage, and timing. -
src/smolagents/agents.py